如何在recyclerView中使用LazyLoader和PullToRefresh?

时间:2018-06-11 05:10:46

标签: java android android-recyclerview lazy-loading pull-to-refresh

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

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

但我有一个问题,当 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) {

            }
        });
    }
}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我认为问题在于您的getData()方法。

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

因此,您需要修改适配器中的列表才能成功notifyDataSetChanged()

实际上,您在onLoadMore()中所做的是正确的,您修改了适配器中的列表 。您应该在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中添加此方法。它是设置适配器用来显示数据的列表:

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