包括list_content到列表布局以保留ListFragment功能

cod*_*gan 16 android android-fragments android-support-library

首先,我使用Android兼容性库V4 rev.3为我的1.6(甜甜圈)

首次创建ListFragment时,它会显示一个不确定的进度指示器,直到您使用setListAdabpter().根据ListFragment.onCreateView的文档,如果我想使用自定义布局:

如果要使用自己的自定义内容覆盖此方法,请考虑在布局文件中包含标准布局{@link android.R.layout#list_content},以便继续保留ListFragment的所有标准行为.特别是,这是目前显示内置不确定进度状态的唯一方法.

问题是,当我进入我的布局文件并尝试: <include layout="@android:layout/list_content" ...>它(eclipse)告诉我

资源不公开.(在'layout'中,值为'@android:layout/list_content').

我认为这意味着list_content.xml我的V4源代码中不存在.这是正确的,因为它根据API v11中出现的文档.

我只是假设它将存在于兼容性库源中,不知道它是否...

我能看到的唯一其他解决方案是挖掘android的API V11源代码并进行复制和粘贴list_content.xml.但是现在我想避免这个hackery.这是唯一的解决方案吗?

ant*_*nyt 9

ListFragment包含在兼容性库没有出现使用该布局(list_content)作为真正的人做.这可能是因为它以jar形式提供,并且无法打包资源.以下是其内容onCreateView:

兼容性库中的ListFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final Context context = getActivity();

    FrameLayout root = new FrameLayout(context);

    // ------------------------------------------------------------------

    LinearLayout pframe = new LinearLayout(context);
    pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
    pframe.setOrientation(LinearLayout.VERTICAL);
    pframe.setVisibility(View.GONE);
    pframe.setGravity(Gravity.CENTER);

    ProgressBar progress = new ProgressBar(context, null,
            android.R.attr.progressBarStyleLarge);
    pframe.addView(progress, new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    root.addView(pframe, new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

    // ------------------------------------------------------------------

    FrameLayout lframe = new FrameLayout(context);
    lframe.setId(INTERNAL_LIST_CONTAINER_ID);

    TextView tv = new TextView(getActivity());
    tv.setId(INTERNAL_EMPTY_ID);
    tv.setGravity(Gravity.CENTER);
    lframe.addView(tv, new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

    ListView lv = new ListView(getActivity());
    lv.setId(android.R.id.list);
    lv.setDrawSelectorOnTop(false);
    lframe.addView(lv, new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

    root.addView(lframe, new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

    // ------------------------------------------------------------------

    root.setLayoutParams(new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

    return root;
}
Run Code Online (Sandbox Code Playgroud)

来自Android 4.0的ListFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(com.android.internal.R.layout.list_content, container, false);
}
Run Code Online (Sandbox Code Playgroud)

鉴于从APIv11源复制布局文件和包含此视图初始化代码之间的选择,我认为很明显哪一个将被视为'hackery';)