Use custom View in a RecyclerView Adapter?

后端 未结 3 2055
天涯浪人
天涯浪人 2020-12-10 11:07

I have a basic custom View which looks like this:

public class CustomView extends RelativeLayout {

    private User user;

    private ImageView profilePict         


        
3条回答
  •  囚心锁ツ
    2020-12-10 11:29

    CustomView extends RelativeLayout {
    

    You have a View already (well, a ViewGroup)

    HOW TO INFLATE THE CUSTOM VIEW?

    You don't need to... The point of a Custom View object is to not need XML, therefore no inflation.

    You can create new CustomView(), but you need to set all the layout parameters, which looks cleaner in XML, I think.


    Most RecyclerView tutorials show inflating via XML though.

    View customView = inflater.inflate(...);
    ViewHolder viewHolder = new ViewHolder(customView);
    

    That should work because in the class chain, you have CustomView > RelativeLayout > ViewGroup > View

    LayoutInflater inflater = LayoutInflater.from(context);
    

    Like I said before, you don't need this if there is no XML file you want to inflate.

    You also don't need the context variable.

    parent.getContext() is a fine solution.

    // ANYTHING HERE?
    

    Well, yeah, you should "bind" the ViewHolder with the data that the ViewHolder should hold.

    Again, most, if not all, tutorials cover this.

提交回复
热议问题