问题描述
我知道标题看起来很奇怪,但是基本上我想要的是将分页应用于列表,并使用url api端点作为参数来控制它!!
I know the title seems weird, but basically what I want is to apply pagination to my list and control it using the url api endpoint as a parameter.!
因此端点看起来像这样,例如> www.someapi.com/josn/list?pageIndex=1
so the endpoint looks like this for example > www.someapi.com/josn/list?pageIndex=1
它将给出一个包含10个项目的数组的响应,如果我想要更多,我必须将参数的索引更改为2,依此类推.!
and it will give a response with array of 10 items, and if I want more I have to change the index of the parameter to 2 and so on.!
响应
{
"Succeeded": true,
"AvailableChallenge": true,
"DebugError": "",
"NextPage": 2,
"AllPages": 2,
"Challenges": [
{
"ID": 114,
"Title": "Amet sit id ratione dolorem numquam"
},
{
"ID": 114,
"Title": "Amet sit id ratione dolorem numquam"
}
...
..
.
]
}
我捆绑了所有内容,但无法正确完成,我已经尝试了 ,没有任何运气.我还尝试使用这两个库 分页 & recyclerview-paginated 甚至是这个 SuperRecyclerView .但这没用.!
I tied everything, but couldn't get it done correctly, I have tried most of the solutions in here and no luck.I've also tried to use these two library Paginate & recyclerview-paginated and even this SuperRecyclerView. but it didn't work.!
任何具有最佳控制逻辑的帮助都将大有帮助.
Any help with the best logic for controlling this will help a lot.
Code for SuperRecyclerView
public class ChallengesListFragment extends BaseFragment {
private int pageNumber = 1;
private SuperRecyclerView recyclerview;
private ArrayList<ChallengeActive> data;
private ChallengesAdapter adapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_challenges_list, container, false);
initView(rootView);
initRecycler();
getData(1);
return rootView;
}
private void initRecycler() {
data = new ArrayList<>();
final RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerview.setLayoutManager(mLayoutManager);
adapter = new ChallengesAdapter(data, getActivity(), this);
recyclerview.setAdapter(adapter);
recyclerview.setupMoreListener(new OnMoreListener() {
@Override
public void onMoreAsked(int overallItemsCount, int itemsBeforeMore, int maxLastVisiblePosition) {
pageNumber++;
getData(maxLastVisiblePosition);
}
}, 10);
recyclerview.setRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
pageNumber = 1;
getData(pageNumber);
}
});
}
private void getData(final int pageNumber) {
WebRequests.GetActiveChallenges(pageNumber, new onResult() {
@Override
public void onSuccess(Object object) {
ArrayList<ChallengeActive> list = (ArrayList<ChallengeActive>) object;
data.clear();
data.addAll(list);
adapter.notifyDataSetChanged();
}
@Override
public void onFail(Object object) {
Toast.makeText(getActivity(), "Something went wrong, try again later",
Toast.LENGTH_SHORT).show();
}
});
}
}
推荐答案
一段时间后,我使用 分页库
After some time, I did it using Paginate Library
代码
public class ChallengesListFragment extends BaseFragment implements OnRefreshListener, Paginate.Callbacks {
// other variables..
private int currentPage = 1;
private boolean hasMore;
private boolean loadingInProgress;
@RequiresApi(api = VERSION_CODES.LOLLIPOP)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_challenges_list, container, false);
initView(rootView);
initToolbar();
initRecycler();
getData();
return rootView;
}
private void initRecycler() {
data = new ArrayList<>();
mLayoutManager = new LinearLayoutManager(getActivity());
recyclerview.setLayoutManager(mLayoutManager);
adapter = new ChallengesAdapter(data, getActivity(), this);
recyclerview.setAdapter(adapter);
Paginate.with(recyclerview, this)
.setLoadingTriggerThreshold(3)
.addLoadingListItem(false)
.build();
}
private void getData() {
swipeRefresh.setRefreshing(true);
WebRequests.GetActiveChallenges(currentPage, new onResult() {
@Override
public void onSuccess(Object object) {
ArrayList<Challenge> list = (ArrayList<Challenge>) object;
data.addAll(list);
adapter.notifyDataSetChanged();
if (list.size() == 10) {
currentPage++;
hasMore = true;
} else {
hasMore = false;
}
loadingInProgress = false;
swipeRefresh.setRefreshing(false);
}
@Override
public void onFail(Object object) {
Toast.makeText(getActivity(), "Something went wrong, try again later",
Toast.LENGTH_SHORT).show();
swipeRefresh.setRefreshing(false);
}
});
}
public ChallengesListFragment() {
}
@Override
public void onRefresh() {
currentPage = 1;
loadingInProgress = true;
getData();
}
@Override
public void onLoadMore() {
loadingInProgress = true;
Handler mHandler = new Handler();
final Runnable hintRunnable = new Runnable() {
@Override
public void run() {
getData();
}
};
mHandler.postDelayed(hintRunnable, 1500);
}
@Override
public boolean isLoading() {
return loadingInProgress;
}
@Override
public boolean hasLoadedAllItems() {
return !hasMore;
}
}
这篇关于如何通过API URL索引使用Recyclerview实现分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!