7

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

Mashhood Qadeer
  • 482
  • 8
  • 19

1 Answers1

11

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

Goku
  • 9,102
  • 8
  • 50
  • 81
  • Sir !You are great . It is working .But Sir why sometimes it return null view when we use View row = recyclerView.getLayoutManager().findViewByPosition(2); – Mashhood Qadeer Dec 15 '17 at 06:41
  • @MashhoodQadeerBhatti because of recyclerView the recyclerView hold only item that is display in screen when you scroll recyclerView than item will removed from memory – Goku Dec 15 '17 at 06:43
  • Sir ! I want your opinion for my work that If I want to get bitmap from All ImageViews in recyclerView where flag was true at the end will it work or not. – Mashhood Qadeer Dec 15 '17 at 06:47
  • @MashhoodQadeerBhatti you can write a public method in adapter to get your arraylist in your activity where you can check your flag let me know if any more help required – Goku Dec 15 '17 at 06:49
  • I have used a public method and following code to get text of TextViews that were selected try{ for(int i=0; i – Mashhood Qadeer Dec 15 '17 at 06:55
  • But for Adapter position when it is not 2 As Sir! Prem has suggested that it only gives that are in view and other are null.Therefore I think can we disable RecyclerView to recycle view – Mashhood Qadeer Dec 15 '17 at 06:57
  • @MashhoodQadeerBhatti didn't get u – Goku Dec 15 '17 at 07:02
  • Sir ! I want recyclerView to stop recycling the views and If it stop to recycle the views then it means it will never give null view So can we get it – Mashhood Qadeer Dec 15 '17 at 07:15
  • you can not **` recyclerView to stop recycling the views`** – Goku Dec 15 '17 at 07:17
  • Then why that method is given holder.setIsRecyclable(false); – Mashhood Qadeer Dec 15 '17 at 07:18
  • @MashhoodQadeerBhatti better to use this https://stackoverflow.com/questions/47826949/how-to-get-view-from-recyclerview-at-specific-adapter-position-and-get-values-fr/47827026?noredirect=1#comment82616566_47827026 – Goku Dec 15 '17 at 07:21
  • 1
    Sir Thanks Thanks Thanks ....Thanks a lot. You have solved my problem how to get view from RecyclerView at a specific position. – Mashhood Qadeer Dec 15 '17 at 07:25
  • @MashhoodQadeerBhatti happy to help you – Goku Dec 15 '17 at 07:27
  • if you dont want to use a RecyclerView , just use a ListView, it will not recycle the views – Gastón Saillén Sep 11 '18 at 20:13