Android 如何在布局中为另一个片段使用片段xml标记?
我正在尝试做一个三页的活动,其中包含一个列表片段。所有的页面都是片段(来自v4支持库),但我需要它们里面的列表片段。当我尝试说我遇到了一个问题()时,我做了一些研究,了解到片段中不能有片段(我指的是xml片段标记,不添加动态嵌套的片段)。但例如,在whatsapp的上一次android更新中,一个活动中有三个页面,其中一个有列表片段(聊天列表),所以我想知道whatsapp是如何做到这一点的?谢谢 包含列表片段的我的xml布局:Android 如何在布局中为另一个片段使用片段xml标记?,android,android-fragments,android-viewpager,android-listfragment,Android,Android Fragments,Android Viewpager,Android Listfragment,我正在尝试做一个三页的活动,其中包含一个列表片段。所有的页面都是片段(来自v4支持库),但我需要它们里面的列表片段。当我尝试说我遇到了一个问题()时,我做了一些研究,了解到片段中不能有片段(我指的是xml片段标记,不添加动态嵌套的片段)。但例如,在whatsapp的上一次android更新中,一个活动中有三个页面,其中一个有列表片段(聊天列表),所以我想知道whatsapp是如何做到这一点的?谢谢 包含列表片段的我的xml布局: <RelativeLayout xmlns:android=
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="Welcome to newsfeed" />
<Button
android:id="@+id/nfButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="52dp"
android:text="@string/signout" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/nfListFragment"
android:name="com.comp.buzz.newsfeed.NFQuestionFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
tools:layout="@layout/item_question" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_newsfeed, container, false);
Fragment fragment = new ListFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.swipe_container, fragment).commit();
return view;
}
您可以嵌套片段,但有一些限制(根据):
- 安卓4.2或更高版本
- 只能通过编程方式嵌套它们
Fragment fragment = new YourFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.swipe_container, fragment).commit();
但是我有不同的布局,中间有一些文本视图、按钮和列表片段。所以我想我必须从xml中添加它。你能详细描述一下你的布局吗?我把它添加到我的问题中,它是viewpager的一个片段的布局。所以在我的主要片段中一定有那个列表片段。我有点困惑。。。你的问题是“我想知道whatsapp是怎么做到的?”,对吧?我想答案是他们是以编程方式添加的:)是的,但只能以编程方式添加的列表片段位于按钮和文本视图等其他东西之间,因此我如何以编程方式添加它,而且片段上还有另一个标记,如swiperefreshlayout:S