将 list_content 包含到列表布局中以保留 ListFragment 功能

发布于 2024-12-11 12:42:41 字数 806 浏览 0 评论 0原文

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

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

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

问题是,当我进入布局文件并尝试: 它(eclipse)告诉我

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

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

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

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

First of, I'm using The Android Compatibility Library V4 rev.3 for my 1.6(Donut)

When you first create a ListFragment it displays an indeterminant progress indicator untill you use setListAdabpter(). According to the documentation of ListFragment.onCreateView, if I want to use a custom lay out:

If you are overriding this method with your own custom content,
consider including the standard layout {@link
android.R.layout#list_content} in your layout file, so that you
continue to retain all of the standard behavior of ListFragment. In
particular, this is currently the only way to have the built-in
indeterminant progress state be shown.

The problem is that when I go into my layout file and try:
<include layout="@android:layout/list_content" ...> it (eclipse) tells me that

Resource is not public. (at 'layout' with value '@android:layout/list_content').

I take that to mean list_content.xml doesn't exist in my V4 source code. which is correct because according to the docs it appeared in API v11.

I just assumed it would exist in the compatibility library source, don't know if it does...

The only other solution I can see would be to dig android's API V11 source code and copy and paste list_content.xml. For now however I would like to avoid this hackery. is this the only solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

入怼 2024-12-18 12:42:41

兼容性库中包含的 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 源代码复制布局文件和包含此视图初始化代码之间的选择,我认为很清楚是哪一个将被视为“黑客”;)

The ListFragment included in the compatibility library doesn't appear to use that layout (list_content) as the real one does. This might be because it is offered as a jar and it is not possible to package resources. Here are the contents of its onCreateView:

ListFragment from compatibility library:

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

ListFragment from Android 4.0:

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

Given the choice between copying the layout file from APIv11 source and including this view initialisation code, I think it is clear which one would be considered 'hackery' ;)

郁金香雨 2024-12-18 12:42:41

我很沮丧地发现 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 曾经位于我的自定义布局中。

I was frustrated to see that the list_content layout was not available using the support library. My approach was simply to reuse the default onCreateView method of the ListFragment and add it as a part of my custom layout:

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

I added a FrameLayout where the ListView used to be in my custom layout.

踏雪无痕 2024-12-18 12:42:41

使用兼容性库时将默认 @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 的功能齐全的默认行为,同时仍具有自定义布局。

One possible work-around to incorporate default @android.R.layout/list_content into custom layout when using compatibility library is to create a dummy 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);
    }
}

Then including this fragment (instead of recommended list_content layout) into your custom layout:

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

This way you'll get a fully functional default behavior of the ListFragment while still having custom layout for it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文