recyclerview 适配器添加空布局

  • Post author:
  • Post category:其他


项目后期要给recyclerview列表增加空布局,找一种方便的改动小的方法。

一、网上看了几种方法感觉不太好用:

第一种:重写RecyclerView,试了没效果。

第二种:对适配器的修改。这种方法对应已经完成的项目不太好修改。

二、自己研究修改适配器如下(推荐):

空布局适配器当作父类(抽象两个方法):

public abstract class EmptyAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public Context mContext;
    private final int EMPTY_VIEW = 0;
    private final int NOT_EMPTY_VIEW = 1;
    public List<T> mDatas = new ArrayList<>();

    public EmptyAdapter(Context context) {
        this.mContext = context;
    }

    public EmptyAdapter(List<T> mDatas, Context context) {
        this.mDatas = mDatas;
        this.mContext = context;
    }

    public void refreshData(List<T> mDatas) {
        this.mDatas.clear();
        this.mDatas.addAll(mDatas);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        //获取传入adapter的条目数,没有则返回 1
        //位空视图保留一个条目
        return (mDatas != null && mDatas.size() > 0) ? mDatas.size() : 1;
    }

    @Override
    public int getItemViewType(int position) {
        //根据传入adapter来判断是否有数据
        if (mDatas != null && mDatas.size() > 0)
            return NOT_EMPTY_VIEW;
        return EMPTY_VIEW;
    }

    public abstract RecyclerView.ViewHolder createMyViewHolder(ViewGroup parent, int viewType);

    public abstract void bindMyViewHolder(RecyclerView.ViewHolder holder, int position);

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //展示空视图或者调用传入adapter方法
        if (viewType == EMPTY_VIEW)
            return new             EmptyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_nodata, parent, false));
        return createMyViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == EMPTY_VIEW)
            return;
        bindMyViewHolder(holder, position);
    }

    class EmptyViewHolder extends RecyclerView.ViewHolder {

        public EmptyViewHolder(View itemView) {
            super(itemView);
        }
    }
}

真正的自己的适配器,继承上面的EmptyAdapter,实现createMyViewHolder()和bindMyViewHolder()方法即可。

public class RvGuideRecomdAdapter extends EmptyAdapter<MessCtData> {

    private LayoutInflater inflater;

    public RvGuideRecomdAdapter(Context context) {
        super(context);
        inflater = LayoutInflater.from(mContext);
    }

    // 重写onCreateViewHolder方法,返回一个自定义的ViewHolder
    @Override
    public RecyclerView.ViewHolder createMyViewHolder(ViewGroup parent, int viewType) {
        return new RvGuideRecomdAdapter.ViewHolder(inflater.inflate(R.layout.item_guide_recomd, parent, false));
    }

    // 填充onCreateViewHolder方法返回的holder中的控件
    @Override
    public void bindMyViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((RvGuideRecomdAdapter.ViewHolder) holder).bind(position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @ViewInject(R.id.root_layout)
        LinearLayout root_layout;
        @ViewInject(R.id.recomd_iv)
        ImageView recomd_iv;
        @ViewInject(R.id.recomd_titletv)
        TextView recomd_titletv;
        @ViewInject(R.id.recomd_texttv)
        TextView recomd_texttv;

        public ViewHolder(View itemView) {
            super(itemView);
            ViewUtils.inject(this, itemView);
        }

        public void bind(final int position) {
            String picUrl = mDatas.get(position).getPicUrl();
            if (mDatas.get(position).getResult() != null && "13".equals(mDatas.get(position).getResult()))
                picUrl = TextUtils.isEmpty(mDatas.get(position).getPicUrl()) ? "http://travel.enn.cn/group1/M00/00/34/CiaAUlsPSZKAJw79AAI_aCrfk0g165.jpg" : mDatas.get(position).getPicUrl();
            Glide.with(mContext).load(picUrl)
                    .apply(new RequestOptions().centerCrop().error(R.mipmap.icon_lxtj_err)).into(recomd_iv);
            recomd_titletv.setText(mDatas.get(position).getName());
            recomd_texttv.setText(mDatas.get(position).getDescription());
            if (!TextUtils.isEmpty(mDatas.get(position).getResult()) && "13".equals(mDatas.get(position).getResult()))
                recomd_texttv.setText(mDatas.get(position).getComment());
            root_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null)
                        mItemClickListener.onItemClick(position);
                }
            });
        }
    }

    // 给RecyclerView的Item添加点击事件
    public RvGuideRecomdAdapter.OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(RvGuideRecomdAdapter.OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }
}

正常使用自己的适配器就可以,数据为空时会自动显示空白布局。



版权声明:本文为a_yue10原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。