Skip to main content

How to access the each view of item by position in Recycler View in android ?

How to access the each view of item by position in Recycler View in android ?


There are some methods to access the view of each item view to change or update the view during the run time. To access the view of the item view ,

consider the Recycler view as below,


RecyclerView mainRecyclerView = (RecyclerView)view.findViewById(R.id.main_recycler_view);

RecyclerAdapter adapter = new RecyclerAdapter(mContext);

mainRecyclerView.setAdapter(adapter);

main.setLayoutManager(new LinearLayoutManager(mContext));

 To access the itemView of each position the following functions can be used,

 1. View itemView = mainRecyclerView.getChildAt(positionOfItem);

 2. View itemView = mainRecyclerView.findViewHolderForAdapterPosition(Position).itemView;

 3. View itemView = mainRecyclerView.findViewHolderForPosition(Position).itemView;

 4. Long itemId = mainRecyclerView.getAdapter().getItemId(position);  

    View itemView = mainRecyclerView.findViewHolderForItemId(itemId);

 5. View itemView = mainRecyclerView.getLayoutManager().findViewByPosition(Position);

 6. View itemView = mainRecyclerView.getLayoutManager().getChildAt(position);


After getting the rootView of each item using above methods you can access each element inside the view using the ,


itemView.findViewById();

and you can do as normal to update the view at runtime.

But one of the problem is that if you guys all the above methods normally from the oncreate or createView or any method of fragment or activity it will throw you the null pointer exception as.

android.View.View.findViewById() java.lang.null null pointer exception.

Always these methods should be called from any of the event listeners like onTouchListener or onClickListener or onScrollListener etc as per the requirement.

But if the requirement is like you need to call simply without any event listener then, use the methods as shown below,

mainRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
       View itemView = mainRecyclerView.getChildAt(positionOfItem);
        mainRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}



Cool !! The issue is solved for those are thirsty for this...!! Keep reading the blog for lots of solutions..!!


Incoming Keywords:
findviewholderforitemid null, findviewholderforitemid, findviewholderforadapterposition null, findviewholderforlayoutposition, findviewholderforposition
recyclerview getchildat null, recyclerview getchildat returns nullfindviewholderforadapterposition returns nullandroid recyclerview get viewholder at position



Comments

Post a Comment

Popular posts from this blog

A.P.I call or async await not working in Array forEach ?

H ello Readers, Welcome back. You would have wondered why does forEach function on the array does not wait for any asynchronous function even if we provide the async await in the node js. If you are the person, wondered about this ever, this post is right for you. Non working example : Lets consider the below snippet, this will not wait for the asynchronous process to wait. I am making a setTimeout to mock the API call async process. This will result the count as, Output : count = 0 OMG !! Why it doesn't work ? Answer probably might be lying inside the Array prototype from JavaScript. Lets take a look inside the "Array.prototype.forEach" function. From the snippet its clear that, the for loop in which they call the callback function does not wait for the asynchronous process to run in the callback. So this forEach is not build for asynchronous process itself. So can't I use forEach any more for running a asynchronous function ? Answer g