addOnScrollListener 的位置在滚动后加载新数据时重置为顶部

The position of addOnScrollListener is resets to top when new data is loaded after scrolling

我有一个回收站视图并在其上实现了 addOnScrollListener。以下是我的api http://uat.nirc.com.np:8080/GWP/inventory/getInventoryRequestOnMobile?memberId=2&offset=0

在我的 api 中有偏移量,每个值最多包含 10 个项目。以下是我的主要 class 实现 addOnScrollListener 的地方。

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
    {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) 
       {
            super.onScrollStateChanged(recyclerView, newState);
            if(newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL)
            {
                isScrolling = true;
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            currentItems = layoutManager.getChildCount();
            totalItems = layoutManager.getItemCount();
            scrollOutItems = layoutManager.findFirstVisibleItemPosition();

            if(isScrolling && (currentItems + scrollOutItems == totalItems ))
            {
                isScrolling = false;
                getData();
            }

        }
    });

下面是getData函数

 public void getData()
   {
    if(token <= reqCount){
        progressBar.setVisibility(View.VISIBLE);
        token = token + 9;
        datacall(token);
    }
}

下面是datacall函数

public void datacall(int token){
    apiInterface = ApiClient.getApiClient().create(TalikaApiInterface.class);
    Call<JinsiAnirodhTalikaModel> call = apiInterface.getData(id,token);
    call.enqueue(new Callback<JinsiAnirodhTalikaModel>() {
        @Override
        public void onResponse(Call<JinsiAnirodhTalikaModel> call, Response<JinsiAnirodhTalikaModel> response) {
            requestCount = Integer.parseInt(response.body().requestCount);
            for(Data a :response.body().data){
                posts.add(a);
            }
            adapter = new JinsiAnurodhTalikaAdapter(posts,getApplicationContext());
             progressBar.setVisibility(View.GONE);
            adapter.notifyDataSetChanged();
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFailure(Call<JinsiAnirodhTalikaModel> call, Throwable t) {
            Log.e("error",t.getMessage());
        }
    });
}

每个令牌中有 10 个项目。滚动到底部后,新数据被加载并且令牌增加 +9。但是在获取新数据时,scrollListener 的位置重置为顶部,即随着底部获取新数据,recyclerView 重置为顶部。

有人可以帮助我吗?我希望 scrollListener 的位置保持在最后一个位置。

当您第二次获取数据时,在您使用 adapter.notifydatasetchanged(); 的情况下使用 adapter.notifydatasetchanged(); 而不是设置适配器,而在第二行您再次 recyclerView.setAdapter(adapter); 设置适配器在 recyclerview 上,当您在列表中加载更多项目时,他们不需要第二次在回收站视图上设置适配器,只是不要第二次在列表上设置适配器,这应该可以解决问题

这样做

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
//  Your other code
    ...

    adapter = new JinsiAnurodhTalikaAdapter(posts,getApplicationContext());
    recyclerView.setAdapter(adapter);
}

并在您的 dataCall 方法中

public void datacall(int token){
    apiInterface = ApiClient.getApiClient().create(TalikaApiInterface.class);
    Call<JinsiAnirodhTalikaModel> call = apiInterface.getData(id,token);
    call.enqueue(new Callback<JinsiAnirodhTalikaModel>() {
        @Override
        public void onResponse(Call<JinsiAnirodhTalikaModel> call, Response<JinsiAnirodhTalikaModel> response) {
            requestCount = Integer.parseInt(response.body().requestCount);
            for(Data a :response.body().data){
                posts.add(a);
            }

            progressBar.setVisibility(View.GONE);
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(Call<JinsiAnirodhTalikaModel> call, Throwable t) {
            Log.e("error",t.getMessage());
        }
    });
}