Android RecyclerView不会更新数据更新

时间:2017-08-31 09:09:34

标签: android android-recyclerview recycler-adapter notifydatasetchanged

RecyclerView仅在创建时显示ArrayList(适配器的数据)中的项目。我添加更多行的所有尝试似乎都有效,但列表仍保留初始数据。 它没有重复,因为我尝试了谷歌上发现的所有建议。 我已经做了什么:

  • adapter.notifyDataSetChanged();
  • adapter.notifyItemChanged(5);
  • recycleList.invalidate();
  • recycleList.setAdapter(适配器);
  • 使用Handler
  • 在UIThread上运行更新

适配器可以正常工作 - onCreateView可以正常工作(创建新行)以及onBindView(绑定新数据)。 getItemCount()返回正确的值(更新后更大)。一切看起来应该是 - 而不是最终的观点 - 不会改变。

唯一可行的方法是使用新数据集从头开始重新创建适配器 - 设计完全不可接受(长列表上很难看)。

以下是代码:

以片段形式创建:

adapter = new MyListAdapter(getContext());
adapter.addItems(initialItems); // These and only these I see on the list
LinearLayoutManager mLayoutManager=new LinearLayoutManager(getContext());
recycleList.setLayoutManager(mLayoutManager);
recycleList.setAdapter(adapter);

尝试从片段更新:

public void onUpdateReady(ArrayList<Model> freshData) {
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() { // UI thread
            adapter.addItems(freshData);
            adapter.notifyDataSetChanged();
            adapter.notifyItemChanged(5);
            recycleList.invalidate();
            //notifyItemRangeInserted(3,freshData.size());
        }
    });
}

适配器:

private Context mContext;
private ArrayList<Model> data;

public MyListAdapter(Context mContext) {
    this.mContext = mContext;
    data=new ArrayList<>();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View view = inflater.inflate(R.layout.list_item, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {        
    viewHolder.textName.setText(data.get(position).getName());
}

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

public void addItems(Collection<Model> list) {
    data.addAll(list);
}

public class ViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.textName) TextView textName;

    public ViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this,itemView);
    }
}

更新:根据评论更改:

public void addItems(Collection<Model> list) {
    ArrayList<Model> newData=new ArrayList<>(); // Make new array
    newData.addAll(data); // Take old
    newData.addAll(list); // Add new
    data=newData; // change to data to newly created array
    notifyDataSetChanged();
}

3 个答案:

答案 0 :(得分:0)

更改适配器构造函数:

private ArrayList<Model> data;
public MyListAdapter(Context mContext,ArrayList<Model> data) {
this.mContext = mContext;
this.data = data;}

public void addItems(Collection<Model> list) {
data.addAll(list);
 }

片段:

adapter = new MyListAdapter(getContext(),initialItems);

答案 1 :(得分:0)

try this

In Fragment
ArrayList<Data> items=new ArrayList<>();

items.addAll(initialItems);
adapter = new MyListAdapter(getContext(),items);
LinearLayoutManager mLayoutManager=new LinearLayoutManager(getContext());
recycleList.setLayoutManager(mLayoutManager);
recycleList.setAdapter(adapter);


public void onUpdateReady(ArrayList<Model> freshData) {
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() { // UI thread
            items.addAll(freshData);
            adapter.notifyDataSetChanged();
            followList.invalidate();

        }
    });
}



private Context mContext;
private ArrayList<Model> data;

public MyListAdapter(Context mContext,ArrayList<Model> data) {
    this.mContext = mContext;
    this.data=data;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View view = inflater.inflate(R.layout.list_item, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {        
    viewHolder.textName.setText(data.get(position).getName());
}

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


public class ViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.textName) TextView textName;

    public ViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this,itemView);
    }
}

答案 2 :(得分:0)

我有一个类似的问题,并且在ListView上调用invalidateViews工作,即使它是一个黑客。而不是recycleList.invalidate();尝试recycleList.invalidateViews();