How to change the recyclerview first row layout?

纵饮孤独 提交于 2019-12-09 10:23:58

问题


I want to add different layout for the first row, how can i set it up?

Adapter class: This is the normal, all row equals:

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
inflater=LayoutInflater.from(parent.getContext());
return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_trends, parent, false));
}

And what to change in the OnBindViewHolder method?


回答1:


First you need to override getItemViewType. Return itemType according to your requirements.

@Override
public int getItemViewType(int position) {
    if (position == 0) return 1;
    else return 2;
}

then in onCreateViewHolder inflate your different layout according to your viewType.

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == 1) {
        // inflate your first item layout & return that viewHolder
    } else {
        // inflate your second item layout & return that viewHolder
    }
}


来源:https://stackoverflow.com/questions/40240405/how-to-change-the-recyclerview-first-row-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!