recyclerview notifyDataSetChange

作者: 一盘好书 | 来源:发表于2018-03-12 16:54 被阅读50次

可能出现的原因

1.在子线程中调用了notifyDataSetChanged;
2.适配器中数据源为空。

踩坑记(适配器中数据为空)

初始化代码没有问题

mTypeConditionData = new ArrayList<>();
mTypeAreaConditionAdapter = new TypeAndAreaAdapter(getActivity(), mTypeConditionData);
mTypeAreaRecycler.setAdapter(mTypeAreaConditionAdapter);

在请求到数据后,对数据源进行如下操作。

private ArrayList<ChooseDateBean> getCategoryData(FilterItemInfoBean[] data) {
    if (data == null || data.length <= 0) {
        return null;
    }

    ArrayList<ChooseDateBean> beans = new ArrayList<>();

    for (FilterItemInfoBean aData : data) {
        if ("category_id".equals(aData.getName()) || "meetingroom_space".equals(aData.getName())) {
            FilterItemOptionInfoBean[] optionInfoBean = aData.getOptions();
            if (optionInfoBean == null) {
                continue;
            }

            beans.add(new ChooseDateBean(TypeAndAreaAdapter.TYPE_TITLE, aData.getLabel()));

            for (FilterItemOptionInfoBean anOptionInfoBean : optionInfoBean) {
                ItemContentBean bean = new ItemContentBean();
                bean.setText(anOptionInfoBean.getLabel());
                bean.setSelected(false);
                beans.add(new ChooseDateBean(TypeAndAreaAdapter.TYPE_ITEM, bean));
            }
        }
    }

    return beans;
}

更新适配器

mTypeConditionData = getCategoryData(data);
mTypeAreaConditionAdapter.notifyDataSetChanged();

看一张图来解释一下为什么这样做会有问题


解释

所以此时更新适配器时,适配器中的数据A对象size还是为0,recyclerview看起来没有任何的变化。

(绑定的绑写错了,大家当作没看到喔😄)

相关文章

网友评论

    本文标题:recyclerview notifyDataSetChange

    本文链接:https://www.haomeiwen.com/subject/unznfftx.html