0

I am using Recycler view using Grid layout manager. i want to scroll recycle view on button click.(on Button click i am passing position and my item will be highlight).so i want to show highlighted item on top. may be that item is on different position.

onViewCreated where i am initialising-----

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    recycleView = view.findViewById(R.id.recycler_vieww);

    lLayout = new GridLayoutManager(context, 3);

    recycleView.setLayoutManager(lLayout);
   /* ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(context, R.dimen.item_offset);
    recycleView.addItemDecoration(itemDecoration);*/

    String channelId = "";
    if (tvStatus != null && tvStatus.getData() != null && tvStatus.getData().getDetails().get(0).getChannelData() != null) {

        if (tvStatus.getData().getDetails().get(0).getCurrentFeature().equalsIgnoreCase("channel")) {
            //rcAdapter = new Televison_Adapter(context, chnlList,"43", shared, readDb);

            if (tvStatus.getData().getDetails().get(0).getChannelData() != null) {

                channelId = tvStatus.getData().getDetails().get(0).getChannelData().getChannelId();

            }
        }
    }



    rcAdapter = new Televison_Adapter(context, chnlList, channelId, shared, readDb,this);
    rcAdapter.setOnChannelSelectListner(this);
    rcAdapter.setOnSelectedListItemListner(this);

    recycleView.setAdapter(rcAdapter);

    rcAdapter.notifyDataSetChanged();

}

Interface Abstract Method ---from where i want to scroll my recyclerView and position is passing from the adapter class.

@Override
public void getItemPosition(final int position) {



    /*RecyclerView.SmoothScroller smoothScroller = new
            LinearSmoothScroller(context) {
                @Override protected int getVerticalSnapPreference() {
                    return LinearSmoothScroller.SNAP_TO_START;
                }
            };

    smoothScroller.setTargetPosition(position);
    lLayout.startSmoothScroll(smoothScroller);*/

    recycleView.scrollTo(0,1000);

}

1 Answers1

2

You can use scrollTo()

  recyclerView.post(new Runnable() {
        @Override
        public void run() {
            recyclerView.scrollToPosition(adapter.getItemCount() - 1);
            // Here adapter.getItemCount()== child count
        }
    });

Or smoothScrollToPosition().

recyclerView.post(new Runnable() {
        @Override
        public void run() {
            recyclerView.smoothScrollToPosition(adapter.getItemCount() - 1);
        }
    });
Rohit Sharma
  • 1,384
  • 12
  • 19