Recyclerview嵌套适配器

view适配器和普通recycler一样 只是在里面进行了嵌套,布局文件在写一个recyclerview,在onBindViewHolder里面进行传值,实现内部
recyclerview的实现,内布局就和普通的recyclerview一样。

* @Data:2019/4/22 9:23
* 描述:
*/
public class OneRecyclerAdapter extends RecyclerView.Adapter {
private Context context;
private Good good;

    public OneRecyclerAdapter(Context context, Good good) {
        this.context = context;
        this.good = good;
    }
    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        }else if(position==1){
            return 1;
        } else if (position == 2) {
            return 2;
        }
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return 3;
    }

    //绑定布局
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == 0) {
            View inflate = View.inflate(context, R.layout.rxxp_view, null);
            return new RxxpHoler(inflate);
        }else if(i==1){
            View inflate = View.inflate(context, R.layout.pzsh_view, null);
            return new PzshHoler(inflate);
        } else if (i == 2) {
            View inflate = View.inflate(context, R.layout.mlss_view, null);
            return new MlssHoler(inflate);
        }
        return null;
    }
    //绑定数据
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof RxxpHoler) {
            Rxxp rxxp = good.getResult().getRxxp();
            ((RxxpHoler) viewHolder).rxxpTitle.setText(rxxp.getName());
            //创建适配器
            RxxpAdapter rxxpAdapter = new RxxpAdapter(context,rxxp);
            ((RxxpHoler) viewHolder).rxxpRecycler.setAdapter(rxxpAdapter);
            ((RxxpHoler) viewHolder).rxxpRecycler.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
        } else if (viewHolder instanceof PzshHoler) {
            Pzsh pzsh = good.getResult().getPzsh();
            ((PzshHoler) viewHolder).pzshTitle.setText(pzsh.getName());
            //创建适配器
            PzshAdapter pzshAdapter = new PzshAdapter(context,pzsh);
            ((PzshHoler) viewHolder).pzshRecycler.setAdapter(pzshAdapter);
            ((PzshHoler) viewHolder).pzshRecycler.setLayoutManager(new LinearLayoutManager(context));
        }else if (viewHolder instanceof MlssHoler) {
            Mlss mlss = good.getResult().getMlss();
            ((MlssHoler) viewHolder).mlssTitle.setText(mlss.getName());
            //创建适配器
            MlssAdapter mlssAdapter = new MlssAdapter(context,mlss);
            //设置
            ((MlssHoler) viewHolder).mlssRecycler.setAdapter(mlssAdapter);
            ((MlssHoler) viewHolder).mlssRecycler.setLayoutManager(new GridLayoutManager(context,2));
        }
    }

    public class RxxpHoler extends RecyclerView.ViewHolder {
        private TextView rxxpTitle;
        private RecyclerView rxxpRecycler;
        public RxxpHoler(@NonNull View itemView) {
            super(itemView);
            rxxpTitle=itemView.findViewById(R.id.rxxpTitle);
            rxxpRecycler=itemView.findViewById(R.id.rxxpRecycler);
        }
    }

    public class PzshHoler extends RecyclerView.ViewHolder {
        private TextView pzshTitle;
        private RecyclerView pzshRecycler;
        public PzshHoler(@NonNull final View itemView) {
            super(itemView);
            pzshTitle=itemView.findViewById(R.id.pzshTitle);
            pzshRecycler=itemView.findViewById(R.id.pzshRecycler);
        }
    }

    public class MlssHoler extends RecyclerView.ViewHolder {
        private TextView mlssTitle;
        private RecyclerView mlssRecycler;
        public MlssHoler(@NonNull View itemView) {
            super(itemView);
            mlssTitle=itemView.findViewById(R.id.mlssTitle);
            mlssRecycler=itemView.findViewById(R.id.mlssRecycler);;
        }
    }
}

你可能感兴趣的:(学习)