创建一个嵌套的片段 [英] Create a Nested Fragment

查看:204
本文介绍了创建一个嵌套的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有MainLayout包含DrawerLayout的多个实例,每个Drawerlayout有3个项目,每个项目都有一个片段。当我在一个项目单击MainLayout的片段显示由FragmentTransaction。

I have MainLayout which contains multiple instances of DrawerLayout, each Drawerlayout has 3 items and every item has a fragment. When I click on an item its fragment displays on MainLayout by FragmentTransaction.

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.home) {
        FragmentTransaction transaction = manager.beginTransaction();
        Hello f1 = new Hello();
        transaction.replace(R.id.main_layout,f1,"home");
        transaction.commit();

    }

至此,项目运行而不会出现问题。
但是,我需要把碎片项目的片段(嵌套片段)内。
项目的片段已经有它内部的一个片段,所以
我该怎么做呢?

Up to this point, the project runs without problems. But, I need to put Fragment inside an item's Fragment (nested Fragment). The Fragment of item already has a fragment inside of it, so how can I do this?

推荐答案

您可以嵌套使用主机片段的片段子经理另一个片段内的片段。一个例子可以设置是这样的:

You can nest a fragment inside another fragment using the host fragment's child fragment manager. An example setup can look like this:

HostFragment.java ,承载任意片段的宿主片段:

HostFragment.java, a host fragment that hosts an arbitrary fragment:

public class HostFragment extends Fragment {
private Fragment hostedFragment;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View view = inflater.inflate(R.layout.host_fragment, container, false);
    if (hostedFragment != null) {
        replaceFragment(hostedFragment);
    }
    return view;
}

public void replaceFragment(Fragment fragment) {
    getChildFragmentManager().beginTransaction().replace(R.id.hosted_fragment, fragment).commit();
}

public static HostFragment newInstance(Fragment fragment) {
    HostFragment hostFragment = new HostFragment();
    hostFragment.hostedFragment = fragment;
    return hostFragment;
}

}

host_fragment.xml ,由HostFragment类充气的布局:

host_fragment.xml, the layout inflated by the HostFragment class:

<?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"
         android:id="@+id/hosted_fragment" >

</FrameLayout>

如果您还需要单独的后退导航每个HostFragment,请参阅<一个href=\"https://medium.com/@nilan/separate-back-navigation-for-a-tabbed-view-pager-in-android-459859f607e4\"相对=nofollow>本教程我已经写了ViewPager类似的情况。希望你能适应教程你的情况。另请参阅和<一个href=\"https://guides.$c$cpath.com/android/Creating-and-Using-Fragments#nesting-fragments-within-fragments\"相对=nofollow>此内容 codePATH的有关片段指南的部分。

If you also need separate back navigation for each HostFragment, refer to this tutorial I've written about a similar situation with a ViewPager. Hopefully you can adapt the tutorial to your situation. Also refer to this and this section of codepath's guide about fragments.

这篇关于创建一个嵌套的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆