6

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.

Community
  • 1
  • 1
Steve
  • 9,114
  • 15
  • 82
  • 133
  • this answer would be useful http://stackoverflow.com/questions/38179847/recyclerview-onscrolllistener-not-working-when-setnestedscrollingenabled-to-fals – crgarridos Oct 18 '16 at 08:30
  • @cgarrido thanks for pointed that link to me.Now I have understood, because of nested scroll view it is [not triggering](http://stackoverflow.com/questions/37073376/recyclerview-with-endless-scrolling-in-nestedscrollview-triggers-onscrolled) the scroll listener.But I need nested scroll view to fix the horizontal recyclerview inside vertical recyclerview – Steve Oct 18 '16 at 08:35
  • I'm not sure but if you have to set recyclerView.setNestedScrollingEnabled(**true**); and that will resolve your problem. If there is a parent RecyclerView, it is not aware about this code... – crgarridos Oct 18 '16 at 09:15
  • @cgarrido If I set NestedScrollingEnabled(true) it will slow down scrolling(affect performance speed). – Steve Oct 18 '16 at 09:48
  • @Abbas's answer didn't solve your problem ? – crgarridos Oct 18 '16 at 09:51
  • @cgarrido that also not worked – Steve Oct 18 '16 at 10:08

4 Answers4

6

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.

Abbas
  • 2,875
  • 4
  • 32
  • 57
  • could u please help me in the same type of question http://stackoverflow.com/questions/41930139/staggered-gridview-recycleview-with-load-more-functionality – Ravindra Kushwaha Jan 30 '17 at 14:18
5

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

Rajesh Gauswami
  • 528
  • 6
  • 20
3

Try this it's work for me #NestedScrollView in side RecycleView Pagination

 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 
        }
    });
Shiv Kumar
  • 535
  • 6
  • 11
  • Please explain your code @Shiv Kumar, as I want to call API when last 3rd item of recyclerview gets visible. currently which is not possible because LinearLayoutManager is not giving proper position on findLastVisibleItemPosition. – Raghav Satyadev Jan 22 '18 at 07:54
  • 1
    Here your solution @RaghavSatyadev ..just put it inside onScrolled int totalcount = linearLayoutManager.findFirstVisibleItemPosition() + linearLayoutManager.getChildCount(); if (!data) { if ((linearLayoutManager.getItemCount() - totalcount) == 3) { data = true; Log.d("data", "new data"); //call your method here data = false; } – Shiv Kumar Jan 23 '18 at 10:37
1

1. Change RecycleView view like this

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);

2. Set scroll listener to your NestedScroll like this

  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();
                        }
                    }
                });