Android findView of Linearlayout and set background in recycleradapter

First of all, I recommend migrating from findViewById() to viewBindng to prevent app from crashing in running time. Second, You can only access views inside recyclerView’s items from adapter. to implement what you want, you should listen for recyclerView’s adapter change. You can use AdapterDataObserver:

adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            super.onItemRangeRemoved(positionStart, itemCount);
            //...
        }
    });

Take a look at this link in android documentation.

Another way to achieve that will be checking for adapter’s item count everytime you remove an element:

movieList.remove(position);
notifyItemRemoved(position);

if(adapter.getItemCount() == 0)
    linear.setBackgroundResource(R.drawable.visitedbg)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top