Android 来自xml的ListFragment布局
如何从xml创建(支持v4库的)列表片段布局?Android 来自xml的ListFragment布局,android,xml,layout,fragment,Android,Xml,Layout,Fragment,如何从xml创建(支持v4库的)列表片段布局? ListFragment以编程方式设置其布局,因此如果您想修改它,您需要修改添加/删除视图。我已将创建的布局转换为xml,您可以添加/删除小部件或添加pulltorefresh解决方案。您不应该更改所需的ID。 由于某些小部件需要内部ID,因此需要调用助手函数,并且它需要位于支持v4名称空间中,因为常量是内部的,具有默认可见性 list_loader.xml <?xml version="1.0" encoding="utf-8"?>
ListFragment
以编程方式设置其布局,因此如果您想修改它,您需要修改添加/删除视图。我已将创建的布局转换为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>
在扩展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;
}
从android sdk(ListFragment)()中:
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态)
提供默认实现以返回简单列表视图。子类可以重写以替换为自己的布局。如果这样做,则返回的视图层次结构必须具有id为android.R.id.list的ListView,并且可以选择具有同级视图id android.R.id.empty,当列表为空时将显示该id
如果您用自己的自定义内容重写此方法,请考虑在布局文件中包含标准布局列表内容,以便继续保留所有ListFract的标准行为。特别是,这是当前显示内置不确定进度状态的唯一方法
因此,当您希望自己的layout.xml用于CreateView上的碎片列表并膨胀自己的布局时
例如:
创建一个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>
请编辑此内容,以便清楚问题是什么(将其作为问题发布),并将解决方案放在下面的答案部分。您是否有可参考的示例?此帖子应以问题和答案的形式发布。这看起来像是一个解决方案,但不清楚它回答的原始问题是什么。是的,你是对的,我要求的是一个示例帖子,或者它足以在答案中移动身体并添加问题?是的,那将是完美的。谢谢。你是说有了这些更改,它会像不是自定义视图一样工作?对我不起作用。看起来我正在使用旧版本的支持库。它甚至不包含ListFragmentLayout类。我不建议使用这种方法。在以后的support lib版本中,它可能不起作用。@sherpya,我按照您的回答得到了
字段ListFragment.INTERNAL\u EMPTY\u ID不可见
错误您必须将代码放入程序包android.support.v4.app中,因为该字段具有默认的VisibilityRhaps,您仍然可以从相应的源中选择值,在r12中,它们是:静态最终int INTERNAL\u EMPTY\u ID=0x00ff0001;静态最终内部进度容器ID=0x00ff0002;静态最终内部列表容器ID=0x00ff0003;这不适用于支持库,因为ID不同
<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>
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.yourlistlayout, null);
return v;
}