Android:将列表视图项设置为“已选择”(突出显示)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12742269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: set list view item as "selected" (highlighted)
提问by Ciprian
In my application I want to do something similar to gmail app on tablets, in the left to have the list of items and in the right to have a fragment with the content of that item, like for gmail app this content is being downloaded after selection. After I click on an item I want it to remain highlighted until, of course I change the selection. I reached a point where this works but only if I click twice on the same item, so first I click, selection works and then the item goes back to its 'default' state and if I click again on it, the selector (for selected state) is visible.
在我的应用程序中,我想在平板电脑上做一些类似于 gmail 应用程序的事情,在左边有项目列表,在右边有一个包含该项目内容的片段,比如对于 gmail 应用程序,这个内容是在选择后下载的. 在我单击一个项目后,我希望它保持突出显示,直到我更改了选择。我到达了一个可以工作的点,但只有当我在同一个项目上单击两次时,所以首先我单击,选择工作,然后项目返回到它的“默认”状态,如果我再次单击它,选择器(对于选定的状态)是可见的。
This is what I have so far:
这是我到目前为止:
1) The selector (listitem_background.xml)
1)选择器(listitem_background.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/solid_white" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/listitem_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/listitem_focused" android:state_selected="true"/>
</selector>
2) For the top linear layout of the list item:
2)对于列表项的顶部线性布局:
android:background="@drawable/listitem_background"
(I tried setting this as listselector, as well)
(我也尝试将其设置为列表选择器)
3) This is the ListView:
3)这是列表视图:
<ListView
android:id="@+id/my_list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:drawSelectorOnTop="true"
android:fadeScrollbars="true"
android:fastScrollEnabled="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollbarFadeDuration="100"
android:scrollbars="vertical" />
4) In the code part I tried to play with this:
4)在代码部分我试图玩这个:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setSelected(true);
...
}
[EDIT] In fact I've noticed that the selection is lost after the commit of the fragment in the right side of the screen. If I don't commit the fragment it works like a charm... I think I need something like this in the selector:
[编辑] 事实上,我注意到在屏幕右侧提交片段后选择丢失了。如果我不提交片段,它就像一个魅力......我想我在选择器中需要这样的东西:
<item android:drawable="@drawable/listitem_focused" android:state_activated="true" android:state_focused="false"/>
But obviously not this...
但显然不是这个……
回答by Ciprian
OK, finally got my answer.
好的,终于得到我的答案了。
The idea is to use the state_activated in the selector and
这个想法是在选择器中使用 state_activated 和
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE)
in the code, or
在代码中,或
android:choiceMode="singleChoice"
in the xml, of course
在 xml 中,当然
This is how the selector should look like:
这是选择器的样子:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/solid_white" android:state_activated="false"/>
<item android:drawable="@drawable/solid_white" android:state_activated="false" android:state_pressed="false"/>
<item android:drawable="@drawable/listitem_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/listitem_focused" android:state_activated="true"/>
</selector>
This is how the list item layout should be:
列表项布局应该是这样的:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/listitem_background"
android:orientation="vertical" >
...
<LinearLayout/>