Anamika Chavan
Anamika Chavan

Reputation: 169

findViewByPosition() of recyclerview shows null after notifydatasetChanged()

I have recyclerview in that i need to color 1st item's button.

so that I use following way,after notifyDataSetChanged()

View viewItem = rvProductList.getLayoutManager().findViewByPosition(0);

but viewItem is getting null.

Is there any way of getting first items position?

Upvotes: 0

Views: 1156

Answers (1)

Truong Giang Dam
Truong Giang Dam

Reputation: 500

Maybe the view at position 0 is recycled at certain position. If you want to test it clearly. Check null viewItem in two case:

  1. Scroll your recycle view up to top, when you can see the 1st item. Then call this and check null

    View viewItem = rvProductList.getLayoutManager().findViewByPosition(0);
    
  2. Scroll down till 1st item being invisible to you, take action like case 1 and check null again.

UPDATE SOLUTION: Solution is set the flag to know when you want to color 1st item. And in onBindViewHolder() method do this

@Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    // I suppose that flag is true then we color 1st item
    if(position == 0 && flag) {
        // color your button here
    } else {
       // ...
    }
}

Hope it help!

Upvotes: 1

Related Questions