Edit:
I have loading more than 200 datas from webservice.When I am scrolling down the recyclerview, it is not triggering the scrollLisener.
Because, if I'm not used dy>0 condition, it is loading all next 20 datas, 20 datas and so on, initially when coming to this activity.
Below I have posted the code relevant to that.
Logcat:
E/dy: 0
Activity code:
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);
Adapter Code:
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);
}
}
}
When I initially came to this activity, I am getting dy as 0. When I'm scrolling the RecyclerView
, dy is not triggering in LogCat
.
I think because of the NestedScrollView
it is not working.But I need nested scroll view because I need to scroll down some views before RecyclerView
.
If it is lazy loading you want then have a look at my 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();
}
}
}
I think this is easier and fairly inexpensive way to achieve lazy loading.
Try This its works for me....
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
}
}
});
Reference : NestedScrollView
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();
}
and call ->
//NestedScrollView nested
nested.setOnScrollChangeListener(new NestedScroll() {
@Override
public void onScroll() {
//do here
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With