来自xml的ListFragment布局

she*_*pya 6 xml layout android fragment

如何从xml 创建ListFragment(支持v4库)布局ListFragment以编程方式设置其布局,因此如果要修改它,则需要添加/删除视图.

she*_*pya 14

我已将创建的布局转换为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>
Run Code Online (Sandbox Code Playgroud)

和类ListFragmentLayoutandroid.support.v4.app(在你的项目中,你不需要修改支持v4 jar)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的片段中延伸 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;
}
Run Code Online (Sandbox Code Playgroud)