4

Actually I wanna get view of particular item in RecyclerView in my fragment.class. For this purpose I tried to set a getter in my adapter class then tried to access it in my fragment but I'm unable to access the views .

Code of Adapter Class :

private List<ViewHolder> holder_list=new ArrayList<>();
 @Override
public void onBindViewHolder(ViewHolder holder, int position) {

    holder_list.add(holder);
   }
public ViewHolder getViewHolder(int position){
    return holder_list.get(position);
}

Fragment Code:

MessageAdapter.ViewHolder holder= msgAdp.getViewHolder(msgAdp.getItemCount()-1);
    //Here holder.mMessageView is a text view
Toast.makeText(ctx,holder.mMessageView.getText().toString(),Toast.LENGTH_SHORT).show();
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
Srinivas Nahak
  • 1,846
  • 4
  • 20
  • 45

2 Answers2

9

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);
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • Sir thanks a lot for your answer but I'm still unable to access the view by your method . I'm getting nullPointerException – Srinivas Nahak Apr 15 '18 at 17:12
  • Which method are you using and paste your exception so that i can tell what's wrong with that. – Khemraj Sharma Apr 15 '18 at 17:14
  • **View view = rView.getChildAt(0); RelativeLayout rl=view.findViewById(R.id.socListContainer); rl.setVisibility(View.GONE);** the error is pointed at the *rl* line . Here rl is the container – Srinivas Nahak Apr 15 '18 at 17:19
  • If you are getting nullpointer exception then you should check again your view Ids and your list that is that position present. Can you tell me what is being null in your code? – Khemraj Sharma Apr 15 '18 at 17:40
  • **holder.itemView.findViewById(R.id.xyz).setVisibility(View.VISIBLE);** this line is being pointed out as nullpointerexception and by the way ids are correct I've checked them twice – Srinivas Nahak Apr 15 '18 at 17:43
  • 1
    Welcome dude, its my passion – Khemraj Sharma Apr 15 '18 at 17:52
0

After many attempts, I found a solution, which is very simple and short very simple line code recyclerView.scrollToPosition(your position);

EYAD SHREAM
  • 21
  • 1
  • 5