Skip to content Skip to sidebar Skip to footer

Change Background Of Expandable List Child View When An Element In Child Layout Is Clicked

I need to change the background of the child view in Expandable list view when child is clicked. Child row layout is something like: //Selector is placed

Solution 1:

Issue:

ultimately you want to trigger the selector of parentView from its childView.

Solution

In your parent layout,add this line:

android:addStatesFromChildren="true"

and if you have the reference of parentView in your java code then you can achieve the task by using:

parentView.setAddStatesFromChildren(true);

where,parentView is your parent layout i.e.:RelativeLayout

Note:

make sure that your childview is not duplicating parent's state. i.e.: android:duplicateParentState or childView.setDuplicateParentState(true)

I hope it will be helpful !

Solution 2:

Could you not just give an id to the layout where you want the elements to be colored and then something like:

layout_with_child_views = (RelativeLayout)nameView.getRootView().findViewById(id)

If your method returns the correct view then you could of course also stick to that.

Then fetch all child elements and change the background color:

for (inti=0; i < layout_with_child_views.getChildCount(); i++) {
   Viewchild= (View) layout_with_child_views 
                .getChildAt(i);
   tintBackground(child);       
}

privatevoidtintBackground(View view) {
    ColorDrawable[] color = { newColorDrawable(Color.WHITE),
            newColorDrawable(Color.GREY) };
    TransitionDrawabletrans=newTransitionDrawable(color);
    view.setBackgroundDrawable(trans);
    trans.startTransition(500);

}

Edit: I have been searching abit and to me it seems like you should be able to use the available listeners of ExpandableListView, so that in your ExpandableListAdapter:

@OverridepublicvoidonChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        // use the parent to set default color to all other entries// v is the view within the expandable list/ListView that was clickedfor (inti=0; i < v.getChildCount(); i++) {
                   Viewchild= (View) v.getChildAt(i);
                   // do highlighting       
               }
    }

I have not tried this myself and am currently unable to setup a test project. Just wanted to give you an alternative approach in case you have not tried something similar.

Post a Comment for "Change Background Of Expandable List Child View When An Element In Child Layout Is Clicked"