Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintaining highlight for selected item in fragment?

I have a layout with two fragments. The left hand fragment is a ListFragment using a SimpleCursorAdaptor, the right hand one is populated with details about the item selected from the list in the left fragment. I'm trying to figure out how to make sure the selected item from the ListFragment stays highlighted until another item in the list is selected.

After some research I got as far as trying to use android:background="@drawable/item_selector" I can changes the colors for the different states, but none of them seem to persist. I thought that selected would... it just seems logical that your selected item remains the selected item until you select a different one.

I even tried using v.setSelected(true); in my clickhandler hoping it might maintain the state, but that didn't work either.

Is there a state I'm missing? I looked through the dev docs and nothing else seemed appropriate...

item_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_pressed="true" 
        android:drawable="@color/green" />
    <item 
        android:state_selected="true" 
        android:drawable="@color/blue" />
</selector>

I'm not sure what other code might help, so feel free to ask for whatever else you might think necessary.

like image 482
Barak Avatar asked Apr 05 '12 06:04

Barak


People also ask

What is dynamic fragment?

A fragment is usually used as part of an activity's user interface and contributes its own layout to the activity. A fragment is implemented as independent object -- independent of the activity that contains it. The benefit is that it can be used by multiple activities associated with the application.

How to add fragment in activity dynamically?

We can use the getSupportFragmentManager() method to get a fragment manager. FragmentTransaction. add(): This method is used to add a fragment to the activity. addToBackStack(): This method is used to add a transaction to the back stack.

How to attach a fragment to an activity?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

How to add fragment programmatically in android?

To programmatically add or remove a Fragment, you will need the FragmentManager and FragmentTransaction instances. Simply stated and per the Android documentation, a FragmentManager manages the fragments in an Activity. Obtain the FragmentManager by calling getFragmentManager( ) in an Activity.


2 Answers

The answer was in the android documentation after further research (two parts of the documentation really).

First, in touch mode there is no selected or focused state.

Second, by default, listviews are set to a choice mode of none (meaning that no item in the list can have a chosen state). All I had to do was change the choice mode (it could be single or multiple, I only needed/wanted single) by adding this:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

Then I used the choice state in my selector XML (translates to the activated state):

<item 
 android:state_activated="true" 
 android:drawable="@color/blue" />

Apply that in the row layout xml file as the background:

   <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:background="@drawable/item_selector"
    android:gravity="center_vertical"/>

My chosen item now appears with a blue background until a different item is chosen.

Note that this (android:state_activated) requires Android API 11 or later.

like image 174
Barak Avatar answered Nov 09 '22 10:11

Barak


I believe you need to add state_focused too to make it feel like selected or highlighted.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_pressed="true" 
        android:drawable="@color/green" />
    <item 
        android:state_selected="true" 
        android:drawable="@color/blue" />

    <item 
        android:state_focused="true" 
        android:drawable="@color/red" />
</selector>
like image 35
waqaslam Avatar answered Nov 09 '22 08:11

waqaslam