Android开发Android开发经验谈

RecyclerView使用以及避坑指南

2019-10-31  本文已影响0人  YoungerHu

基本使用

RecyclerView中添加header


当然这只是最基本的能用了,在使用过程中会遇到一些奇奇怪怪的问题,接下来我们一一解决


recyclerView中数据错乱问题

RecyclerView中的局部刷新

还是回到那个收藏的问题,如果点击收藏按钮,只更新当前的item这个需求如何实现?


最后,我把整个带Header的,可以局部更新UI的adapter基本框架都放出来,大家有需要的自行取用

完整结构

public class HelloRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int HEADER = 1;
    private static final int NORMAL = 2;
    private static final String COLLECTION = "collection";
    private static final String STAR = "star";

    private Context context;
    private Object headData;
    private List dataList;

    public HelloRecyclerAdapter(Context mContext,
                                Object headerData,
                                List answerList) {
        this.context = mContext;
        this.headData = headerData;
        this.dataList = answerList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEADER) {
            View view = LayoutInflater.from(context)
                    .inflate(R.layout.item_header_view, parent, false);
            return new HeaderViewHolder(view);
        } else {
            View view = LayoutInflater.from(context)
                    .inflate(R.layout.item_normal_view, parent, false);
            return new NormalViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        //不去覆写这个方法
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List<Object> payloads) {
        if (holder instanceof HeaderViewHolder) {
            bindHeaderView((HeaderViewHolder) holder, payloads);
        } else {
            bindNormalView((NormalViewHolder) holder, position, payloads);
        }
    }

    private void bindHeaderView(HeaderViewHolder holder, List<Object> payLoads) {
        if (payLoads != null && payLoads.size() > 0) {
            String payLoad = (String) payLoads.get(0);
            switch (payLoad) {
                case COLLECTION:
                    //header收藏数量更新
                    updateCollection(holder);
                    break;
                case STAR:
                    //header点赞数量更新
                    updateStar(holder);
                    break;
            }
        } else {
            //正常绑定数据
        }
    }

    private void bindNormalView(NormalViewHolder holder, int position, List<Object> payLoads) {
        if (payLoads != null && payLoads.size() > 0) {
            String s = (String) payLoads.get(0);
            switch (s) {
                case STAR:
                    //点赞数量更新
                    updateStar(holder, dataList.get(position - 1));
                    break;
                case COLLECTION:
                    //收藏数量更新
                    updateCollection(holder, dataList.get(position - 1));
                    break;
            }
        } else {
            //绑定正常数据
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEADER;
        } else {
            return NORMAL;
        }
    }

    public void collectionSuccess(String id) {
        //当header的收藏数量变化时候
        //通过ID找到下标
        int position = findPositionForId(id);
        //更新数量
        dataList.get(position).setCollectionNum(data.getCollectionNum()+1);
        //更新对应UI
        notifyItemChanged(position,COLLECTION);
    }

    public void disCollectionSuccess(String id) {
        //当header的收藏数量变化时候
        notifyItemChanged(0, COLLECTION);
    }

    public void starSuccess(String id) {
        int position = 0;
        //略去通过ID寻找position过程
        //略去通过ID更改对应数据的点赞数量
        notifyItemChanged(position, STAR);
    }

    public void disStarSuccess(String id) {
        int position = 0;
        //略去通过ID寻找position过程
        //略去通过ID更改对应数据的点赞数量
        notifyItemChanged(position, STAR);
    }

    class HeaderViewHolder extends RecyclerView.ViewHolder {
        public HeaderViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class NormalViewHolder extends RecyclerView.ViewHolder {
        public NormalViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读