RecyclerView 分页加载不正确?
Posted
技术标签:
【中文标题】RecyclerView 分页加载不正确?【英文标题】:RecyclerView pagination does not load properly? 【发布时间】:2021-07-24 05:19:57 【问题描述】:我需要为 recyclerview 进行分页。首先加载20个数据。滚动后,它必须再加载 20 个数据。但是它将这 20 个数据加载为新列表并删除旧的 20 个数据。如何将它们添加到前 20 个末尾?我尝试了一些不同的方法,但我无法成功获取 JsonArray。
FirstFragment.java
public class FirstFragment extends Fragment
RecyclerView recyclerView;
private ArrayList<UserOrder> userOrders;
OrderByPointsAdapter orderByPointsAdapter;
ProgressBar progressBarMain, progressBarRecy;
private NestedScrollView nestedScrollView;
private int xo=0,yo=20;
@Override
public void onAttach(@NonNull Context context)
super.onAttach(context);
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.row_first, container, false);
progressBarRecy = view.findViewById(R.id.progressBarFirstRecy);
nestedScrollView = view.findViewById(R.id.nestedRowFirst);
recyclerView = view.findViewById(R.id.recyclerFirstRow);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
loadRecycler(xo,yo);
nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener()
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
if (scrollY == v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())
xo = xo+20;
yo = yo +20;
progressBarRecy.setVisibility(View.VISIBLE);
loadRecycler(xo, yo);
);
return view;
private void loadRecycler(int xo, int yo)
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.0.35/")
.addConverterFactory(GsonConverterFactory.create())
.build();
//RequestInterface ekliyoruz
RequestInterface request = retrofit.create(RequestInterface.class);
Call<JsonResponse> call = request.getJSON(String.valueOf(xo),String.valueOf(yo));
call.enqueue(new Callback<JsonResponse>()
@Override
public void onResponse(Call<JsonResponse> call, Response<JsonResponse> response)
if (response.isSuccessful() && response.body()!= null)
progressBarRecy.setVisibility(View.GONE);
JsonResponse jsonResponse = response.body();
userOrders = new ArrayList<>(Arrays.asList(jsonResponse.getUserOrders()));
orderByPointsAdapter = new OrderByPointsAdapter(userOrders,getActivity().getApplicationContext());
recyclerView.setAdapter(orderByPointsAdapter);
);
【问题讨论】:
【参考方案1】:发生了什么?
我们每次都设置从最后一个响应到回收器视图适配器的订单列表。在onResponse
函数中使用下面的sn-p来修复它。
解决方案
if (response.isSuccessful() && response.body()!= null)
progressBarRecy.setVisibility(View.GONE);
JsonResponse jsonResponse = response.body();
if(userOrders == null)
userOrders = new ArrayList<>(Arrays.asList(jsonResponse.getUserOrders()));
else
userOrders.addAll(Arrays.asList(jsonResponse.getUserOrders()));
orderByPointsAdapter = new OrderByPointsAdapter(userOrders,getActivity().getApplicationContext());
recyclerView.setAdapter(orderByPointsAdapter);
优化提示
不要多次重新初始化和重置适配器到回收站视图。相反,请使用相同的适配器并更新userOrders
列表。
使用回收站视图的ListAdapter
而不是RecyclerView.Adapter
来表示OrderByPointsAdapter
。
【讨论】:
以上是关于RecyclerView 分页加载不正确?的主要内容,如果未能解决你的问题,请参考以下文章
基于Android官方AsyncListUtil优化改进RecyclerView分页加载机制
Android Room联合AsyncListUtil实现RecyclerView分页加载ORM数据
recyclerView.addOnScrollListener - “使用 MVVM 改造分页”正在加载相同的响应/列表