ribot / easy-adapter

[DEPRECATED] Easy Adapters library for Android

Home Page:http://ribot.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom Component

devloe opened this issue · comments

I have this:

@LayoutId(R.layout.item_comment)
public class CommentViewHolder extends ItemViewHolder<Debate> {

What if I have a custom component that extends LinearLayout and is in charge of inflating and populating the R.layout.item_comment?

I use this to place a "comment" into a layout:

<com.example.testapp.components.CommentItem 
                        android:id="@+id/comment"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                    />

Is there a way to use a custom component with a view holder?

Hope I explained my situation in a way you can understand.

Here's an explanation of what I'm trying to do I think: http://blog.xebia.com/2013/07/22/viewholder-considered-harmful/

I guess why I need is be able to cast the inflation to a my custom component

EasyAdapter.java:119:

convertView = (CommentItem) mInflater.inflate(mItemLayoutId, parent, false);

What you think?

I've never tried this but you should be able to use a custom component in the same way as a normal layout file. Your item_comment.xml file should look like this:

<com.example.testapp.components.CommentItem 
        android:id="@+id/comment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

The EasyAdapter will inflate it and save a reference inside the ViewHolder so from the ViewHolder you can access your custom view like this:

CommentItem customView = (CommentItem) getView();

Ok I will try this.

Let's say I have a method on the CommentItem class that sets the UI:

class CommentItem extends LinearLayout{
       public void setupUI(Comment data){
            this.titleView.setText(data.title);
            this.descriptionView.setText(data.description);
      }
}

Is it possible to call this method like this?

public void onSetValues(Debate item, PositionInfo positionInfo) {
         getView().setUI(item);        
}

Sorry i'm new to Android.

getView() will return a generic View object so you will have to cast it to your specific custom view. This should work:

public void onSetValues(Debate item, PositionInfo positionInfo) {
    CommentItem commentView = (CommentItem) getView();
    commentView.setUI(item);        
}

Excellent! It works like a charm. :)

I know it was a little bit off topic. Thanks for all.