在具有自定义视图的ListFragment上使用setListShown时出错

时间:2012-10-13 03:51:14

标签: android android-fragments android-listfragment

  

可能重复:
  list fragment does not accept my layout

我正在尝试在ListFragment上使用自定义视图,但是当我尝试使用setListShownsetListShownNoAnimation时,我得到了一个异常!

我正在使用android-support-v4

谢谢!

1 个答案:

答案 0 :(得分:14)

创建布局文件 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>

将它放在 ListFragment 类中:

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;
}

现在你可以正常使用:

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