我知道android没有突出显示TouchMode中的任何内容。但我正在做一些类似于gmail应用程序的东西,你从左侧选择东西,并在活动的右侧显示细节(想知道谷歌是如何做到的)。
所以故事是我必须突出显示在左侧ListView中选择的内容。我发现了一些类似的问题,解决方案基本上是:
1.覆盖适配器的getView方法和setBackground以获取所选位置
2.ViewBackground视图onItemClick并清除它以进行另一个选择
但由于一种奇怪的行为,它们都没有为我工作:当我点击一个项目并突出显示它时,它后面的第五个项目也会突出显示,依此类推,当我向下滚动列表时。
有什么建议吗? THX!
答案 0 :(得分:23)
使用listView.setChoiceMode(int choiceMode);
参数
choiceMode来自android.widget.AbsListView类的CHOICE_MODE_NONE,CHOICE_MODE_SINGLE或CHOICE_MODE_MULTIPLE之一
http://developer.android.com/reference/android/widget/AbsListView.html#setChoiceMode(int)
您还需要添加MultiChoiceModeListener,您可以拥有CHOICE_MODE_SINGLE
(android.widget.AbsListView.MultiChoiceModeListener)
请参阅下面的示例
答案 1 :(得分:16)
这适用于自定义列表活动或ListFragment
在ListView中突出显示所选项目
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
{
for(int a = 0; a < parent.getChildCount(); a++)
{
parent.getChildAt(a).setBackgroundColor(Color.TRANSPARENT);
}
view.setBackgroundColor(Color.GREEN);
}
答案 2 :(得分:2)
//// @ drawable / list_selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal"
android:state_pressed="false" android:state_selected="false" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_touch"
android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_touch"
android:state_pressed="false" android:state_selected="true"/>
<item android:drawable="@drawable/list_item_bg_pressed"
android:state_activated="true"/>
</selector>
////////////////////和ListView
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"
android:divider="#060606"/>
/////////////////////// listview Item
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector">
<ImageView
android:id="@+id/icon"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="@string/desc_list_item_icon"
android:src="@drawable/ic_home"
android:layout_centerVertical="true" />
</RelativeLayout>
答案 3 :(得分:1)
您突出显示多个项目的原因可能是因为您要么:重用整个视图,要么将这两个视图的背景设置为同一个Drawable实例。如果屏幕上有相同的drawable两次,那么第一次发生的所有事件都会发生在所有其他事件上,因为该逻辑是在Drawable本身的实例中实现的。
要解决此问题,请执行以下任一操作:不要为多行重复使用Views,或者不要将Drawable重复用于多行(每次都创建一个新行)
我知道这听起来像资源密集型,但除非你有更好的解决方案,否则这是最容易解决的问题。
答案 4 :(得分:0)
因为listviews中的项目模仿顶部的项目,所以它们也模仿了它们的背景。您必须在getView()函数中设置项目的每个背景。在getView()的每个项目中,您必须为所选项目和未选择项目设置背景。
答案 5 :(得分:0)
将其用作列表选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_bg" />
</selector>
和list_bg
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/list_bg_color" />
</shape>
选择首选突出显示颜色