Search code examples
javaandroidandroid-layoutandroid-listviewandroid-listfragment

How to change a ListFragment's ListView after using ViewFlipper's showNext()?


I have a ListFragment with a custom layout. What I'm trying to do is to switch my ListView in the same layout by using a ViewFlipper animation. This actually works but as I'm using an ArrayAdapter to populate both ListViews, a setListAdapter() doesn't seem to refresh my second ListView even tough I'm using it after calling showNext(). How do I tell my ListFragment that I've changed the view?. Is it even possible?.

This is my custom layout, and since I'm also using custom ListView's, I've got to use android:id="@id/android:list" for both lists.

   <LinearLayout>
        <TextView/>

        <ListView 
            android:id="@id/android:list"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:drawSelectorOnTop="false"/>        
    </LinearLayout>

    <LinearLayout>    
        <TextView/>

        <ListView 
            android:id="@id/android:list"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:drawSelectorOnTop="false"/>        
    </LinearLayout>

</ViewFlipper>

And this is part of my code:

headerFlipper = (ViewFlipper)getActivity().findViewById(R.id.viewFlipper);
headerFlipper.showNext();

// at this point a simple getListView() returns the same first ListView object.

// this changes the first ListView (not visible anymore)
// how to change the second one?
setListAdapter(new ArrayAdapter<DummyContent.DummyMessage>(getActivity(),
        android.R.layout.simple_list_item_activated_1,
        android.R.id.text1, DummyContent.MESSAGES));

Any advice? Thanks in advance.


Solution

  • You have two items with the same id in your viewflipper so the activity is only going to use one. To do it like this you'll need to change your current ListActivity to a FragmentActivity and then create two separate ListFragment activities to handle each list.

    Keep in mind that some of these class names may change depending on if you're using the support library, but overall there are very few differences between them.

    For the layouts, just place the two LinearLayouts for the fragments in their own xml file and include them in the viewFlipper xml.

    viewflipper.xml

    <?xml version="1.0" encoding="utf-8"?>
    <ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/viewFlipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <include 
            android:id="@+id/fragListOne"  
            layout="@layout/listfragmentone" />
    
        <include 
            android:id="@+id/fragListTwo"  
            layout="@layout/listfragmenttwo" />
    
    </ViewFlipper>
    

    then for each fragment you could use something like:

    listfragmentone & listfragmenttwo

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="8dp"
        android:paddingRight="8dp">
    
        <ListView android:id="@+id/android:list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00FF00"
                android:layout_weight="1"
                android:drawSelectorOnTop="false"/>
    
        <TextView android:id="@id/android:empty"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FF0000"
                android:text="No data"/>
    </LinearLayout>
    

    Code wise, you'll have your main activity that coordinates data and everything, and the two listview fragments that handle each listview separately. The main activity will also need to keep track of which one is visible, etc.

    Main activity - extends FragmentActivity

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.id.viewFlipper);
    
        // use fragment manager to see which fragments are instantiated
        // and available, then reuse any available in case of
        // orientation change
    
        FragmentManager.enableDebugLogging(true)
        FragmentManager fragManager = getSupportFragmentManager();
        ListFragmentOne fragOne = fragManager.findFragmentById("frag one tag");
    
        // if fragOne is null add a new instance via an add transaction
        // etc.
    }
    

    The Fragment activities, extending ListFragment, should be similar to normal list activities except you'll use onCreateView instead of onCreate.