ListFragment does not accept my layout

前端 未结 10 1275
情歌与酒
情歌与酒 2020-12-01 02:09

According to this link: ListFragment android developers

I want to set my custom layout for list, but it makes exceptions.

Here is the code:

p         


        
相关标签:
10条回答
  • 2020-12-01 02:59

    This sounds like an issue that's been reported to Google already.

    Google Issue

    If this is indeed your issue, there's a couple of suggestions at the bottom of that page for fixing it (well, more making it work than fixing it).

    0 讨论(0)
  • 2020-12-01 03:07

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

    Source:

    http://source-android.frandroid.com/frameworks/base/core/java/android/app/ListFragment.java

    http://source-android.frandroid.com/frameworks/base/core/res/res/layout/list_content.xml

    0 讨论(0)
  • 2020-12-01 03:07

    Another solution that worked for me as well is just to put this line

    <include layout="@android:layout/list_content" />
    

    instead of your list view.

    You are able to build your own content on top of it or surround it with some other views. For example, here is how my layout looks like:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/default_view_padding"
            android:gravity="center"
            android:weightSum="4">
    
        <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btnCounterBack"
                android:src="?attr/ic_previous"
                android:background="?android:attr/selectableItemBackground"
                android:contentDescription="@null"/>
    
        <TextView
                android:id="@+id/tvCounterLevel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="@string/counter_categories"
                android:layout_gravity="center"
                android:paddingTop="@dimen/default_view_padding"
                android:paddingBottom="@dimen/default_view_padding"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:gravity="center"/>
    
    </RelativeLayout>
    
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:background="?attr/dropShadowBackground"
            android:orientation="vertical" >
    
        <include layout="@android:layout/list_content" />
    
    </LinearLayout>
    

    0 讨论(0)
  • 2020-12-01 03:10

    Try to remove setListShown(true);?

    I had same problem, some time ago. It's seams that you cant use it if you have custom layout in ListFragment.

    0 讨论(0)
提交回复
热议问题