首页
关于
友链
开往

Recyclerview多样式混合布局

问题:已知字段中会有一个名为layoutStyle的字段来控制布局的样式,求解这个recyclerview的adapter如何写

嘶,看到这个题目真是眉头一紧,因为对recyclerview不是很熟悉(当然对listview也不是很熟悉),哎 怎么办捏,他妈的。先照着网上的方法抄了以下,暂时如此,改天回头把recyclerview的代码读读

直接贴最重要的Aadpter方法了,其他都是次要的

public class MyAdapter extends RecyclerView.Adapter {
    public static final int TYPE_ONE = 1;
    public static final int TYPE_TWO = 2;

    private List<Bean> beanList = new ArrayList<>();

    public MyAdapter(List<Bean> beanList) {
        this.beanList = beanList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_ONE:
//                view  = View.inflate(parent.getContext(), R.layout.item_type_one,null);
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_type_one,parent,false);
                return new OneViewHolder(view);
            case TYPE_TWO:
//                view  = View.inflate(parent.getContext(), R.layout.item_type_two,null);
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_type_two,parent,false);
                return new TwoViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof OneViewHolder) {
            ((OneViewHolder) holder).mCover.setImageResource(beanList.get(position).getPic());
            ((OneViewHolder) holder).mDesc.setText(beanList.get(position).getDesc());
            ((OneViewHolder) holder).mTitle.setText(beanList.get(position).getTitle());
        } else if (holder instanceof TwoViewHolder) {
            ((TwoViewHolder) holder).mAvator.setImageResource(beanList.get(position).getPic());
            ((TwoViewHolder) holder).mName.setText(beanList.get(position).getName());
            ((TwoViewHolder) holder).mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("==","你点击的是第 "+position+" 项");
                 }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        int layoutStyle = beanList.get(position).getLayoutStyle();
        switch (layoutStyle) {
            case 1:
                return TYPE_ONE;
            case 2:
                return TYPE_TWO;
        }
        return 0;
    }

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

    private class OneViewHolder extends RecyclerView.ViewHolder{
        private ImageView mCover;
        private TextView mDesc;
        private TextView mTitle;

        public OneViewHolder(@NonNull View itemView) {
            super(itemView);
            mCover = itemView.findViewById(R.id.iv_cover);
            mDesc = itemView.findViewById(R.id.tv_desc);
            mTitle = itemView.findViewById(R.id.tv_title);
        }
    }
    private class TwoViewHolder extends RecyclerView.ViewHolder{
        private ImageView mAvator;
        private TextView mName;
        private Button mButton;

        public TwoViewHolder(@NonNull View itemView) {
            super(itemView);
            mAvator = itemView.findViewById(R.id.iv_avator);
            mName = itemView.findViewById(R.id.tv_name);
            mButton = itemView.findViewById(R.id.btn_btn);
        }
    }
}

里面还遇到了一个问题,未解决

//                view  = View.inflate(parent.getContext(), R.layout.item_type_two,null);
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_type_two,parent,false);

这两个方法都能弄出视图来,可是,第一个方法呈现的视图是紧凑的,也就是我的margin值是无效的….留个坑

#Android #Recyclerview