我有一个列表视图,在这里我想强调一个自定义的方式选择的项目。我设置每个项目的属性在适配器的 getView
的方法,包括 itemView.setSelected(真)
。
I have a listview, where I want to highlight selected items in a custom way. I'm setting every item properties in the adapter's getView
method, including itemView.setSelected(true)
.
的主要布局定义列表视图中的方式如下:
The main layout defines the listview in the following way:
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="multipleChoice"
android:listSelector="@drawable/list_selector" />
(与选择模式播放也没有帮助)。
(Playing with choice mode does not help either).
在 list_selector 是一个几乎空存根:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@android:color/transparent" />
</selector>
我不需要特定的样式列表视图作为一个整体,所以我要离开一个默认的,但根据这个答案,我们需要选择一个列表视图的项目选择工作。总之,没有在 list_selector 问题仍然存在。
I don't need specific styles for listview as a whole, so I'd leave a default one, but according to this answer, we need a selector for a listview for item selector to work. Anyway, without the list_selector the problem remains.
在ListView项目布局:
The listview item layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:background="@drawable/listitem_background"
android:orientation="vertical">
和它引用下面的 listitem_background 选择:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@android:color/white" />
<item android:drawable="@android:color/transparent" />
</selector>
的问题是,所选择的项目不具有白色背景
The problem is, that selected items do not have white background.
如果我修改安卓state_selected =真正的
选择在 listitem_background 即可,例如,机器人: STATE_ pressed =真正的
,然后选择开始工作,这是项目的背景变成白色,如果一个项目是感动。
If I change android:state_selected="true"
selector in the listitem_background to, for example, android:state_pressed="true"
, then the selector starts to work, that is item background becomes white if an item is touched.
所以,我认为,有错误或者在选择器,或我怎么选择项目的方式。
So, I suppose, there is an error either in the selectors, or in the way how I select items.
我可以从Java设置背景或利用辨认的状态写入一个解决办法,但我想了解和解决当前的问题与选择。先谢谢了。
I can write a workaround by setting background from Java or utilizing checkable states, but I want to understand and fix current problem with selectors. Thanks in advance.
推荐答案
那么在你的项目视图中添加这一行?
What about adding this line in your item view?
android:background="?android:attr/activatedBackgroundIndicator"
例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewListRowOption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
</LinearLayout>
然后选择看起来是这样的:
Then the selector looks something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@drawable/list_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_selected" android:state_activated="true"/>
<item android:drawable="@drawable/list_default"/>
</selector>
和列表视图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listViewMainFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="@drawable/list_item_selector"
android:choiceMode="singleChoice">
</ListView>
</LinearLayout>
相关推荐
最新文章