ribot / easy-adapter

[DEPRECATED] Easy Adapters library for Android
http://ribot.github.io
Apache License 2.0
421 stars 50 forks source link

Custom Component #9

Closed devloe closed 10 years ago

devloe commented 10 years ago

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/

devloe commented 10 years ago

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?

ivacf commented 10 years ago

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();
devloe commented 10 years ago

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.

ivacf commented 10 years ago

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);        
}
devloe commented 10 years ago

Excellent! It works like a charm. :)

devloe commented 10 years ago

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