Recyclerview - Get View at Particular Position

RecyclerView - Get view at particular position

I suppose you are using a LinearLayoutManager to show the list. It has a nice method called findViewByPosition that

Finds the view which represents the given adapter position.

All you need is the adapter position of the item you are interested in.

edit: as noted by Paul Woitaschek in the comments, findViewByPosition is a method of LayoutManager so it would work with all LayoutManagers (i.e. StaggeredGridLayoutManager, etc.)

How to get view from RecyclerView at specific adapter position and get values from those views?

Try this use recyclerView.getLayoutManager().findViewByPosition(int position) to get particular row of recyclerView

View row = recyclerView.getLayoutManager().findViewByPosition(0);
TextView textView = row.findViewById(R.id.YourTextviwID);
textView.setText("Nilu");
ImageView textView = row.findViewById(R.id.YourImageViewID);
imageView.setBackgroundResource(R.mipmap.ic_launcher);

NOTE :- only item that is display in screen when you scroll recyclerView than item will removed from memory

EDIT :- you can write a public method in adapter to get your arraylist in your activity where you can check your flag

How to get particular item's view of RecyclerView?

Here is the easiest method

If you want get ViewHolder of item.

RecyclerView.ViewHolder viewHolder = rvList.getChildViewHolder(rvList.getChildAt(0));

or if you want get View object of item.

View view = rvList.getChildAt(0);

Use the one you need. You can get view or ViewHolder. You can manipulate them as you need.

Edit:

getChildAt method is reliable as i also face issue some time, may be it is not yet fixed.

You can use this code

RecyclerView.ViewHolder holder = (RecyclerView.ViewHolder)
recyclerView.findViewHolderForAdapterPosition(position);
if (null != holder) {
holder.itemView.findViewById(R.id.xyz).setVisibility(View.VISIBLE);
}

Edit 2: Note

This is known issue that if you call findViewHolderForAdapterPosition just after setting list then you get NullPointerException.

if notifyDataSetChanged() has been called but the new layout has not
been calculated yet, this method will return null since the new
positions of views are unknown until the layout is calculated.

link

For solving this you can do like this.

recyclerView.postDelayed(new Runnable() {
@Override
public void run() {
RecyclerView.ViewHolder holder = (RecyclerView.ViewHolder)
recyclerView.findViewHolderForAdapterPosition(position);
if (null != holder) {
holder.itemView.findViewById(R.id.xyz).setVisibility(View.VISIBLE);
}
}
}, 50);

How to get specific position view in RecyclerView?

there's workaround you can try for this,

create a function in your activity

function void updateRecyclerView(position){
yourArrayListForVisibility.set(position,'hide');
mAdapter.notifyDataSetChanged();
}

@Override
public void onResume(){
super.onResume();
mAdapter.notifyDataSetChanged();

}

call this function in your adapter before intent and in your adapter check for this arraylist value to hide or show content, always set default value to 'yourArrayListForVisibility' for every position

How do I get the position selected in a RecyclerView?

Set your onClickListeners on onBindViewHolder() and you can access the position from there. If you set them in your ViewHolder you won't know what position was clicked unless you also pass the position into the ViewHolder

EDIT

As pskink pointed out ViewHolder has a getPosition() so the way you were originally doing it was correct.

When the view is clicked you can use getPosition() in your ViewHolder and it returns the position

Update

getPosition() is now deprecated and replaced with getAdapterPosition()

Update 2020

getAdapterPosition() is now deprecated and replaced with getAbsoluteAdapterPosition() or getBindingAdapterPosition()

Kotlin code:

override fun onBindViewHolder(holder: MyHolder, position: Int) {
// - get element from your dataset at this position
val item = myDataset.get(holder.absoluteAdapterPosition)
}


Related Topics



Leave a reply



Submit