提问者:小点点

滚动后,Recycler移动到第一个位置


我在使用回收视图和Gridview时遇到了困难。问题是当我的应用程序成功加载下一页(下一个数据)时,回收视图总是回到顶部。

我希望回收视图从最后一个索引开始。

示例:第一页加载10个项目,在成功贷款下一页后,回收器视图从项目8开始滚动。

为了解决这个问题,我已经尝试了StackOverflow上的所有解决方案,但仍然一无所获。

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                isScrolling = true;
            }

        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            currentItems = manager.getChildCount();
            totalItems = manager.getItemCount();
            scrolloutItems = manager.findFirstVisibleItemPosition();

            if (isScrolling && (currentItems + scrolloutItems == totalItems)) {
                isScrolling = false;
                getdata(pagetoken);

                //getdata(pagetoken);

                Toast.makeText(MainActivity.this, "Scrolling", Toast.LENGTH_SHORT).show();

            }
        }
    });

}


public void getdata() {
    progress.setVisibility(View.VISIBLE);
    ApiClient.getApis().getblogdata(Constants.key).enqueue(new Callback<MyModel>() {
        @Override
        public void onResponse(Call<MyModel> call, Response<MyModel> response) {
            if (response.isSuccessful()) {
                assert response.body() != null;
                arraylist.addAll(response.body().items);
            }

            if (arraylist != null) {
                adapter = new Data_Adapter(MainActivity.this, arraylist);
                recyclerView.setAdapter(adapter);
            }

            pagetoken = response.body().nextPageToken;

            swiprefresh.setRefreshing(false);
            progress.setVisibility(View.GONE);
        }

        @Override
        public void onFailure(Call<MyModel> call, Throwable t) {
            Toast.makeText(MainActivity.this, "\t \t Connection Error : \n Please Check Your Internet Connection", Toast.LENGTH_SHORT).show();
        }
    });
}

public void getdata(String pageToken) {
    ApiClient.getApis().getblogdata(Constants.key, pageToken).enqueue(new Callback<MyModel>() {
        @Override
        public void onResponse(Call<MyModel> call, Response<MyModel> response) {

            if (response.isSuccessful()) {
                assert response.body() != null;
                arraylist.addAll(response.body().items);
            }

            if (arraylist != null) {
                adapter = new Data_Adapter(MainActivity.this, arraylist);
                recyclerView.setAdapter(adapter);
            }
            if (response.body().nextPageToken != null) {
                pagetoken = response.body().nextPageToken;
            }
            Log.d("retro", "" + arraylist.size());

            swiprefresh.setRefreshing(false);
        }

        @Override
        public void onFailure(Call<MyModel> call, Throwable t) {
            Toast.makeText(MainActivity.this, "Connection Error : \n Please Check Your Internet Connection", Toast.LENGTH_SHORT).show();
        }
    });
}

共1个答案

匿名用户

>

  • 问题:-添加第二页数据后,RecyclerView进入顶部位置

    • 原因:-您一直在创建适配器的新实例

      在您的情况下,适配器Data_Adapter并且您正在创建

      adapter = new Data_Adapter(MainActivity.this, arraylist);
      
      • 示例:-添加第二页数据后RecyclerView进入顶部

      >

    • 创建数组列表:-

      private ArrayList<String> catList = new ArrayList<>();
      

      on onCreate[初始化您的适配器并将其分配给回收视图]

      if (arraylist != null) {
          adapter = new Data_Adapter(MainActivity.this, arraylist);
          recyclerView.setAdapter(adapter);
      }
      

      在getdata方法中[在onResponse中通知适配器]:-

      if (arraylist != null && arraylist.size() > 0) {
          // to notify adapter
          adapter.notifyItemInserted(adapter.getItemCount());
      }