0

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?

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Anamika Chavan
  • 149
  • 1
  • 3
  • 14
  • this is because first item is already recycled (not visible) – pskink Jan 04 '18 at 08:13
  • if you want to find the position of the first fully visible item, you can use int firstCompleteleyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition(); Check this SO [answer](https://stackoverflow.com/a/25053500/5392118) – Burak Cakir Jan 04 '18 at 08:47

1 Answers1

1

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!