2

How can i update position on scroll. right now the position gets updated every time i click on item. i want to update the position every time i scroll. What i wanna do is have a text view which will get updated when scrolling through the items.

recyclerView.addOnItemTouchListener(new GalleryAdapter.RecyclerTouchListener(this, recyclerView, new GalleryAdapter.ClickListener()
            {

                @Override
                public void onClick(View view, int position) {

                    Intent intent = new Intent(mainActivityCarasoul.this, PDFViewerActivity.class);
                    intent.putExtra(PDFViewerActivity.TAG, books.get(position));
                    intent.putExtra("from", "mainActivityCarasoul");
                    startActivity(intent);
                }
                @Override
                public void onLongClick(View view, int position) {
                }
            }));

            recyclerView.addOnScrollListener(new CenterScrollListener());

            recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                   // title.setText(books.get(position).getName());

                }
            });

2 Answers2

0

Hi you can use below code,

 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
    {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy)
        {
            GridLayoutManager layoutManager = ((GridLayoutManager)mRecyclerView.getLayoutManager());
            int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
            int findFirstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
            int findLastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
            int findLastCompletelyVisibleItemPosition = layoutManager.findLastCompletelyVisibleItemPosition();
        }
    });
Magesh Pandian
  • 8,789
  • 12
  • 45
  • 60
  • i am getting this error java.lang.ClassCastException: com.azoft.carousellayoutmanager.CarouselLayoutManager cannot be cast to android.support.v7.widget.GridLayoutManager – Khallad shahin Apr 20 '17 at 04:41
  • i think you setLayoutManager is CarouselLayoutManager , so you can change GridLayoutManager to CarouselLayoutManager in above code. i.e CarouselLayoutManager layoutManager = ((CarouselLayoutManager )mRecyclerView.getLayoutManager()); – Magesh Pandian Apr 20 '17 at 04:47
  • 1
    yeah i tried that but i get error in those 4 lines findFirstVisibleItemPosition(); – Khallad shahin Apr 20 '17 at 04:51
  • i am not sure may be in your carouselayout those api not available, for check you can change as grid – Magesh Pandian Apr 20 '17 at 04:54
0

use this, you will have to maintain a previousPosition in your activity and in your adapter class a selectedItem as int intialize previousPoistion=-1; and selectedPosition=1;

 @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            switch (newState) {
                case 0:
                    int pos = linearLayoutManager1.findLastVisibleItemPosition();
                    yourAdapter.selectedPosition = pos - 1;
                    previousPosition = pos - 1;
                    yourTextView.setText(yourLIst.get(pos-1));
                    break;
            }
        }
    });

and in your onBindView Holder of adapter

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
if (position == selectedPosition) {
             //do what you want when selected
            } 
}
Muhib Pirani
  • 765
  • 1
  • 6
  • 15