在列表视图中显示当前选择

tyc*_*czj 28 android android-listview

正如平板电脑版的gmail和google talk中所见,我试图在列表视图中显示当前选择.我知道这不是标准做法,应该在必要时予以避免.在我的程序中,列表视图总是在屏幕上,点击的项目显示右侧的新片段(类似于gmail和google talk).

为了避免用户猜测选择了哪个项目,我想显示当前的选择,我尝试创建一个选择器,但在点击它之后它会变回正常的背景.

我该怎么做到这一点?

这是我的选择器xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/list_item_bg2" android:state_pressed="false" android:state_selected="false"
    android:state_focused="false"/>
<item android:drawable="@drawable/list_item_selected_bg2" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_selected_bg2" android:state_pressed="false" android:state_focused="false"
    android:state_selected="true" android:state_checked="false"/>
<item android:drawable="@drawable/list_item_selected_bg2" android:state_pressed="false" android:state_focused="true"
    android:state_selected="false"/>
<item android:drawable="@drawable/list_item_selected_bg2" android:state_pressed="false" android:state_focused="false"
    android:state_selected="false" android:state_checked="true"/>
<item android:drawable="@drawable/list_item_selected_bg2" android:state_pressed="true" android:state_focused="true"
    android:state_selected="true" android:state_checked="true"/>
<item android:drawable="@drawable/list_item_selected_bg2" android:state_pressed="true"/>



</selector> 
Run Code Online (Sandbox Code Playgroud)

Com*_*are 73

Gmail和类似应用程序使用的是activated状态,具有适当的行布局.看到:

简而言之,您:

  • 使用具有激活背景的行布局(例如android.R.layout.simple_list_item_activated_1)
  • 使用setChoiceMode(ListView.CHOICE_MODE_SINGLE)你的ListView
  • "检查"应使用激活该行setItemChecked()对你ListView启用"激活"状态,并有持久亮点

  • 如果有人想知道,这也适用于自定义行布局.你所要做的就是在你的listrow布局中添加`android:background ="?android:attr/activatedBackgroundIndicator". (2认同)

Jör*_*örg 9

你的另一个选择是将自定义列表项的背景设置为android:background ="?android:attr/activatedBackgroundIndicator"


Ran*_*med 8

如果您为每一行使用自定义布局:

  1. 用android创建一个选择器:state_activated ="true"
  2. 将其应用为自定义布局的背景.

选择器drawable的一个例子:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
 android:exitFadeDuration="@android:integer/config_shortAnimTime">
<item android:state_activated="true" android:drawable="@color/android_green" />
<item android:drawable="@android:color/transparent" />
</selector>
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

22542 次

最近记录:

10 年,3 月 前