在recyclerView中如何使用LazyLoader和PullToRefresh?

How to use LazyLoader and PullToRefresh in recyclerView?

为什么没有人帮助我???我真的需要你的帮助

在我的应用程序中,我想使用 PullToRefreshLazyLoader 功能,为此我编写了以下代码并为我工作。

但我有一个问题,当 PullToRefresh recyclerView 和滚动 recyclerView 时不显示更多项目。

我的意思是:当使用 pullToRefresh 时,则不起作用 lazyLoader!

我的代码:

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        Constants.listeners.add(this);
        View v = inflater.inflate(R.layout.fragment_auction_current_tab, null);
        ButterKnife.bind(this, v);
        context = getActivity();
        handler = new Handler();
        list = v.findViewById(R.id.list);
        prefsUtils = new PrefsUtils(getActivity());
        apis = ApiClient.getClient().create(APIs.class);
        swipeToRefresh = v.findViewById(R.id.swipeToRefresh);

        jwtToken = Constants.settings.getString("jwt_token", "");

        list_item = getActivity().findViewById(R.id.list_item);
        linearLayoutManager = new LinearLayoutManager(context);
        auctionListAdapter = new AuctionsListAdapter(context, todayModel, R.layout.list_item_auction_normal_new);
        list.setLayoutManager(linearLayoutManager);
        list.setHasFixedSize(true);
        list.setAdapter(auctionListAdapter);
        list.setOnScrollListener(new EndlessRecyclerLinearPage(linearLayoutManager) {
            @Override
            public void onLoadMore(final int current_page) {
                miniLoader.setVisibility(View.VISIBLE);
                Call<AuctionsListResponse> call = apis.getTodatAuctions(jwtToken, current_page, 10);
                call.enqueue(new Callback<AuctionsListResponse>() {
                    @Override
                    public void onResponse(Call<AuctionsListResponse> call, Response<AuctionsListResponse> response) {
                        if (response.isSuccessful()) {
                            if (response.body() != null) {
                                if (response.body().getRes() != null) {
                                    if (response.body().getRes().getToday().size() > 0 && response.body().getRes().getToday() != null) {
                                        miniLoader.setVisibility(View.GONE);
                                        auctionListAdapter.addNewItem(response.body().getRes().getToday());
                                    } else {
                                        miniLoader.setVisibility(View.GONE);
                                    }
                                }
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<AuctionsListResponse> call, Throwable t) {

                    }
                });
            }
        });
        getData();
        swipeToRefresh.setColorSchemeResources(R.color.colorAccent);
        swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });

        return v;
    }

    private void getData() {
        Call<AuctionsListResponse> call = apis.getTodatAuctions(jwtToken, 1, 10);
        call.enqueue(new Callback<AuctionsListResponse>() {
            @Override
            public void onResponse(Call<AuctionsListResponse> call, Response<AuctionsListResponse> response) {
                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        if (response.body().getRes() != null) {
                            if (response.body().getRes().getToday().size() > 0 && response.body().getRes().getToday() != null) {
                                todayModel.clear();
                                todayModel.addAll(response.body().getRes().getToday());
                                auctionListAdapter.notifyDataSetChanged();
                                itemsLoader.setVisibility(View.GONE);
                                if (swipeToRefresh != null) {
                                    swipeToRefresh.setRefreshing(false);
                                }
                            }
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<AuctionsListResponse> call, Throwable t) {

            }
        });
    }
}

我该如何解决这个问题?

我认为问题出在你的getData()方法上。

为什么?当您从 RecyclerView.Adapter 调用 notifyDataSetChanged() 时,它会告诉您的适配器再次检查它拥有的列表,并完全重新绑定和重绘 RecyclerView 中的所有内容。但问题是,在你的 getData() 中你只设置了 todayModel 列表,它是你的 Fragment.

中的对象引用

因此,您需要修改 Adapter 中的列表 才能成功 notifyDataSetChanged().

实际上,您在 onLoadMore() 中所做的修改 inside 适配器中的列表是正确的。您应该在 getData().

中做相同(或类似)的事情

示例代码:

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    Constants.listeners.add(this);
    View v = inflater.inflate(R.layout.fragment_auction_current_tab, null);
    ButterKnife.bind(this, v);
    context = getActivity();
    handler = new Handler();
    list = v.findViewById(R.id.list);
    prefsUtils = new PrefsUtils(getActivity());
    apis = ApiClient.getClient().create(APIs.class);
    swipeToRefresh = v.findViewById(R.id.swipeToRefresh);

    jwtToken = Constants.settings.getString("jwt_token", "");

    list_item = getActivity().findViewById(R.id.list_item);
    linearLayoutManager = new LinearLayoutManager(context);
    auctionListAdapter = new AuctionsListAdapter(context, todayModel, R.layout.list_item_auction_normal_new);
    list.setLayoutManager(linearLayoutManager);
    list.setHasFixedSize(true);
    list.setAdapter(auctionListAdapter);
    list.setOnScrollListener(new EndlessRecyclerLinearPage(linearLayoutManager) {
        @Override
        public void onLoadMore(final int current_page) {
            miniLoader.setVisibility(View.VISIBLE);
            getData(currentPage);
        }
    });
    getData(1);
    swipeToRefresh.setColorSchemeResources(R.color.colorAccent);
    swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            getData(1);
        }
    });

    return v;
}

private void getData(int page) {
    Call<AuctionsListResponse> call = apis.getTodatAuctions(jwtToken, page, 10);
    call.enqueue(new Callback<AuctionsListResponse>() {
        @Override
        public void onResponse(Call<AuctionsListResponse> call, Response<AuctionsListResponse> response) {
            if (response.isSuccessful()) {
                if (response.body() != null) {
                    if (response.body().getRes() != null) {
                        if (response.body().getRes().getToday().size() > 0 && response.body().getRes().getToday() != null) {
                            todayModel.clear();
                            todayModel
                                 .addAll(response.body()
                                                 .getRes()
                                                 .getToday());

                            //NOTICE THE DIFFERENCE HERE
                            auctionListAdapter.setModel(todayModel);
                            auctionListAdapter.notifyDataSetChanged();
                            itemsLoader.setVisibility(View.GONE);
                            if (swipeToRefresh != null) {
                                swipeToRefresh.setRefreshing(false);
                            }
                            miniLoader.setVisibility(View.GONE);
                        }
                    }
                }
            }
        }

        @Override
        public void onFailure(Call<AuctionsListResponse> call, Throwable t) {
             itemsLoader.setVisibility(View.GONE);
             if (swipeToRefresh != null) {
                swipeToRefresh.setRefreshing(false);
             }
             miniLoader.setVisibility(View.GONE);
        }
    });
}

在您的 AuctionsListAdapter 中添加此方法。就是设置Adapter用来显示数据的列表:

public void setModel(List<Today> todayModel) {
    this.model = todayModel;
}