设置列表视图项为&QUOT

设置列表视图项为&QUOT

本文介绍了安卓:设置列表视图项为"选择" (高亮)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我想要做类似的Gmail应用程序,一些平板电脑,在左侧有项目,并在右侧的列表中有与该项目的内容片断,像Gmail应用程序这个内容被选择后下载。当我点击一个项目,我希望它仍然突出,当然,直到我改变选择。我的地步了这个工作,但只有当我点击两次在同一个项目,所以首先我点击,选择作品,然后该项目可以追溯到它的默认状态,如果我再次点击它,选择(选定状态)是可见的。

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.

这是我到目前为止有:

1)选择(listitem_background.xml)

1) The selector (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)对于列表项的顶部线性布局:

2) For the top linear layout of the list item:

android:background="@drawable/listitem_background"

(我试过设置此为listselector,以及)

(I tried setting this as listselector, as well)

3)这是ListView控件:

3) This is the ListView:

<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)在code部分我试着用这个玩:

4) In the code part I tried to play with this:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    view.setSelected(true);
    ...
}

事实上,我也注意到,选择是遗失的片段后,在屏幕右侧的提交。如果我没有犯片段它的工作原理就像一个魅力...我想我需要像这样的选择:

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...

推荐答案

OK,终于得到了答案。

OK, finally got my answer.

我们的想法是使用state_activated在选择器以及

The idea is to use the state_activated in the selector and

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE)

在code,或

android:choiceMode="singleChoice"

在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/>

这篇关于安卓:设置列表视图项为&QUOT;选择&QUOT; (高亮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:27