切换内部片段 [英] Toggle Inside Fragment

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

问题描述

我使用NavigationView(使用切换按钮打开)进行了一个活动:

I have made one Activity with a NavigationView (opened with a toggle button):

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

private DrawerLayout drawer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layout);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.app_name, R.string.app_name);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    if (navigationView != null) {
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setItemIconTintList(null);
        navigationView.getMenu().getItem(0).setChecked(true);
    }
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer != null) {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}


@Override
public boolean onNavigationItemSelected(MenuItem item) {
    Fragment fragment = null;
    Class fragmentClass;

            fragmentClass = FirstFragment.class;

            try {
                fragment = (Fragment) fragmentClass.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }

            break;
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.layout, fragment).commit();
    item.setChecked(true);
    setTitle(item.getTitle());
    drawer.closeDrawers();
    return true;
}
}

当我选择NavigationView的一项时,它将启动一个片段:

When I select one item of NavigationView it starts a Fragment:

public class FirstFragment extends Fragment {

private View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.activity_layout, container, false);
    Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
    DrawerLayout drawer = (DrawerLayout) view.findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            getActivity(), drawer, toolbar, R.string.app_name, R.string.app_name);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    return view;
}
}

问题:当我选择按钮≡打开Fragment内的NavigationView时,我选择了不会打开FirstFragment的项目.如果我从左侧滑动以打开Fragment内的NavigationView,然后选择项目,则它将打开FirstFragment.如何在FirstFragment的MyActivity中重用onNavigationItemSelected?

Problem: When I select the button ≡ to open the NavigationView inside Fragment and I select the items it doesn't open FirstFragment. If I swipe from the left to open NavigationView inside Fragment and I select the items it opens FirstFragment. How can I reuse onNavigationItemSelected from MyActivity on FirstFragment?

这是acitivity_layout.xml:

This is the acitivity_layout.xml:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ddffff"
android:fitsSystemWindows="true">

<LinearLayout
    android:id="@+id/list_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    //FragmentLayout
    <RelativeLayout
        android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

        <include
            android:id="@+id/bar"
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

            ...

    </RelativeLayout>

</LinearLayout>


<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#ddffff"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

  </android.support.v4.widget.DrawerLayout>

推荐答案

根本原因是您在 Fragment 中使用与 Activity中相同的布局,并且 Toolbar 位于容器 ViewGroup 中,您可以将 Fragment 放入其中.一旦使用自己的 DrawerLayout Toolbar 加载了 Fragment ,它就会覆盖 Activity 工具栏,因此没有什么与众不同.但是, Fragment 中的切换按钮正在操作 Fragment 中的抽屉,而该抽屉是第二个 NavigationView ,它的设置方式不像 Activity 中的一个,因此,当您单击通过扳钮打开的抽屉时,没有任何反应.但是,如果将抽屉拖动到打开的位置,打开的实际上是 Activity 的抽屉,并且 NavigationView 正确设置,所以单击该按钮即可正常工作.

The root cause of this is that you're using the same layout in your Fragment as you are in the Activity, and the Toolbar is inside the container ViewGroup you transact the Fragment into. Once a Fragment has been loaded with its own DrawerLayout and Toolbar, it covers the Activity's Toolbar, so nothing looks out of the ordinary. However, the toggle in the Fragment is operating the drawer in the Fragment, and that drawer is a second NavigationView that hasn't been setup like the one in the Activity, so nothing happens when you click on the drawer opened with the toggle. But, if you drag the drawer open, the one that's opening is actually the Activity's drawer, and that NavigationView was setup correctly, so clicking on that one works as expected.

第一个更正是将 Toolbar -它的< include> 元素,即-移动到您所在的 RelativeLayout 外部重新用作 Fragment 容器.这将防止在加载 Fragment 时遮盖 Activity Toolbar .

The first correction is to move the Toolbar - its <include> element, that is - to outside of the RelativeLayout you're using as the Fragment container. This will prevent the Activity's Toolbar from being obscured when Fragments are loaded.

第二步是为 Fragment s定义单独的,不同的布局XML文件.您不想在 Fragment 中使用与在 Activity 中相同的布局,主要是因为这可能并不是您真正想要的,而且还因为您确实不想有多个冗余的 DrawerLayout s, Toolbar s和 NavigationView s,这会导致问题,如您所见

The second step is to define separate, different layout XML files for the Fragments. You don't want to use the same layout in the Fragments as you are in the Activity, mostly because that's probably not really what you intend, and also because you really don't want to have multiple, redundant DrawerLayouts, Toolbars, and NavigationViews, which can cause problems, as you've seen.

这篇关于切换内部片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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