[英]Change text color on listView with buttons
I'm creating a ListView with buttons and having some issues. 我正在创建带有按钮的ListView并出现一些问题。
My activity need to do 2 different actions for each action ( ItemClick
and buttonClick
). 我的活动需要为每个操作(
ItemClick
和buttonClick
)执行2个不同的操作。
I assumed that: 我以为:
1 – Because I has button on list items, I cant use OnItemClickListener()
. 1 –因为我在列表项上有按钮,所以不能使用
OnItemClickListener()
。 Right? 对?
So, I create layout for list items and make it clickable. 因此,我为列表项创建布局并使其可单击。
listitem_textview_button.xml: listitem_textview_button.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="@drawable/selector_list_item">
<Button
android:id="@+id/listitem_textview_button_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:text="@string/edit" />
<TextView
android:id="@+id/listitem_textview_button_txv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:textSize="14sp"
android:textColor="@drawable/selector_textview"
android:minHeight="?android:attr/listPreferredItemHeight"
android:layout_toRightOf="@+id/listitem_single_line_w_button_btn" />
</RelativeLayout>
Notice that I've created a selector for the layout and a stateColorList for Textview. 注意,我已经为布局创建了一个选择器,并为Textview创建了stateColorList。
selector_list_item.xml selector_list_item.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false"
android:drawable="@color/transparent" />
<item
android:state_focused="true"
android:state_enabled="false"
android:state_pressed="true"
android:drawable="@drawable/shape_list_item_disabled" />
<item
android:state_focused="true"
android:state_enabled="false"
android:drawable="@drawable/shape_list_item_disabled" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/shape_list_item_transition" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/shape_list_item_transition" />
<item
android:state_focused="true"
android:drawable="@drawable/shape_list_item_focus" />
</selector>
selector_textview.xml selector_textview.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/black" />
<item android:state_focused="true" android:color="@color/black" />
<item android:state_pressed="true" android:color="@color/black" />
<item android:color="@color/red" />
</selector>
2 – This is the best way to implement a ListView with custom items (included TextView Colors)? 2 –这是用自定义项目(包括TextView颜色)实现ListView的最佳方法吗?
The above code does not change the color of textViews when I click on items. 当我单击项目时,上面的代码不会更改textViews的颜色。
In some tests I saw that the text color changes when: 在一些测试中,我看到文本颜色在以下情况下发生变化:
1. Using arrows of emulator. 1.使用仿真器箭头。 2. Removing the button of ListView item.
2.删除ListView项目的按钮。
Where is the problem? 问题出在哪儿?
prinscreens: listitem selected by arrow device (text black, ok !) 主屏幕:箭头设备选择的列表项(文本为黑色, 确定 !)
listitem clicked by finger (text red, should be black, fail ) 用手指点击的listitem(文本为红色,应为黑色, 失败 )
answer: 回答:
add android:duplicateParentState="true"
to TextView. 将
android:duplicateParentState="true"
到TextView。
I don't get it, your layout is clickable but you have buttons? 我不明白,您的布局可以点击,但是您有按钮? Is the button for selecting a row?
是用于选择行的按钮吗?
If I were you I would not make anything clickable except the button. 如果我是你,除了按钮,我什么都不会点击。 android:focusable="false" android:clickable="false"
android:focusable =“ false” android:clickable =“ false”
Then you can in the button listener set the selection on a row manually: 然后,您可以在按钮侦听器中手动设置选择:
getListView().setSelection(position);
Let me know if this works. 让我知道这个是否奏效。
[EDIT] The real problem is that the TextView needs to be clickable, not the layout. [编辑]真正的问题是TextView需要可单击,而不是布局。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.