16

首先,我在我的 1.6(Donut) 中使用 Android 兼容库 V4 rev.3

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

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

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

资源不公开。(在“布局”处,值为“@android:layout/list_content”)。

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

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

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

4

3 回答 3

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

来自 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);
}

考虑到从 APIv11 源代码复制布局文件和包含此视图初始化代码之间的选择,我认为很明显哪个会被视为“黑客”;)

于 2012-03-16T21:38:12.303 回答
2

看到使用支持库无法使用 list_content 布局,我感到很沮丧。我的方法只是重用 ListFragment 的默认 onCreateView 方法并将其添加为我的自定义布局的一部分:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View listContent = super.onCreateView(inflater, container,
            savedInstanceState);

    View v = inflater.inflate(R.layout.my_custom_layout, container,
            false);

    FrameLayout listContainer = (FrameLayout) v.findViewById(R.id.my_list_container);

    listContainer.addView(listContent);

    return v;
}

我添加了一个 FrameLayout,ListView 曾经在我的自定义布局中。

于 2013-10-23T23:34:45.420 回答
1

在使用兼容性库时将默认 @android.R.layout/list_content 合并到自定义布局中的一种可能解决方法是创建一个虚拟 ListFragment:

package com.example.app;

import android.support.v4.app.ListFragment;

public class ListContentFragment extends ListFragment {
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        //Supress default list and empty text behavior
        //super.onViewCreated(view, savedInstanceState);
    }
}

然后将此片段(而不是推荐的 list_content 布局)包含到您的自定义布局中:

...
<fragment class="com.example.app.ListContentFragment"
    android:layout_weight   ="1" 
    android:layout_width    ="fill_parent"
    android:layout_height   ="0dip" />
...

这样,您将获得 ListFragment 的功能齐全的默认行为,同时仍然具有自定义布局。

于 2012-04-19T15:38:50.123 回答