多条目加载数据的适配器

public class MyAdapter extends XRecyclerView.Adapter {
    Context context;
     List data;

    public MyAdapter(Context context, List data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getItemViewType(int position) {
      return position%2;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
    {
        View view = LayoutInflater.from(context).inflate(R.layout.item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i)
    {
        int ty = getItemViewType(i);
        switch (ty)
        {
            case 0:
                //DraweeHierarchy:子类是GenericDraweeHierarchy,主要用于维护和绘制Drawable对象,以及怎样展示等等
                GenericDraweeHierarchy gg = GenericDraweeHierarchyBuilder.newInstance(context.getResources())
                        .setRoundingParams(RoundingParams.fromCornersRadius(20))
                        .build();
                viewHolder.img.setHierarchy(gg);
                //控制器设置图片
                DraweeController build=  Fresco.newDraweeControllerBuilder()
                        .setUri(data.get(i).thumbnail_pic_s)
                       //设置在加载失败后,能否重试
                        .setTapToRetryEnabled(true)
                        .build();
                viewHolder.img.setController(build);
                break;
            case 1:
                GenericDraweeHierarchy gg1 = GenericDraweeHierarchyBuilder.newInstance(context.getResources())
                        .setRoundingParams(RoundingParams.asCircle())
                        .build();
                viewHolder.img.setHierarchy(gg1);
                DraweeController build1=  Fresco.newDraweeControllerBuilder()
                        .setUri(data.get(i).thumbnail_pic_s)
                        .setTapToRetryEnabled(true)
                        .build();
                viewHolder.img.setController(build1);
                break;
        }
        viewHolder.name.setText(data.get(i).title);





    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView name;
        private final SimpleDraweeView img;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(  R.id.name);
            img = itemView.findViewById(R.id.img);
        }
    }
}

你可能感兴趣的:(多条目加载数据的适配器)