Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Highlight listview item when selected

$
0
0

Please help. I can't figure out how to highlight a listview item when it's selected. My code (see below for snippits) is not changing the color of the ListView item at all. The item also needs to remain highlighted no matter where else on the screen is tapped unless another item in the list is tapped, in which case switch the highlight to the new item and return the old item to default color. The View consists of an ActionBarSherlock with a menu, an ImageButton above the ListView, the ListView, and an ImageButton below the ListView.

In drawable folder: background_selection.xml

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/listitem_default_color" android:state_activated="false"/>
    <item android:drawable="@color/listitem_default_color" android:state_activated="false" android:state_pressed="false"/>
    <item android:drawable="@color/listitem_pressed_color" android:state_pressed="true"/>
    <item android:drawable="@color/listitem_focused_color" android:state_activated="true"/>
</selector>

In values folder: colors.xml

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
    <color name="background">#A0A0A0</color>
    <color name="listitem_default_color">#A0A0A0</color>
    <color name="listitem_pressed_color">#FFFFFF</color>
    <color name="listitem_focused_color">#FFFFFF</color>
</resources>

In layout folder: List.axml

...
<ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_height="wrap_content"
        android:id="@android:id/list"
        android:background="@drawable/background_selection"
        android:layout_width="match_parent"
        android:layout_weight="0.8" />
    ...
</ListView>
...

In ListActivity code:

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);
    SetTheme (Resource.Style.MyAppTheme);
    SetContentView (Resource.Layout.List);
    ListView.ChoiceMode = ChoiceMode.Single;
    ...
}

void ListView_ItemClick (object sender, AdapterView.ItemClickEventArgs e)
{
    e.View.Selected = true;
    //If the below two lines are added, it highlights the menu in the action bar and does nothing with the ListView
    //e.View.RequestFocusFromTouch();
    //e.View.RequestFocus();
    ...
}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>