0

Basically I want my recyclerview to automatically scroll to a position where the item is not half shown. Like the one in googleplay.

I have written a code

public void scrollToVisible(){
    int firstVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
    View view = recyclerView.getLayoutManager().getChildAt(0);
    if (firstVisibleItemPosition > 0 && view != null) {
        int offsetTop = view.getTop();
        if (firstVisibleItemPosition - 1 >= 0 && adapter.getItemCount() > 0) {
            ((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(firstVisibleItemPosition - 1, offsetTop);
        }
    }
}

The problem comes next. I dont know where to put this code. I have a vague idea to put it when the recyclerview stops on scrolling but I've been searching for quite some time now and i cant find such a method. when i put it on the onScroll some unexpected behavior comes out

Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
Gilbert Mendoza
  • 405
  • 1
  • 7
  • 16

4 Answers4

7

You may create a CustomRecyclerView extending RecyclerView

public class CustomRecyclerView extends RecyclerView {
@Override
public void onScrollStateChanged(int state) {
    super.onScrollStateChanged(state);

    // check if scrolling has stopped
    if (state == SCROLL_STATE_IDLE) {
         LinearLayoutManager linearLayoutManager = (LinearLayoutManager) getLayoutManager();
         // use code here
    }
}
  • this shows up when i use a customrecyclerview android.view.InflateException: Binary XML file line #8: Error inflating class gaming.kg.passingvehiclecounter.CustomRecyclerView – Gilbert Mendoza Dec 10 '16 at 11:29
  • ok i finally made it work thanks. only problem now is the smoothscrolling but thats a different question so ill mark your answer as correct – Gilbert Mendoza Dec 10 '16 at 11:42
1

If it maybe of any help to someone:

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                    super.onScrolled(recyclerView, dx, dy);

                        Log.d("y value",String.valueOf(dy));
                    if (dy > 0) {                       

                       //scrolling up

                    } else {
// Scrolling down
                    }
                }

                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);

                    if (newState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                        // Do something
                        Log.e("SCROLL_STATE_FLING","SCROLL_STATE_FLING");
                    } else if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                        Log.e("SCROLLTOUCH_SCROLL","SCROLL_STATE_TOUCH_SCROLL");
                        //slideUp(party_view);
                        // Do something
                    } else if (newState==AbsListView.OnScrollListener.SCROLL_STATE_IDLE){
                        // Do something
                        //slideDown(party_view);
                        Log.e("SCROLL_STATE_IDLE","SCROLL_STATE_IDLE");
                    }
                }
            });
nimi0112
  • 2,065
  • 1
  • 18
  • 32
0

complete example with UI synchronization

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            Handler handler = new Handler(getMainLooper());

            if (newState == 0) {
                handler.removeCallbacks(MainActivity.this::hideFab);
                handler.postDelayed(MainActivity.this::showFab, 400);
            } else {
                handler.removeCallbacks(MainActivity.this::showFab);
                handler.postDelayed(MainActivity.this::hideFab, 100);
            }

        }
    });


private void hideFab() {
    addFile.hide();
    addText.hide();
    camera.hide();
}



private void showFab() {
    addFile.show();
    addText.show();
    camera.show();
}
android X
  • 1
  • 1
0

newState == 0 means The RecyclerView is not currently scrolling.

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == 0){
                    int center = recyclerView.getWidth() / 2;
                    View centerView = recyclerView.findChildViewUnder(center, recyclerView.getTop());
                    int centerPos = recyclerView.getChildAdapterPosition(centerView);
                   recyclerView.smoothScrollToPosition(centerPos);
                }
            }
        });
yago
  • 59
  • 4