3

I saw this question. The answer at this question is workable. BUT... I have to create scrolling to both of sides. This answer has helped me with reaching bottom, but I don't know how to send request when I reach top of the list. I tried to use smth like that:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                if(dy > 0) //check for scroll down
                {

                }
            }
        });

and added this:

if(dy == 0) 
{
           getting some data          
}

but it works wrongly. Why? Because after loading some data after reaching bottom of the list, my list automatically reach the top of the list and data loading again. So I have to add one more condition but I don't imagine which one. Maybe someone can help me?

UPDATE

I tried to add smth like that:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                if (dy > 0) //check for scroll down
                {
                  pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();
                  if (loading) {
                      loading = false;
                   }
                }

                if(pastVisiblesItems==5){
                    loading=true;
                }

                if (loading&&recyclerView.canScrollVertically(-1)){
                    if (prev_url != null) {

                        Log.i("m","[]");
                    }
                }
            }
        });

but this condition:

if (loading&&recyclerView.canScrollVertically(-1)){
                        if (prev_url != null) {

                            Log.i("m","[]");
                        }
                    }

will send ~30 requests and it will be wrong for me :(

Andrew
  • 1,947
  • 2
  • 23
  • 61

2 Answers2

1

Try this solution. Method loadMorePosts(); will be called only if the user can no longer scroll vertically.

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            Boolean reachedBottom = !recyclerView.canScrollVertically(1);
            if (reachedBottom) {
                loadMorePosts();
            }
        }
     });
    })

Create the method loadMorePosts(); where you query the next data. Alternatively you will have to look into the new paging libary. Documentation here.

bensadiku
  • 436
  • 3
  • 15
  • it didn't help me, it sends requests automatically when the list reaches the bottom :) – Andrew Mar 28 '19 at 12:02
  • Yes, thats what its supossed to do. That's the point of endless recycler view – bensadiku Mar 28 '19 at 12:03
  • I'm trying to create smth like at gmail app, but I didn't manage to use paging library so I decided to create it by myself, your solution didn't help me, sorry :( – Andrew Mar 28 '19 at 12:05
  • It would help if i could know more what feature you're trying to implements from Gmail. – bensadiku Mar 28 '19 at 12:08
  • at gmail you can see endless list, but I can't use paging library because I have my own adapter where I can't implement this library – Andrew Mar 28 '19 at 12:10
  • Okay no paging library, but the solution i provided will give you an endless list of items. I'm not sure what is wrong with my solution. You want a endless list. With my solution, the method `loadMorePosts();` will be called every time you reach bottom, therefore creating a endless list. – bensadiku Mar 28 '19 at 12:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190833/discussion-between-andrew-goroshko-and-bensadiku). – Andrew Mar 28 '19 at 12:14
1

You can try inverting conditions for reaching to top in your scroll listener like below :

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            if(dy > 0) //check for scroll down
            {
                visibleItemCount = mLayoutManager.getChildCount();
                totalItemCount = mLayoutManager.getItemCount();
                pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

                if (loading) 
                {
                    if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount) 
                    {
                        loading = false;
                        Log.v("...", "Last Item Wow !");
                        //Do pagination.. i.e. fetch new data
                    }
                }
            }
            if(dy < 0) //check for scroll up
            {
                visibleItemCount = mLayoutManager.getChildCount();
                pastVisiblesItems = mLayoutManager.findLastVisibleItemPosition();

                if (loading) 
                {
                    if ( (pastVisiblesItems - visibleItemCount) <= 0) 
                    {
                        loading = false;
                        Log.v("...", "First Item Wow !");
                        //Do pagination.. i.e. fetch new data
                    }
                }
            }
        }
    });
Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
  • As per you question, you have issue for pagination when user **scroll to top** right? – Jeel Vankhede Mar 28 '19 at 12:46
  • I have a solution for reaching bottom of the list, but not the top of it – Andrew Mar 28 '19 at 12:48
  • Solution i provided full-fills both conditions, when `(dy < 0)` means we're having scroll upward while `if ( (pastVisiblesItems - visibleItemCount) <= 0)` condition determines that user scrolled to at least top. – Jeel Vankhede Mar 28 '19 at 13:04