Java 在刷卡刷新后加载更多不工作的内容
我有一个小问题,我使刷卡刷新和加载更多。但是如果我刷刷新,加载更多就不起作用了。数据只显示第一页,之后的下一页不会显示,加载更多内容也不起作用 我调用了clearData、setListeners和requestDataFromServer函数 那故障在哪里呢Java 在刷卡刷新后加载更多不工作的内容,java,android,Java,Android,我有一个小问题,我使刷卡刷新和加载更多。但是如果我刷刷新,加载更多就不起作用了。数据只显示第一页,之后的下一页不会显示,加载更多内容也不起作用 我调用了clearData、setListeners和requestDataFromServer函数 那故障在哪里呢 package com.dekikurnia.appcelebrity.profile_list; import android.annotation.SuppressLint; import android.graphics.Colo
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();
}
}
package com.dekikurnia.app名人录\u列表;
导入android.annotation.SuppressLint;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ProgressBar;
导入android.widget.Toast;
导入androidx.annotation.NonNull;
导入androidx.annotation.Nullable;
导入androidx.fragment.app.fragment;
导入androidx.recyclerview.widget.DefaultItemAnimator;
导入androidx.recyclerview.widget.LinearLayoutManager;
导入androidx.recyclerview.widget.recyclerview;
导入androidx.swiperefreshlayout.widget.swiperefreshlayout;
导入butterknife.BindView;
进口毛刀,毛刀;
import com.dekikurnia.apps.R;
导入com.dekikurnia.app名人录.adapters.ProfileAdapter;
导入com.dekikurnia.app名人录.models.Profile;
导入com.google.android.material.floatingactionbutton.floatingactionbutton;
导入java.util.ArrayList;
导入java.util.List;
公共类ProfileListFragment扩展片段实现ProfileListContract.View{
@BindView(R.id.rvProfile)
RecyclerView rvProfile;
@BindView(R.id.progressBarProfile)
进度条加载;
@BindView(R.id.fab_滚动)
浮动操作按钮滚动;
@BindView(R.id.swipe\u刷新)
swipeRefresh布局swipeRefresh;
外形适配器;
直线布局经理直线布局经理;
私有ProfileListPresenter ProfileListPresenter;
private List profileList=new ArrayList();
私有字符串TAG=“ProfileFragment”;
专用int pageNo=1;
private int previousTotal=0;
私有布尔加载=真;
私有整数总页数=3;
int firstVisibleItem、visibleItemCount、totalItemCount;
@可空
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
getActivity().setTitle(“Ruang Casting”);
视图=充气机。充气(R.layout.fragment_外形,容器,假);
ButterKnife.bind(这个,视图);
setView();
setListeners();
profileListPresenter=新的profileListPresenter(此);
profileListPresenter.requestDataFromServer();
fabScroll.setOnClickListener(v->rvProfile.smoothScrollToPosition(0));
swipeRefresh.setColorSchemeColors(Color.BLUE,Color.GREEN);
swipeRefresh.setOnRefreshListener(()->{
clearData();
setListeners();
profileListPresenter.requestDataFromServer();
});
返回视图;
}
@SuppressLint(“错误常量”)
私有void setView(){
adapter=newprofileadapter(getContext(),profileList);
linearLayoutManager=新的linearLayoutManager(getContext(),linearLayoutManager.VERTICAL,false);
rvProfile.setLayoutManager(linearLayoutManager);
setItemAnimator(新的DefaultItemAnimator());
rvProfile.setAdapter(适配器);
}
私有void setListeners(){
rvProfile.addOnScrollListener(新的RecyclerView.OnScrollListener(){
@凌驾
已克隆的公共void(@NonNull RecyclerView RecyclerView,int-dx,int-dy){
super.onScrolled(recyclerView、dx、dy);
visibleItemCount=rvProfile.getChildCount();
totalItemCount=linearLayoutManager.getItemCount();
firstVisibleItem=linearLayoutManager.findFirstVisibleItemPosition();
如果(装载){
如果(totalItemCount>previousTotal){
加载=假;
previousTotal=totalItemCount;
}
}
如果(!loading&&(visibleItemCount+firstVisibleItem)>=totalItemCount
&&firstVisibleItem>=0
&&totalItemCount>=totalPages){
profileListPresenter.getMoreData(页码);
加载=真;
}
if(dy>0&&fabScroll.getVisibility()==View.VISIBLE){
fabcoll.hide();
}else if(dy<0&&fabScroll.getVisibility()!=View.VISIBLE){
show();
}
}
});
}
@凌驾
公共服务进展{
swipeRefresh.setRefreshing(true);
}
@凌驾
public void hideProgress(){
swipeRefresh.setRefreshing(false);
}
@凌驾
public void setDataToRecyclerView(列表配置文件ArrayList){
profileList.addAll(profileArrayList);
adapter.notifyDataSetChanged();
pageNo++;
}
@凌驾
公共责任无效(可丢弃可丢弃){
Log.e(标记,throwable.getMessage());
Toast.makeText(getContext(),“获取配置文件时出错”,Toast.LENGTH_SHORT.show();
}
@凌驾
public void clearData(){
int size=profileList.size();
profileList.clear();
适配器。NotifyItemRange已删除(0,大小);
}
@凌驾
公共空间{
super.ondestory();
profileListPresenter.onDestroy();
}
}
在以下代码段中的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;
}
您正在检查(!loading&&(visibleItemCount+firstVisibleItem)>=totalItemCount)
当您第一次加载更多时,条件是正确的,但在您刷卡刷新后,这些值需要设置回0,以便验证if语句,并且可以再次调用getMoreData(pageNo)
<
swipeRefresh.setOnRefreshListener(() -> {
clearData();
setListeners();
profileListPresenter.requestDataFromServer();
});