ShamylZakariya / StickyHeaders

Adapter and LayoutManager for Android RecyclerView which enables sticky header positioning.
MIT License
1.4k stars 185 forks source link

smooth scroll to a perticular section #73

Open jayattma opened 6 years ago

jayattma commented 6 years ago

how to do smooth scroll to a perticular section when click on a button.

ManBali commented 6 years ago

@jayattma Did U find the way ? i really want to this method

rkpatel097 commented 6 years ago
  RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(binding.rvVouche .getContext
                        ()) {
            @Override
            protected int getVerticalSnapPreference() {
                return LinearSmoothScroller.SNAP_TO_START;
            }

            @Override
            protected int calculateTimeForScrolling(int dx) {
                float MILLISECONDS_PER_PX = calculateSpeedPerPixel(getContext()
                        .getResources()
                        .getDisplayMetrics());
                return (int) Math.ceil(Math.abs(dx) * MILLISECONDS_PER_PX) + 150;
            }
        };
        smoothScroller.setTargetPosition(voucherAdapter.getAdapterPositionForSectionHeader(position));
        binding.rvVoucher.getLayoutManager().startSmoothScroll(smoothScroller);

Try This ...it's work perfect for me

rkpatel097 commented 6 years ago

MILLISECONDS_PER_PX = calculateSpeedPerPixel(getContext() .getResources() .getDisplayMetrics());

In this line smoothScroller.setTargetPosition(voucherAdapter.getAdapterPositionForSectionHeader(position));

position must be getSection which you get from HeaderViewHolder

sagarpatel288 commented 5 years ago

getAdapterPositionForSectionHeader(int passYourPosition) is the method already there.