Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

listview - Android: set list view item as "selected" (highlighted)

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.

This is what I have so far:

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) For the top linear layout of the list item:

android:background="@drawable/listitem_background"

(I tried setting this as listselector, as well)

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) 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);
    ...
}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

OK, finally got my answer.

The idea is to use the state_activated in the selector and

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE) 

in the code, or

android:choiceMode="singleChoice"

in the xml, of course

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...