RecyclerView仅在滚动时更新 [英] RecyclerView updates only on scroll

查看:126
本文介绍了RecyclerView仅在滚动时更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的适配器带有RecyclerView布局:

public class SimpleRecyclerAdapter extends RecyclerView.Adapter<SimpleViewHolder> {

  protected List<JSONObject> data = new ArrayList<>();

  public List<JSONObject> getData() { return data; }
}     

我更新它的适配器,如:

I update it's adapter like:

public void udpdateFromJson( JSONObject payload ) {
  JSONArray msgs = payload.optJSONArray( "messages" );
  for( int ix = 0; null != msgs && ix < msgs.length(); ix++ ){
    JSONObject o = msgs.optJSONObject( ix );
    if( loadingMore ) adapter.getData().add( 1 + ix, o );
    else adapter.getData().add( o );
  }

  adapter.notifyDataSetChanged();
  recyclerView.invalidate();
}

问题是,只有触摸或滚动视图时,我才能看到新项目.

The problem is, that I get to see the new item only when I touch or scroll the view.

我想念什么?

更新:

为解决此问题,我替换了行

to fix the problem I replaced the lines

adapter.notifyDataSetChanged();
recyclerView.invalidate();

adapter.notifyItemRangeInserted( loadingMore ? 1 : 0, msgs.length() );

它奏效了

推荐答案

尝试指定插入或删除的项目的位置,而不是 notifyDataSetChanged(); .您可以使用

try specifying the position of the item inserted or removed instead of notifyDataSetChanged();. you can use

notifyItemInserted(int pos);
notifyItemRangeChanged(int start, int end);`

这篇关于RecyclerView仅在滚动时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆