Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Component #9

Closed
devloe opened this issue May 28, 2014 · 6 comments
Closed

Custom Component #9

devloe opened this issue May 28, 2014 · 6 comments

Comments

@devloe
Copy link

devloe commented May 28, 2014

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
Copy link
Author

devloe commented May 28, 2014

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
Copy link
Contributor

ivacf commented May 28, 2014

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
Copy link
Author

devloe commented May 28, 2014

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
Copy link
Contributor

ivacf commented May 28, 2014

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
Copy link
Author

devloe commented May 28, 2014

Excellent! It works like a charm. :)

@ivacf ivacf closed this as completed May 28, 2014
@devloe
Copy link
Author

devloe commented May 28, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants