Search code examples
androidandroid-fragmentsandroid-listfragment

Error using setListShown on a ListFragment with a custom view


Possible Duplicate:
list fragment does not accept my layout

I'm trying to use a custom view on a ListFragment but when I try to use setListShown or setListShownNoAnimation I get an exception!

I'm using android-support-v4.

Thank you!


Solution

  • Create the layout file list_content.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        <LinearLayout android:id="@+id/progressContainer"
                android:orientation="vertical"
                android:layout_width="match_parent" 
                android:layout_height="match_parent"
                android:visibility="gone"
                android:gravity="center">
    
            <ProgressBar style="?android:attr/progressBarStyleLarge"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            <TextView android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text=""
                    android:paddingTop="4dip"
                    android:singleLine="true" />
    
        </LinearLayout>
    
        <FrameLayout android:id="@+id/listContainer"
                android:layout_width="match_parent" 
                android:layout_height="match_parent">
    
            <ListView android:id="@android:id/list"
                    android:layout_width="match_parent" 
                    android:layout_height="match_parent"
                    android:drawSelectorOnTop="false" />
            <TextView android:id="@+id/internalEmpty"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:textAppearance="?android:attr/textAppearanceLarge" />
        </FrameLayout>
    
    </FrameLayout>
    

    Put this inside your ListFragment class:

    public ListView mList;
    boolean mListShown;
    View mProgressContainer;
    View mListContainer;
    
    public void setListShown(boolean shown, boolean animate){
        if (mListShown == shown) {
            return;
        }
        mListShown = shown;
        if (shown) {
            if (animate) {
                mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
                        getActivity(), android.R.anim.fade_out));
                mListContainer.startAnimation(AnimationUtils.loadAnimation(
                        getActivity(), android.R.anim.fade_in));
            }
            mProgressContainer.setVisibility(View.GONE);
            mListContainer.setVisibility(View.VISIBLE);
        } else {
            if (animate) {
                mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
                        getActivity(), android.R.anim.fade_in));
                mListContainer.startAnimation(AnimationUtils.loadAnimation(
                        getActivity(), android.R.anim.fade_out));
            }
            mProgressContainer.setVisibility(View.VISIBLE);
            mListContainer.setVisibility(View.INVISIBLE);
        }
    }
    public void setListShown(boolean shown){
        setListShown(shown, true);
    }
    public void setListShownNoAnimation(boolean shown) {
        setListShown(shown, false);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        int INTERNAL_EMPTY_ID = 0x00ff0001;
        View root = inflater.inflate(R.layout.list_content, container, false);
        (root.findViewById(R.id.internalEmpty)).setId(INTERNAL_EMPTY_ID);
        mList = (ListView) root.findViewById(android.R.id.list);
        mListContainer =  root.findViewById(R.id.listContainer);
        mProgressContainer = root.findViewById(R.id.progressContainer);
        mListShown = true;
        return root;
    }
    

    Now you can use normally:

    setListShown(boolean shown);
    setListShown(boolean shown, boolean animate);
    setListShownNoAnimation(boolean shown);