[英]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: 子行的布局类似于:
<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). 现在,当我单击TextView时,我想更改整个行的背景(最上层的相对布局包括在每个具有子布局的视图上)。 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). 另外,当OnChildClick()收到回调时,行的背景也会更改(由于选择器放置在顶层布局中)。
My attempt is: 我的尝试是:
In TextView's onlclick method: 在TextView的onlclick方法中:
((RelativeLayout)(nameView.getParent().getParent().getParent())).setPressed(true);
but this is not resulting in row layout to change background color. 但这不会导致行布局更改背景颜色。
Issue: 问题:
ultimately you want to trigger the selector of parentView from its childView. 最终,您想从其childView触发parentView的选择器。
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: 如果您在Java代码中具有parentView的引用,则可以通过使用以下命令完成任务:
parentView.setAddStatesFromChildren(true);
where,parentView is your parent layout ie: RelativeLayout
其中,parentView是您的父级布局,即: RelativeLayout
Note: 注意:
make sure that your childview is not duplicating parent's state. 确保您的子视图不会复制父项的状态。 ie: android:duplicateParentState
or childView.setDuplicateParentState(true)
即: android:duplicateParentState
或childView.setDuplicateParentState(true)
I hope it will be helpful ! 希望对您有所帮助!
Could you not just give an id to the layout where you want the elements to be colored and then something like: 您是否可以不只是给想要给元素上色的布局提供ID,然后再输入类似的内容:
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: 编辑:我一直在搜索,对我来说似乎您应该能够使用ExpandableListView的可用侦听器,以便在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. 只是想给您一种替代方法,以防您未尝试过类似的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.