更新Android中的RecyclerView项目

时间:2019-04-27 09:54:51

标签: android android-recyclerview

我想使用向API发送请求和计时器来更新RecyclerView中的项目。

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    get_items();

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        synchronized public void run() {
            get_items();
        }
    }, TimeUnit.SECONDS.toMillis(3), TimeUnit.SECONDS.toMillis(3));

}

public void get_items(){
    Client client = ServiceGenerator.createService(Client.class,getBaseContext());
    final Call<List<Items>> call = Client.getItems("items");
    call.enqueue(new Callback<List<Items>>() {
        @Override
        public void onResponse(Call<List<Items>> call, Response<List<Items>> response) {
            List<Items> items = response.body();
            ItemsAdapter adapter = new ItemsAdapter(items);
            recyclerView = findViewById(R.id.chatrecycler);
            recyclerView.setAdapter(adapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(Activity.this, LinearLayoutManager.VERTICAL,true));
        }
        @Override
        public void onFailure(Call<List<Items>> call, Throwable t) {
        }
    });
}

列表每3秒更新一次,但从头开始。

在更新过程中我应该怎么做才能保持列表的位置?

3 个答案:

答案 0 :(得分:0)

在刷新之前获取RecyclerView项的位置 然后更新您的回收站视图,而不是重新创建 如下替换您的onResponse

select ETAB.ET_ETABLISSEMENT as 'STORE CODE'

[Month-1]=(select CONVERT(DECIMAL(15,2),sum(gl_totalttcdev)) 
from piece left join ligne on gl_souche=gp_souche and gl_naturepieceg=gp_naturepieceg and gl_numero=gp_numero and gl_indiceg=gp_indiceg 
    left join etabliss as e on gp_etablissement=et_etablissement 
    left join ARTICLE on GL_CODEARTICLE = ARTICLE.GA_CODEARTICLE
    where gp_naturepieceg='FFO' 
    and year(gp_datepiece) = year(DATEADD(MONTH,-1,GETDATE()))
     and  month(gp_datepiece) =  MONTH( DATEADD(MONTH,-1,GETDATE())) 
    and gl_typearticle<>''
    and gl_typearticle<>'FI' 
    and ETAB.ET_ETABLISSEMENT =e.ET_ETABLISSEMENT
    and ETAB.ET_LIBELLE =e.ET_LIBELLE
    group by gp_etablissement, et_libelle),

    [Month-2]=(select CONVERT(DECIMAL(15,2),sum(gl_totalttcdev)) 
    from piece left join ligne on gl_souche=gp_souche and gl_naturepieceg=gp_naturepieceg and gl_numero=gp_numero and gl_indiceg=gp_indiceg left join etabliss as e on gp_etablissement=et_etablissement 
    left join ARTICLE on GL_CODEARTICLE = ARTICLE.GA_CODEARTICLE
    where gp_naturepieceg='FFO' 
    and year(gp_datepiece) = year(DATEADD(MONTH,-1,GETDATE()))
     and  month(gp_datepiece) =  MONTH( DATEADD(MONTH,-1,GETDATE())) 
    and gl_typearticle<>''
    and gl_typearticle<>'FI' 
    and ETAB.ET_ETABLISSEMENT =e.ET_ETABLISSEMENT
    and ETAB.ET_LIBELLE =e.ET_LIBELLE
    group by gp_etablissement, et_libelle),

    [Some thing for the other months ..]
    [Month-24]..,

    from ETABLISS ETAB

答案 1 :(得分:0)

您不应在每个api调用中创建新的RecyclerView对象。此外,您还将在每个调用中为RecycltView分配适配器和LayoutManager。

您只需要使用RecycletView设置一次setAdapter,然后第二次只需在适配器列表中添加新数据并通知适配器刷新即可。

请检查以下代码。

private ItemsAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    recyclerView = findViewById(R.id.chatrecycler);
    recyclerView.setLayoutManager(new LinearLayoutManager(SingleChat.this, LinearLayoutManager.VERTICAL,true));

    get_items();

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        synchronized public void run() {
            get_items();
        }
    }, TimeUnit.SECONDS.toMillis(3), TimeUnit.SECONDS.toMillis(3));
}

public void get_items(){
    Client client = ServiceGenerator.createService(Client.class,getBaseContext());
    final Call<List<Items>> call = Client.getItems("items");
    call.enqueue(new Callback<List<Items>>() {
        @Override
        public void onResponse(Call<List<Items>> call, Response<List<Items>> response) {
            List<Items> items = response.body();

            if(adapter == null) {
                adapter = new ItemsAdapter(items);
                recyclerView.setAdapter(adapter);
            } else {
                runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       adapter.addMoreItems(items);
                       adapter.notifyDatasetChanged();
                   }
              });
            }
        }
        @Override
        public void onFailure(Call<List<SingleChatItem>> call, Throwable t) {
        }
    });
}
  

在您的ItemsAdapter.java中添加一种方法,

public void addMoreItems(List<Items> items) {
    this.items = items;
}

答案 2 :(得分:0)

我还没有亲自测试过此代码,但是它必须可以工作。请告诉我是否没有。

    final ItemsAdapter adapter = null;
    ArrayList<Items> items = null;
    recyclerView = findViewById(R.id.chatrecycler);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(Activity.this, LinearLayoutManager.VERTICAL,true));
    call.enqueue(new Callback<List<Items>>() {
        @Override
        public void onResponse(Call<List<Items>> call, Response<List<Items>> response) {
            List<Items> newItems = response.body();
            if(adapter == null){
                adapter = new ItemsAdapter(items);
                items.addAll(newItems);
            }else{
                int i = 0;
                for(Item item : items){
                    int index = newItems.indexOf(item);
                    item.stuff = newItems.get(index).stuff;
                    adapter.notifyItemChanged(i);
                    i++;
                }
            }
        }
        @Override
        public void onFailure(Call<List<Items>> call, Throwable t) {
        }
    });