0

How to get getFirstVisiblePosition() and getChildAt(0) in recyclerview? Because I want to get position when user click item.

and code in listview is

index = listview.getFirstVisiblePosition();
View v = listview.getChildAt(0);
top = (v == null) ? 0 : (v.getTop() - listview.getPaddingTop());

but I want to use recyclerview.

How to set it?

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
ARR.s
  • 769
  • 4
  • 20
  • 39

3 Answers3

1

Both are available via the LayoutManager:

layoutManager.findViewByPosition(position);
layoutManager.findFirstVisibleItemPosition();
Yossi Segev
  • 607
  • 5
  • 12
1

if you are using LinearLayoutManager you can use:

int findFirstVisibleItemPosition();

Orientos
  • 151
  • 3
  • 15
0

you can use getAdapterPosition() in you view holder. for example:

private class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

       public myViewHolder(View view) {
           super(view);
             view.setOnclickListener(this);
       }

 @Override
public void onClick(View view) {

       int clickedItemPosition = view.getAdapterPosition();

}
   }

Android Recycler View Documentation

How do I get the position selected in a RecyclerView?

Irfan Ul Haq
  • 1,065
  • 1
  • 11
  • 19