Android开发-RecyclerView实现加载多类型列表视图样式

前 言

在平常的业务场景中RecyclerView加载列表视图样式都是单一的类型,但是有些应用场景下需要在RecyclerView下加载不同的列表视图样式。比如以下应用场景:
Android开发-RecyclerView实现加载多类型列表视图样式_第1张图片
从上图中可以看出,在一个RecyclerView控件中可以同时加载两种不同的视图列表。那么下面就来看看如何实现这种效果。

编码实现
  • 创建一个用于加载视图列表的Adapter
public class FilmReviewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<FilmReviewInfo> mList;
    private FilmReviewInfo data;
    private OnItemClickListener mOnItemClickListener;

    private static int VIEW_TYPE_1 = 1;
    private static int VIEW_TYPE_2 = 2;

    public FilmReviewAdapter(Context context, List<FilmReviewInfo> mList) {
        this.mContext = context;
        this.mList = mList;
    }

    @Override
    public int getItemViewType(int position) {
        if (mList.get(position).getCoverImgType() == 2) {
            return VIEW_TYPE_2;
        }
        return VIEW_TYPE_1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_2) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_film_review2, parent, false);
            ViewHolder2 holder = new ViewHolder2(view);
            return holder;
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_film_review1, parent, false);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        data = mList.get(position);
        if (holder instanceof ViewHolder) {
            RequestOptions options = new RequestOptions()
                    .placeholder(R.drawable.movie_bg)
                    .error(R.drawable.movie_bg);
            Glide.with(mContext).load(data.getCoverImg()).apply(options).into(((ViewHolder) holder).iv_cover);
            ((ViewHolder) holder).tv_author.setText(data.getAuthor());
            ((ViewHolder) holder).tv_published_date.setText(data.getPublishedDate());
            ((ViewHolder) holder).tv_title.setText(data.getTitle());
        } else if (holder instanceof ViewHolder2) {
            RequestOptions options = new RequestOptions()
                    .placeholder(R.drawable.movie_bg)
                    .error(R.drawable.movie_bg);
            Glide.with(mContext).load(data.getCoverImg()).apply(options).into(((ViewHolder2) holder).iv_cover);
            ((ViewHolder2) holder).tv_author.setText(data.getAuthor());
            ((ViewHolder2) holder).tv_published_date.setText(data.getPublishedDate());
            ((ViewHolder2) holder).tv_title.setText(data.getTitle());
        }

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_cover;
        private TextView tv_author;
        private TextView tv_published_date;
        private TextView tv_title;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_cover = (ImageView) itemView.findViewById(R.id.iv_cover);
            tv_author = (TextView) itemView.findViewById(R.id.tv_author);
            tv_published_date = (TextView) itemView.findViewById(R.id.tv_published_date);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
        }
    }

    public static class ViewHolder2 extends RecyclerView.ViewHolder {

        private ImageView iv_cover;
        private TextView tv_author;
        private TextView tv_published_date;
        private TextView tv_title;

        public ViewHolder2(View itemView) {
            super(itemView);
            iv_cover = (ImageView) itemView.findViewById(R.id.iv_cover);
            tv_author = (TextView) itemView.findViewById(R.id.tv_author);
            tv_published_date = (TextView) itemView.findViewById(R.id.tv_published_date);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);

    }

    public void setItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

}

代码中的getItemViewType(int position)方法是用来返回某一条目下要加载的视图类型;onCreateViewHolder(ViewGroup parent, int viewType)方法可以根据viewType的数字类型来加载视图类型的布局。比如viewType==1 ,那么加载LayoutInflater.from(mContext).inflate(R.layout.item_film_review1, parent, false) ,viewType == 2的话,那么加载LayoutInflater.from(mContext).inflate(R.layout.item_film_review2, parent, false)。

  • 最后在Activity或者Fragment里加载RecyclerView控件和设置Adapter。代码如下:
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 1));
        adapter = new FilmReviewAdapter(this, mList);
        mRecyclerView.setAdapter(adapter);
界面运行截图如下:

Android开发-RecyclerView实现加载多类型列表视图样式_第2张图片

apk安装包下载体验地址:

可以扫描以下二维码进行下载安装,或者点击以下链接 http://app.fukaimei.top/ce7b 进行下载安装体验。
Android开发-RecyclerView实现加载多类型列表视图样式_第3张图片

———————— The end ————————

码字不易,如果您觉得这篇博客写的比较好的话,可以赞赏一杯咖啡吧~~
在这里插入图片描述


Demo程序源码下载地址一(GitHub)
Demo程序源码下载地址二(码云)

你可能感兴趣的:(Android开发随笔,android,移动开发,安卓,RecyclerView,Java)