ListFragment布局从XML(ListFragment Layout from xml)

2019-06-26 16:52发布

如何创建XML 布局 (支持V4库)一个ListFragment? ListFragment编程设置它的布局,所以如果你想修改它,你需要惹添加/删除视图。

Answer 1:

我已经转换所创建的布局,以XML,你可以添加/删除您的小工具或如增加pulltorefresh解决方案。 你不应该改变所需的ID。 由于需要一段的辅助函数需要的部件内部的ID被调用,它需要在支持V4命名空间,因为常量是内部默认的知名度。

list_loader.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/progress_container_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone" >

        <ProgressBar
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/list_container_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/empty_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center" />

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false" >
        </ListView>
    </FrameLayout>
</FrameLayout>

和类ListFragmentLayoutandroid.support.v4.app (在你的项目,你并不需要修改支持V4罐子)

package android.support.v4.app;

import your.package.R;
import android.view.View;

public class ListFragmentLayout
{
    public static void setupIds(View view)
    {
        view.findViewById(R.id.empty_id).setId(ListFragment.INTERNAL_EMPTY_ID);
        view.findViewById(R.id.progress_container_id).setId(ListFragment.INTERNAL_PROGRESS_CONTAINER_ID);
        view.findViewById(R.id.list_container_id).setId(ListFragment.INTERNAL_LIST_CONTAINER_ID);
    }
}

在片段扩展ListFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.list_loader, container, false);
    ListFragmentLayout.setupIds(view);
    return view;
}


Answer 2:

从Android SDK(ListFragment)( http://developer.android.com/reference/android/app/ListFragment.html ):

公共查看onCreateView(LayoutInflater吹气,ViewGroup中容器,包savedInstanceState)

提供默认实现返回一个简单的列表视图。 子类可以重写自己的布局来代替。 如果这样做,返回的视图层次结构必须有一个ListView id为android.R.id.list并且可能还会有一个同级视图id android.R.id.empty也就是当列表为空时显示。

如果你重写此方法与自己的自定义内容,请考虑在布局文件的标准布局list_content,让你继续保留所有ListFragment的标准行为。 特别是,这是目前具有内置的超静定进度状态显示的唯一途径。

所以,当你想你自己的FragmentList超越控制onCreateView layout.xml和夸大自己的布局。

例如:

创建一个Android的布局(yourlistlayout.xml),并把下面的XML上要显示你的ListFragment位置。

    <ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
    </ListView>

ListFragment类把下面的代码:

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


文章来源: ListFragment Layout from xml