recyclerview带有自己的滚动侦听器,具有以下方法:
void onScrollStateChanged(RecyclerView recyclerView, int newState)
当RecyclerView的滚动状态发生变化时要调用的回调方法。
void onScrolled(RecyclerView recyclerView, int dx, int dy)
滚动RecyclerView时要调用的回调方法。
当滚动到达列表末尾时,是否有任何方法可以触发加载程序加载更多数据?
我已经实现了这种方式:
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
GenerItem generItem=generItems.get(i);
Log.d("TAG","position "+i);
if(i==generItems.size()-1)
((GenerSearchActivity)mContext).onScroll(i);
viewHolder.bindValues(generItem);
}
这里onScroll()in activity将触发加载器加载更多数据。请建议最好的方法是什么。
答案 0 :(得分:1)
基本上有3个步骤。
但是首先,我们需要一个Callback,它将作为RecyclerView.Adapter和Activity之间的桥梁
public interface PaginationCallBack{
public void oadNextPage();
}
在您的活动中实施此回调
class YourActivity extends AppCompatActivity implements PaginationCallBack{
int pageNum = 1;
@Override
public void loadNextPage(){
// Your implementation
}
}
在RecyclerView.Adapter中初始化回调
class YourAdapter extends RecyclerView.Adapter{
private PaginationCallBack paginationCallBack;
public YourAdapter(PaginationCallBack paginationCallBack) {
this.paginationCallBack = paginationCallBack;
}
}
步骤1
在onBindViewHolder
方法中添加条件并通过回调进行通知。
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewholder, int position) {
if(position+1==images.size()){
paginationCallBack.loadNextPage(); // Callback
}
}
第2步:呼叫下一页
getImages(++ pageNum)//您的REST方法的页码
@Override
public void loadNextPage(){
getImages(++pageNumber) // REST call with next Page
}
第3步将结果添加到旧列表中,并通知datasetChanged
public void getImages(int pageNum){
List<Images> newResults = //RESTCALL
imageList.addAll(newResults);
adapter.updateDataSet(imageList)
}
updateDataSet(imageList)
方法在哪里?将此方法写入RecyclerView.Adapter
public void updateDataSet(List<GalleryMedia> newImages){
if(newImages!=null){
images = newImages;
}
notifyDataSetChanged();
}
答案 1 :(得分:0)
Here's a scroll listener which implements load more and quick return pattern:
public abstract class HidingScrollListener extends RecyclerView.OnScrollListener {
private static final float HIDE_THRESHOLD = 10;
private static final float SHOW_THRESHOLD = 70;
private int mToolbarOffset = 0;
private boolean mControlsVisible = true;
private int mToolbarHeight;
private int mTotalScrolledDistance;
private int previousTotal = 0;
private boolean loading = true;
private int visibleThreshold = 4;
int firstVisibleItem, visibleItemCount, totalItemCount;
private LinearLayoutManager layoutManager;
public HidingScrollListener(Context context, LinearLayoutManager layoutManager) {
mToolbarHeight = Tools.getFooterHeight(context);
this.layoutManager = layoutManager;
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
if (mTotalScrolledDistance < mToolbarHeight) {
setVisible();
}
else {
if (mControlsVisible) {
if (mToolbarOffset > HIDE_THRESHOLD) {
setInvisible();
}
else {
setVisible();
}
}
else {
if ((mToolbarHeight - mToolbarOffset) > SHOW_THRESHOLD) {
setVisible();
}
else {
setInvisible();
}
}
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
clipToolbarOffset();
onMoved(mToolbarOffset);
if ((mToolbarOffset < mToolbarHeight && dy > 0) || (mToolbarOffset > 0 && dy < 0)) {
mToolbarOffset += dy;
}
if (mTotalScrolledDistance < 0) {
mTotalScrolledDistance = 0;
}
else {
mTotalScrolledDistance += dy;
}
// for load more
visibleItemCount = recyclerView.getChildCount();
totalItemCount = layoutManager.getItemCount();
firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
loading = true;
onLoadMore();
}
}
private void clipToolbarOffset() {
if (mToolbarOffset > mToolbarHeight) {
mToolbarOffset = mToolbarHeight;
}
else if (mToolbarOffset < 0) {
mToolbarOffset = 0;
}
}
private void setVisible() {
if (mToolbarOffset > 0) {
onShow();
mToolbarOffset = 0;
}
mControlsVisible = true;
}
private void setInvisible() {
if (mToolbarOffset < mToolbarHeight) {
onHide();
mToolbarOffset = mToolbarHeight;
}
mControlsVisible = false;
}
public abstract void onMoved(int distance);
public abstract void onShow();
public abstract void onHide();
public abstract void onLoadMore();
}
And it's implementation is below:
HidingScrollListener scrollListener = new HidingScrollListener(activity, manager) {
@Override
public void onMoved(int distance) {
}
@Override
public void onShow() {
}
@Override
public void onHide() {
}
@Override
public void onLoadMore() {
// you can do your pagination here.
}
};
mRecyclerView.addOnScrollListener(scrollListener);