Search code examples
javaandroidandroid-recyclerviewrecyclerview-layout

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


Solution

  • 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