Home > Blockchain >  my list (Recyclerview) displays on multiple screens want to change the background colour of list ite
my list (Recyclerview) displays on multiple screens want to change the background colour of list ite

Time:12-24

This is my viewholderClass which displays the list on multiple screen.

 class TagViewHolder(itemView: View, listener: RecyclerViewItemClickListener?,isTraining: Boolean) : BaseViewHolder<AdapterTag>(itemView, listener) {
        override fun onBindView(context: Context, data: AdapterTag ) {
            itemView.tag_icon.setImageResource(data.getIconResId())
            itemView.tag_name.text = data.getTitle()
            itemView.tag_count.text = "[${data.getItemCount()}]"
//background color change of selected item in list
            itemView.isSelected = (data.getItemCount() == 0)
        }
    }

this is my drawable class which implements in below drawable class for background colour change.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="@dimen/card_radius" />
            <padding android:bottom="@dimen/card_vertical_padding"
                android:left="@dimen/card_horizontal_padding"
                android:right="@dimen/card_horizontal_padding"
                android:top="@dimen/card_vertical_padding" />
            <solid android:color="@color/cardBackgroundWithZeroItem"/>
        </shape>
    </item>
</selector>

this is my list background class which changes the background colour in selected_state.

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:drawable="@drawable/training_tag_card_state"/>
        <item>
            <shape android:shape="rectangle">
                <corners android:radius="@dimen/card_radius" />
                <padding android:bottom="@dimen/card_vertical_padding"
                    android:left="@dimen/card_horizontal_padding"
                    android:right="@dimen/card_horizontal_padding"
                    android:top="@dimen/card_vertical_padding" />
                <solid android:color="@color/cardBackground"/>
            </shape>
        </item>
    </selector>

My recycler view list displays on multiple screen but i have to change the background color of list items only on particular screen but in the tagViewHolder it changes background color of list items on everyscreen how to use the isTraining:Boolean(it is for my screen i want to change background color of list items) such that i can use background color on desired screen of list items.what should i change in implemetations to get the solution of above problem.

CodePudding user response:

Pass any value to the adapter from the activity. Based on that Boolean value you have changed the background.

CodePudding user response:

You need to pass isTraining Boolean value through adapter

For ex : You have 2 Activities

A and B , You want change the List Item Background in A Activity only for that u need to pass the isTraining value True

   if (isTraining) {
        itemView.parentView.background =
            ContextCompact.getDrawable(itemView.parentView.context, R.drawable.resourceid)
    } else {
        //Default value
        ContextCompact.getDrawable(itemView.parentView.context, R.drawable.resourceid)
    }
  • Related