Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have RecyclerView having an ImageView and a TextView for each row layout.In ViewHolder in RecyclerViewAdapter, I have click listener as

 v.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     Images i=list.get(getAdapterPosition());
                     i.setFlag(!i.getFlag());
                     list.set(getAdapterPosition(),(i));
                     notifyDataSetChanged();
                 }
             });

In that click listener I am changing Boolean flag such that it can show either an item is selected or not.Depending on it's value I want to Traverse entire ArrayList by checking

for(int i=0; i<images.size(); i++){
                if(images.get(i).getFlag()){

                    // there I want to get view from LayoutManager for that item
                    //but how to get view for that item and get Bitmap form imageview for that item
                    //end of geting view 
                }
            }

Please help to get view at specific adapter position from Recyclerview Image to reflect my problem is as follows enter image description here

like image 898
Mashhood Avatar asked Dec 15 '17 06:12

Mashhood


People also ask

How do you get the position of the Recycler view?

The RecyclerView. ViewHolder class provides us a method called getAdapterPosition which will return the proper position of anything that currently drawn on the RecyclerView. So, the way I fix the problem I had is by changing the position into viewHolder. getAdapterPosition() .

How do I get the selected item position in the adapter?

You do not have to use any extra method. Just create a global variable named 'position' and initialize it with getAdapterPosition() in any of the major method of the adapter (class or similar). Here is a brief documentation from this link. getAdapterPosition added in version 22.1.

How does recycler view and adapter work?

Adapter: Provides a binding from your app's data set (which is specific to your app) to item views that are displayed within the RecyclerView . The adapter knows how to associate each item-view position in the RecyclerView to a specific location in the data source.


1 Answers

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

like image 90
Goku Avatar answered Oct 07 '22 18:10

Goku