滑动刷新后无法加载更多内容

时间:2019-07-30 09:13:22

标签: java android

我有一个小问题,我可以刷新刷卡并加载更多内容。但是如果我刷刷新,则无法加载更多内容。数据仅显示第一页,之后显示下一页,则无法显示,无法加载更多内容。

我已经调用了clearData,setListeners和requestDataFromServer函数,

那哪里出了问题?

package com.dekikurnia.appcelebrity.profile_list;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.dekikurnia.appcelebrity.R;
import com.dekikurnia.appcelebrity.adapters.ProfileAdapter;
import com.dekikurnia.appcelebrity.models.Profile;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

public class ProfileListFragment extends Fragment implements ProfileListContract.View {

    @BindView(R.id.rvProfile)
    RecyclerView rvProfile;
    @BindView(R.id.progressBarProfile)
    ProgressBar pbLoading;
    @BindView(R.id.fab_scroll)
    FloatingActionButton fabScroll;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    ProfileAdapter adapter;
    LinearLayoutManager linearLayoutManager;
    private ProfileListPresenter profileListPresenter;
    private List<Profile> profileList = new ArrayList<>();

    private String TAG = "ProfileFragment";

    private int pageNo = 1;
    private int previousTotal = 0;
    private boolean loading = true;
    private int totalPages = 3;
    int firstVisibleItem, visibleItemCount, totalItemCount;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getActivity().setTitle("Ruang Casting");
        View view = inflater.inflate(R.layout.fragment_profile, container, false);
        ButterKnife.bind(this, view);

        setView();
        setListeners();
        profileListPresenter = new ProfileListPresenter(this);
        profileListPresenter.requestDataFromServer();

        fabScroll.setOnClickListener(v -> rvProfile.smoothScrollToPosition(0));

        swipeRefresh.setColorSchemeColors(Color.BLUE, Color.GREEN);
        swipeRefresh.setOnRefreshListener(() -> {
            clearData();
            setListeners();
            profileListPresenter.requestDataFromServer();
        });
        return view;
    }

    @SuppressLint("WrongConstant")
    private void setView(){
        adapter = new ProfileAdapter(getContext(), profileList);
        linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        rvProfile.setLayoutManager(linearLayoutManager);
        rvProfile.setItemAnimator(new DefaultItemAnimator());
        rvProfile.setAdapter(adapter);
    }

    private void setListeners() {
        rvProfile.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                visibleItemCount = rvProfile.getChildCount();
                totalItemCount = linearLayoutManager.getItemCount();
                firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
                if (loading) {
                    if (totalItemCount > previousTotal) {
                        loading = false;
                        previousTotal = totalItemCount;
                    }
                }
                if (!loading && (visibleItemCount + firstVisibleItem) >= totalItemCount
                        && firstVisibleItem >= 0
                        && totalItemCount >= totalPages) {
                    profileListPresenter.getMoreData(pageNo);
                    loading = true;
                }

                if (dy > 0 && fabScroll.getVisibility() == View.VISIBLE) {
                    fabScroll.hide();
                } else if (dy < 0 && fabScroll.getVisibility() != View.VISIBLE) {
                    fabScroll.show();
                }
            }
        });
    }

    @Override
    public void showProgress() {
        swipeRefresh.setRefreshing(true);
    }

    @Override
    public void hideProgress() {
        swipeRefresh.setRefreshing(false);
    }

    @Override
    public void setDataToRecyclerView(List<Profile> profileArrayList) {
        profileList.addAll(profileArrayList);
        adapter.notifyDataSetChanged();
        pageNo++;
    }

    @Override
    public void onResponseFailure(Throwable throwable) {
        Log.e(TAG, throwable.getMessage());
        Toast.makeText(getContext(),"Error fetching Profile",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void clearData() {
        int size = profileList.size();
        profileList.clear();
        adapter.notifyItemRangeRemoved(0, size);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        profileListPresenter.onDestroy();
    }
}

1 个答案:

答案 0 :(得分:0)

在以下代码段的setListeners()方法中:

visibleItemCount = rvProfile.getChildCount();
            totalItemCount = linearLayoutManager.getItemCount();
            firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
            if (loading) {
                if (totalItemCount > previousTotal) {
                    loading = false;
                    previousTotal = totalItemCount;
                }
            }
            if (!loading && (visibleItemCount + firstVisibleItem) >= totalItemCount
                    && firstVisibleItem >= 0
                    && totalItemCount >= totalPages) {
                profileListPresenter.getMoreData(pageNo);
                loading = true;
            }

您正在检查if (!loading && (visibleItemCount + firstVisibleItem) >= totalItemCount)

第一次加载更多内容时,条件是正确的,但在刷卡刷新后,这些值需要重新设置为0,以便验证if语句并可以再次调用getMoreData(pageNo)

创建一个名为resetData()的方法,该方法将这些值重置为0,并与其他方法一起调用:

swipeRefresh.setOnRefreshListener(() -> {
        clearData();
        setListeners();
        profileListPresenter.requestDataFromServer();
    });

希望这会有所帮助!