recyclerview适配器

//继承RecycleView适配器
public class MyAdapter extends RecyclerView.Adapter {
private Context context;
private List result;
private static final int TYPE_ONE = 0;
private static final int TYPE_TWO = 1;
@Override
public int getItemViewType(int position) {
return position % 2;
}
public MyAdapter(Context context, List result) {
this.context = context;
this.result = result;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (TYPE_ONE == i) {
//引入条目布局
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_item, null, false);
//传给自定义Viewholder
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
} else {
//引入条目布局
View view = LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_expandable_list_item_1, null, false);
//传给自定义Viewholder
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
int itemViewType = getItemViewType(i);
if (TYPE_ONE == itemViewType) {
//给控件赋值
viewHolder.title.setText(result.get(i).getTitle());
Glide.with(context).load(result.get(i).getImageUrl()).into(viewHolder.img);
} else {
viewHolder.title2.setText(result.get(i).getTitle());
}
}
@Override
public int getItemCount() {
return result.size();
}

//自定义ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder {

    private final ImageView img;
    private final TextView title;
    private final TextView title2;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        //通过布局查找控件
        title = itemView.findViewById(R.id.title1);
        title2 = itemView.findViewById(android.R.id.text1);
        img = itemView.findViewById(R.id.img);
    }
}

}


你可能感兴趣的:(recyclerview适配器)