欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

RecyclerView多种item布局

程序员文章站 2022-05-29 20:24:40
...

先看效果图

右边的布局使用的是RecyclerView:基本的用法也就不多说了,都已经比较熟悉了。

RecyclerView多种item布局

通过上面的效果图 我们已经实现了最基本的RecyclerView多种item布局,下面我就仔细说下到底是怎么实现的吧:
实现多种样式核心方法如下:

  @Override
    public int getItemViewType(int position) {
        if (position < 1) {
            return 0;
        } else if (3 <= position && position < 7) {
            return 1;
        } else return 2;
    }

接收的position表示当前第几的item需要处理,通过然后返回想要处理成那种试图的样子,(我这里0表示一行只有一个布局,1表示一行有4个布局,2表示一行有两个布局);然后通过处理函数接收处理,代码如下:

 @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        int itemtype=getItemViewType(position);
        switch (itemtype){
            case 0:
                ViewHplder_match vh1= (ViewHplder_match) holder;
                vh1.goods_img_one.setImageDrawable(list.get(position).getGoodsimage());
                break;
            case 1:
                ViewHplder_two vh2= (ViewHplder_two) holder;
                vh2.goods_img_two.setImageDrawable(list.get(position).getGoodsimage());
                vh2.goods_nm_two.setText(list.get(position).getGoodsname());
                break;
            case 2:
                ViewHplder_four vh3= (ViewHplder_four) holder;
                vh3.goods_image.setImageDrawable(list.get(position).getGoodsimage());
                vh3.goods_name.setText(list.get(position).getGoodsname());
                vh3.goods_value.setText(list.get(position).getGoodsvalue());
                break;
        }

    }

ViewHplder_match是继承了RecyclerView.ViewHolder 表示一行只有一个布局 ,代码如下:(请自觉忽略我设置的点击事件)

public class ViewHplder_match extends RecyclerView.ViewHolder {


    public ImageView goods_img_one;
    public MyRecyclerViewAdapter.OnRecyclerViewItemClickListener mOnItemClickListener=null;

    public ViewHplder_match(View itemView,
                            final MyRecyclerViewAdapter.OnRecyclerViewItemClickListener
                                    mOnItemClickListener) {
        super(itemView);
        goods_img_one=(ImageView)itemView.findViewById(R.id.goods_img_one);
        this.mOnItemClickListener=mOnItemClickListener;
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnItemClickListener!=null){
                    mOnItemClickListener.onItemClick(v,getPosition());
                }
            }
        });
    }
}

当然ViewHplder_two和ViewHplder_four的实现也是特别类似的,主要实现布局样式就跟实现一个布局样式没有什么两样,自行脑补;

下面是MyRecyclerViewAdapter的实现:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<Goods> list;

    public MyRecyclerViewAdapter(Context context,List<Goods> mlist){
        this.context=context;
        this.list=mlist;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view=null;
        RecyclerView.ViewHolder holder=null;


        switch (viewType){
            case 0:
                view= LayoutInflater.from(context).inflate(R.layout.holder_lauout_match,null);
                holder=new ViewHplder_match(view,mOnItemClickListener);
                break;
            case 1:
                view=LayoutInflater.from(context).inflate(R.layout.holder_lauout_two,null);
                holder=new ViewHplder_two(view,mOnItemClickListener);
                break;
            case 2:
                view=LayoutInflater.from(context).inflate(R.layout.holder_lauout_four,null);
                holder=new ViewHplder_four(view,mOnItemClickListener);
                break;
        }


        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        int itemtype=getItemViewType(position);
        switch (itemtype){
            case 0:
                ViewHplder_match vh1= (ViewHplder_match) holder;
                vh1.goods_img_one.setImageDrawable(list.get(position).getGoodsimage());
                break;
            case 1:
                ViewHplder_two vh2= (ViewHplder_two) holder;
                vh2.goods_img_two.setImageDrawable(list.get(position).getGoodsimage());
                vh2.goods_nm_two.setText(list.get(position).getGoodsname());
                break;
            case 2:
                ViewHplder_four vh3= (ViewHplder_four) holder;
                vh3.goods_image.setImageDrawable(list.get(position).getGoodsimage());
                vh3.goods_name.setText(list.get(position).getGoodsname());
                vh3.goods_value.setText(list.get(position).getGoodsvalue());
                break;
        }

    }

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

    @Override
    public int getItemViewType(int position) {
        if (position < 1) {
            return 0;
        } else if (3 <= position && position < 7) {
            return 1;
        } else return 2;
    }

    /**
     * 下面是有关点击事件的一些东西
     */

    private OnRecyclerViewItemClickListener mOnItemClickListener=null;


    public interface OnRecyclerViewItemClickListener{
        void onItemClick(View view , int pos);
    }

    public void setRecyclerViewOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}

好了,RecyclerView的多种布局是不是特别简单,说白了也就是根据item在list中的position给布局分个类而已;