Khallad shahin
Khallad shahin

Reputation: 61

update position of recyclerview when scrolling (android)

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());

                }
            });

Upvotes: 2

Views: 929

Answers (2)

Muhib Pirani
Muhib Pirani

Reputation: 775

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
            } 
}

Upvotes: 0

Magesh Pandian
Magesh Pandian

Reputation: 9389

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();
        }
    });

Upvotes: 0

Related Questions