Skip to content Skip to sidebar Skip to footer

Including List_content Into List Layout To Retain ListFragment Functionality

First of, I'm using The Android Compatibility Library V4 rev.3 for my 1.6(Donut) When you first create a ListFragment it displays an indeterminant progress indicator untill you us

Solution 1:

The ListFragment included in the compatibility library doesn't appear to use that layout (list_content) as the real one does. This might be because it is offered as a jar and it is not possible to package resources. Here are the contents of its onCreateView:

ListFragment from compatibility library:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final Context context = getActivity();

    FrameLayout root = new FrameLayout(context);

    // ------------------------------------------------------------------

    LinearLayout pframe = new LinearLayout(context);
    pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
    pframe.setOrientation(LinearLayout.VERTICAL);
    pframe.setVisibility(View.GONE);
    pframe.setGravity(Gravity.CENTER);

    ProgressBar progress = new ProgressBar(context, null,
            android.R.attr.progressBarStyleLarge);
    pframe.addView(progress, new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    root.addView(pframe, new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

    // ------------------------------------------------------------------

    FrameLayout lframe = new FrameLayout(context);
    lframe.setId(INTERNAL_LIST_CONTAINER_ID);

    TextView tv = new TextView(getActivity());
    tv.setId(INTERNAL_EMPTY_ID);
    tv.setGravity(Gravity.CENTER);
    lframe.addView(tv, new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

    ListView lv = new ListView(getActivity());
    lv.setId(android.R.id.list);
    lv.setDrawSelectorOnTop(false);
    lframe.addView(lv, new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

    root.addView(lframe, new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

    // ------------------------------------------------------------------

    root.setLayoutParams(new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

    return root;
}

ListFragment from Android 4.0:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(com.android.internal.R.layout.list_content, container, false);
}

Given the choice between copying the layout file from APIv11 source and including this view initialisation code, I think it is clear which one would be considered 'hackery' ;)


Solution 2:

I was frustrated to see that the list_content layout was not available using the support library. My approach was simply to reuse the default onCreateView method of the ListFragment and add it as a part of my custom layout:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View listContent = super.onCreateView(inflater, container,
            savedInstanceState);

    View v = inflater.inflate(R.layout.my_custom_layout, container,
            false);

    FrameLayout listContainer = (FrameLayout) v.findViewById(R.id.my_list_container);

    listContainer.addView(listContent);

    return v;
}

I added a FrameLayout where the ListView used to be in my custom layout.


Solution 3:

One possible work-around to incorporate default @android.R.layout/list_content into custom layout when using compatibility library is to create a dummy ListFragment:

package com.example.app;

import android.support.v4.app.ListFragment;

public class ListContentFragment extends ListFragment {
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        //Supress default list and empty text behavior
        //super.onViewCreated(view, savedInstanceState);
    }
}

Then including this fragment (instead of recommended list_content layout) into your custom layout:

...
<fragment class="com.example.app.ListContentFragment"
    android:layout_weight   ="1" 
    android:layout_width    ="fill_parent"
    android:layout_height   ="0dip" />
...

This way you'll get a fully functional default behavior of the ListFragment while still having custom layout for it.


Post a Comment for "Including List_content Into List Layout To Retain ListFragment Functionality"