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 te
Solution 1:
Hi you can use below code,
mRecyclerView.addOnScrollListener(newRecyclerView.OnScrollListener()
{
@OverridepublicvoidonScrolled(RecyclerView recyclerView, int dx, int dy)
{
GridLayoutManagerlayoutManager= ((GridLayoutManager)mRecyclerView.getLayoutManager());
intfirstVisiblePosition= layoutManager.findFirstVisibleItemPosition();
intfindFirstCompletelyVisibleItemPosition= layoutManager.findFirstCompletelyVisibleItemPosition();
intfindLastVisibleItemPosition= layoutManager.findLastVisibleItemPosition();
intfindLastCompletelyVisibleItemPosition= layoutManager.findLastCompletelyVisibleItemPosition();
}
});
Solution 2:
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;
@OverridepublicvoidonScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
switch (newState) {
case0:
intpos= linearLayoutManager1.findLastVisibleItemPosition();
yourAdapter.selectedPosition = pos - 1;
previousPosition = pos - 1;
yourTextView.setText(yourLIst.get(pos-1));
break;
}
}
});
and in your onBindView Holder of adapter
@OverridepublicvoidonBindViewHolder(final RecyclerView.ViewHolder holder, finalint position) {
if (position == selectedPosition) {
//do what you want when selected
}
}
Post a Comment for "Update Position Of Recyclerview When Scrolling (android)"