首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在列表布局中包含list_content以保留ListFragment功能

在列表布局中包含list_content以保留ListFragment功能
EN

Stack Overflow用户
提问于 2011-10-23 06:34:46
回答 3查看 6.5K关注 0票数 16

首先,我在1.6(甜甜圈)上使用的是安卓兼容库V4 rev.3。

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

链接如果您使用自己的自定义内容覆盖此方法,请考虑在版面文件中包含标准版面{@

android.R.layout#list_content},以便继续保留ListFragment的所有标准行为。特别是,这是当前显示内置的不确定进度状态的唯一方法。

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

资源不是公共的。(在'layout‘,值为'@android:layout/list_content')。

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

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

我能想到的唯一的解决方案就是挖掘android的API V11源代码,然后复制并粘贴list_content.xml。但是,现在我想避免这种黑客攻击。这是唯一的解决方案吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-17 05:38:12

兼容性库中包含的ListFragment似乎并不像真正的布局(list_content)那样使用该布局。这可能是因为它是作为jar提供的,并且不可能打包资源。下面是它的onCreateView的内容

兼容库中的ListFragment:

代码语言:javascript
复制
@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;
}

来自Android4.0的ListFragment:

代码语言:javascript
复制
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(com.android.internal.R.layout.list_content, container, false);
}

如果要在从APIv11源代码复制布局文件和包含此视图初始化代码之间进行选择,我认为很清楚哪一个会被认为是“黑客”;)

票数 9
EN

Stack Overflow用户

发布于 2013-10-24 07:34:45

我很沮丧地看到list_content布局不能使用支持库。我的方法很简单,就是重用ListFragment的默认onCreateView方法,并将其添加到我的自定义布局中:

代码语言:javascript
复制
@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;
}

我在以前自定义布局中的ListView所在的位置添加了一个FrameLayout。

票数 2
EN

Stack Overflow用户

发布于 2012-04-19 23:38:50

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

代码语言:javascript
复制
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布局)包含到您的自定义布局中:

代码语言:javascript
复制
...
<fragment class="com.example.app.ListContentFragment"
    android:layout_weight   ="1" 
    android:layout_width    ="fill_parent"
    android:layout_height   ="0dip" />
...

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7863074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档