Search code examples
javaandroidandroid-layoutandroid-fragmentsmaster-detail

Customize the ListView in a Fragment-based Master-Detail Flow?


I found the template that ADT generates for a Master/Detail Flow Activity to be rather obtuse, and I don't like the fact that the ListView is not declared in a layout file which forces me to work with it programatically. For example, instead of just setting android:background on the ListView in a layout file, I'm forced to find the ListView via findViewById in the fragment's onCreateView(...) method, then call setBackground(...). For maintainability and general readability, I'd like to do the same thing via a layout file.

Instead, I'm trying to inflate a custom layout in the onCreateView method of the "master" fragment:

public class InboxFragment extends ListFragment {

public InboxFragment() {}

private ListView listView = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
    listView = (ListView) inflater.inflate(R.layout.inbox_fragment_layout, 
            null);
    return listView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // the following doesn't work
    listView.setAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(), 
            android.R.layout.simple_list_item_activated_1, 
            android.R.id.text1, 
            DummyContent.ITEMS));
    // nor does this
    //setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(), 
    //      android.R.layout.simple_list_item_activated_1, 
    //      android.R.id.text1, 
    //      DummyContent.ITEMS));
}

}

However, calling setListAdapter (or even setAdapter) doesn't seem to do anything, even though I've given the ListView an id of android:id="@id/android:list" in R.layout.inbox_fragment_layout:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#D3D3D3" />

My "master" activity just calls a fragment layout:

public class InboxActivity extends FragmentActivity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inbox_phone_layout);
    }

}

inbox_phone_layout.xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragmentPhoneInbox"
    android:name="com.justin.inbox.InboxFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

Instead, I'm greeted with a blank page and it doesn't look like the ListView is loaded at all. What am I doing wrong, here? Sample project on GitHub.


Solution

  • I fixed your issue by changing inbox_phone_layout.xml to the following:

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragmentPhoneInbox"
        android:name="com.justin.inbox.InboxFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />