我在 RecyclerView 中使用适配器类中的以下code
到show data
,但现在我想显示一些数据使用不同布局的相同 RecyclerView中的其他ArrayList
(some positions
喜欢:第1位和第6位)。
不同的布局(假设: another_layout.xml )包含 2 TextViews 和图像,也想要在布局的侦听器上实现单击 ..
@Override
public PlaylistCardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// inflate a card layout
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.youtube_video_card, parent, false);
// populate the viewholder
ViewHolder vh = new ViewHolder(v);
return vh;
}
答案 0 :(得分:1)
RecyclerView适用于多个布局
1.覆盖getItemViewType(int position)方法
例如,我有两个布局layout1和layout2.我希望layout1在顶部,然后layout2。所以getItemViewType就是这样的
@Override
public int getItemViewType(int position) {
if(position == 0){ //for layout1
return 1;
}else{
return 2; //for layout2
}
}
2。像这样的每个布局的不同视图
class ViewHolder_LayoutOne extends RecyclerView.ViewHolder{
TextView name;
//Constructor
}
class ViewHolder_LayoutTwo extends RecyclerView.ViewHolder{
TextView name;
//Constructor
}
3。根据位置膨胀不同的布局
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh = null;
if(viewType == 1){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_layout_one,parent,false);
vh = new MyViewHolder_LayoutOne(view);
}else if(viewType == 2){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_layout_two,parent,false);
vh = new MyViewHolder_LayoutTwo(view);
}
return vh;
}
4。根据职位绑定您的观点
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
switch(getItemViewType(position)){
case 1: //for layout 1
((ViewHolder_LayoutOne)holder).name.setText("");
break;
case 2:// for layout 2
((ViewHolder_LayoutTwo)holder).name.setText("");
break;
}
}
5.现在最重要的getItemCount()方法//返回视图数
@Override
public int getItemCount() {
return toptitles.length + 1;
// as i have only layout at the top and the remaining size equals the length of the array toptitles.So the overall length would be
//number of views of layout1 + number of views of layout2
}
希望这有帮助!!!
答案 1 :(得分:0)
您可以覆盖 getItemViewType ,并使用不同的ViewHolder进行不同的布局。
答案 2 :(得分:0)
您需要覆盖 getItemViewType ,如下所示:
@Override
public int getItemViewType(int position) {
Add your condition and return viewType(Constant)
}
现在,检查viewType并相应地重新调整viewHolder。
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEWHOLDER1) {
View v = LayoutInflater.from(activity).inflate(R.layout.layout1, parent, false);
return new ViewHolder1(v);
}else if(viewType == VIEWHOLDER2){
View v = LayoutInflater.from(activity).inflate(R.layout.layout2, parent, false);
return new ViewHolder2(v);
}
}
现在,检查viewHolder的实例
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolder1) {
Show values in UI accordingly.
}else if(holder instanceof ViewHolder2){
Show values in UI accordingly.
}
}
答案 3 :(得分:0)
您可以使用所需的布局在getItemViewType(int position)中设置所需的类型。在onCreateViewHolder中,您可以检查viewType并相应地对该行进行充气。