修改:
我从webservice加载了200多个数据。当我向下滚动recyclerview时,它没有触发scrollLisener。
因为,如果我没有使用dy> 0条件,它会加载所有接下来的20个数据,20个数据,依此类推,最初是在进行此活动时。
下面我发布了相关的代码。
logcat的:
E/dy: 0
活动代码:
recyclerView = (RecyclerView) findViewById(R.id.rv_list_tab_home_recycler);
recyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
//homePostitemsAdapter = new UserPostAdapter(TabHomeActivity.this, homePostItemsList);
homePostitemsAdapter = new TabHomeAdapter(homePostItemsList, recyclerView);
// recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(homePostitemsAdapter);
recyclerView.setNestedScrollingEnabled(false);
适配器代码:
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;
public TabHomeAdapter(List<HomePostItems> objects, RecyclerView recycle) {
homePostArrListItems = objects;
if (recycle.getLayoutManager() instanceof LinearLayoutManager) {
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recycle
.getLayoutManager();
Log.e("LinearLayoutManager", "Test");
recycle.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView,
int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager
.findLastVisibleItemPosition();
Log.e("dy", ""+dy);
if (!loading
&& totalItemCount <= (lastVisibleItem + visibleThreshold ) && dy > 0) {
// End has been reached
// Do something
Log.e("totalItemCount", ""+totalItemCount);
Log.e("lastVisibleItem", ""+lastVisibleItem);
Log.e("visibleThreshold", ""+visibleThreshold);
Log.e("loading", ""+loading);
Log.e("onLoadMoreListener", ""+onLoadMoreListener);
if (onLoadMoreListener != null) {;
onLoadMoreListener.onLoadMore();
}
loading = true;
}
}
});
}
public void setLoaded() {
loading = false;
}
@Override
public int getItemCount() {
return homePostArrListItems.size();
}
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}
public static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public ProgressViewHolder(View v) {
super(v);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
}
}
}
当我最初参加此活动时,我的dy为0.当我滚动RecyclerView
时,dy未在LogCat
中触发。
我认为因为NestedScrollView
它是not working。但我需要嵌套滚动视图,因为我需要在RecyclerView
之前向下滚动一些视图。
答案 0 :(得分:6)
如果是延迟加载,则需要查看我的RecyclerAdapter
@Override
public void onBindViewHolder(VH viewHolder, final int position) {
// Set Data to Views
if(position == count) {
// When last item is reached.
if (onLoadMoreListener != null) {;
onLoadMoreListener.onLoadMore();
}
}
}
我认为这是实现延迟加载的更简单且相当便宜的方法。
答案 1 :(得分:3)
试试这对我有用......
nestedScrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
if(v.getChildAt(v.getChildCount() - 1) != null) {
if ((scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) &&
scrollY > oldScrollY) {
//code to fetch more data for endless scrolling
}
}
});
答案 2 :(得分:2)
public abstract class NestedScroll implements NestedScrollView.OnScrollChangeListener {
public void onScrollChange(NestedScrollView view, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (view.getChildAt(view.getChildCount() - 1) != null) {
if ((scrollY >= (view.getChildAt(view.getChildCount() - 1).getMeasuredHeight() - view.getMeasuredHeight())) &&
scrollY > oldScrollY) {
onScroll();
}
}
}
public abstract void onScroll();
}
并致电 - &gt;
//NestedScrollView nested
nested.setOnScrollChangeListener(new NestedScroll() {
@Override
public void onScroll() {
//do here
}
});
答案 3 :(得分:0)
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
NestedScrollView nestedScrollView = NestedScrollView)findViewById(R.id.nestedscroll);
nestesdScroll.getViewTreeObserver().addOnScrollChangedListener(new
ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
View view = (View) nestesdScroll.getChildAt(nestesdScroll.getChildCount() - 1);
int diff = (view.getBottom() - (nestesdScroll.getHeight() + nestesdScroll.getScrollY()));
if (diff == 0) {
//code to fetch more data for endless scrolling
load_more_data();
}
}
});