Change background of Expandable List child view when an element in child layout is clicked

心不动则不痛 提交于 2019-12-10 23:08:31

问题


I need to change the background of the child view in Expandable list view when child is clicked.

Child row layout is something like:

<RelativeLayout>     //Selector is placed for this layout
    ....
   <RelativeLayout>
      .... 
       <RelativeLayout>
         ....
         <TextView>
        ....
    ....

selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_pressed="true"
        android:drawable= "@drawable/greyish"/>
</selector>

Now, when i clicked on TextView i want to change the background of whole row ( top most Relative layout comprise on every view with in child layout). How can i trigger the selector of top level relative layout.

Also, when OnChildClick() receives callback then row's background changes( because of selector placed in top level layout).

My attempt is:

In TextView's onlclick method:

   ((RelativeLayout)(nameView.getParent().getParent().getParent())).setPressed(true);

but this is not resulting in row layout to change background color.


回答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 !




回答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 (int i = 0; i < layout_with_child_views.getChildCount(); i++) {
   View child = (View) layout_with_child_views 
                .getChildAt(i);
   tintBackground(child);       
}

private void tintBackground(View view) {
    ColorDrawable[] color = { new ColorDrawable(Color.WHITE),
            new ColorDrawable(Color.GREY) };
    TransitionDrawable trans = new TransitionDrawable(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:

    @Override
    public void onChildClick(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 clicked
               for (int i = 0; i < v.getChildCount(); i++) {
                   View child = (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.



来源:https://stackoverflow.com/questions/19001946/change-background-of-expandable-list-child-view-when-an-element-in-child-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!