【问题标题】:ListFragment Layout from xml来自 xml 的 ListFragment 布局
【发布时间】:2012-07-31 00:42:30
【问题描述】:

如何从xml创建一个ListFragment(支持v4库)layoutListFragment 以编程方式设置它的布局,所以如果你想修改它,你需要弄乱添加/删除视图。

【问题讨论】:

  • 请编辑它,以便清楚问题是什么(将其作为问题发布)并将解决方案放在下面的答案部分中。
  • 你有例子可以参考吗?
  • 帖子应该只是问题和答案的形式。这看起来像是一个解决方案,但不清楚它回答的原始问题是什么。
  • 是的,您是对的,我要求的是示例帖子,或者在答案中移动正文并添加问题就足够了?
  • 是的,那将是完美的。谢谢。

标签: android xml layout fragment


【解决方案1】:

我已将创建的布局转换为 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>

android.support.v4.app中的ListFragmentLayout类(在你的项目中,你不需要修改支持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);
    }
}

在扩展 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;
}

【讨论】:

  • 您的意思是,通过这些更改,它将像不是自定义视图一样工作?
  • 对我不起作用。看起来我正在使用旧版本的支持库。它甚至不包含 ListFragmentLayout 类。我不建议使用这种方法。在支持库的未来版本中,它可能无法正常工作。
  • @sherpya,我按照你的回答得到了The field ListFragment.INTERNAL_EMPTY_ID is not visible 错误
  • 你必须将代码放在包 android.support.v4.app 因为该字段具有默认可见性
  • 也许您仍然可以从 对应的 源中选择值,在 r12 中它们是: static final int INTERNAL_EMPTY_ID = 0x00ff0001;静态最终 int INTERNAL_PROGRESS_CONTAINER_ID = 0x00ff0002;静态最终 int INTERNAL_LIST_CONTAINER_ID = 0x00ff0003;
【解决方案2】:

来自android sdk (ListFragment) (http://developer.android.com/reference/android/app/ListFragment.html):

public View onCreateView(LayoutInflater inflater、ViewGroup 容器、Bundle savedInstanceState)

提供默认实现以返回简单的列表视图。子类可以覆盖以替换为自己的布局。如果这样做,返回的视图层次结构必须有一个 ID 为 android.R.id.list 的 ListView,并且可以选择有一个兄弟视图 id android.R.id.empty,当列表为空时显示。

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

因此,当您想要自己的 layout.xml 用于 FragmentList 时,请覆盖 onCreateView 并扩充您自己的布局。

例如:

创建一个 android 布局 (yourlistlayout.xml) 并将以下 xml 放在要显示 ListFragment 的位置。

    <ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
    </ListView>

在您的 ListFragment 类中输入以下代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.yourlistlayout, null);       
    return v;       
}

【讨论】:

  • 这不适用于支持库,因为 id 不同
猜你喜欢
  • 1970-01-01
  • 2012-07-16
  • 2012-05-23
  • 2013-04-27
  • 2012-12-09
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多