更改扩展列表子视图的背景被点击子布局的元素时 [英] 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.
子行布局是这样的:
<RelativeLayout> //Selector is placed for this layout
....
<RelativeLayout>
....
<RelativeLayout>
....
<TextView>
....
....
选择:
<?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>
现在,当我在TextView的点击我要改变整行的背景(最上面的相对布局包括在每一个视图与子布局)。我怎样才能触发顶级相对布局的选择。
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.
此外,当OnChildClick()接收回调再行的背景变化(因为选择器放置在顶层布局)。
Also, when OnChildClick() receives callback then row's background changes( because of selector placed in top level layout).
我的尝试是:
在TextView中的onlclick方式:
In TextView's onlclick method:
((RelativeLayout)(nameView.getParent().getParent().getParent())).setPressed(true);
但这不是导致行布局来改变背景颜色。
but this is not resulting in row layout to change background color.
推荐答案
问题:
最终要从其childView触发parentView的选择。
ultimately you want to trigger the selector of parentView from its childView.
解决方案
在你父母的布局,加入这一行:
In your parent layout,add this line:
android:addStatesFromChildren="true"
如果你在你的java code有parentView的参考,那么你可以通过使用实现的任务是:
and if you have the reference of parentView in your java code then you can achieve the task by using:
parentView.setAddStatesFromChildren(true);
其中,parentView是你的父母布局即: RelativeLayout的
where,parentView is your parent layout i.e.:RelativeLayout
注意:
请确保您的childview不重复父母的状态。
即:的android:duplicateParentState
或 childView.setDuplicateParentState(真)
make sure that your childview is not duplicating parent's state.
i.e.: android:duplicateParentState
or childView.setDuplicateParentState(true)
我希望这会有所帮助!
这篇关于更改扩展列表子视图的背景被点击子布局的元素时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!