如何使用导航抽屉嵌套碎片?

时间:2016-06-22 19:49:41

标签: java android android-fragments

我一直在尝试在最近几个小时内搜索SO,但除了尝试创建解决方法之外,我找不到解决方案。我正在尝试为我的应用程序创建一个导航抽屉,但为了这样做,我不得不最终尝试将我的基本活动更改为一个片段。这是结果:

public class ToolReaderActivity extends Fragment implements ToolListFragment.OnToolSelectedListener, CompatActionBarNavListener, OnClickListener {

    boolean mIsDualPane = false;

    Context c;

    ToolListFragment mToolListFragment;
    InfoFragment mInfoFragment;
    Fragment mContent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        c = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.onepane_with_bar, container, false);

        // find our fragments
        mToolListFragment = (ToolListFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.toolList);
        mInfoFragment = (InfoFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.toolInfo);

        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.toolList, mToolListFragment).commit();

        FragmentTransaction transaction2 = getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.toolInfo, mInfoFragment).commit();

        mToolListFragment.setContext(c);
        mToolListFragment.setOnToolSelectedListener(this);

        View infoView = getView().findViewById(R.id.toolInfo);
        mIsDualPane = infoView != null && infoView.getVisibility() == View.VISIBLE;

        int catIndex = savedInstanceState == null ? 0 : savedInstanceState.getInt("catIndex", 0);
        setUpActionBar(mIsDualPane, catIndex);

        mToolListFragment.setSelectable(mIsDualPane);
        restoreSelection(savedInstanceState);

        if (savedInstanceState != null) {
            //Restore the fragment's instance
            mContent = getActivity().getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
        }

        return rootView;
    }

...
...
...
}

预期输出是带有导航抽屉的活动,因此是一个片段。该片段包含这个主要活动ToolReaderActivity(因为它以前是一个活动而命名,因为它以前是一个活动),里面有1-2个片段。它是一个ListFragment,其中包含一个显示所选项目信息的片段(如果mDualPane为true,则为另一个片段,否则为单独的活动)。

我无法找到一种方法让它工作,我已经厌倦了使用onCreate,onCreateView,onActivityCreated来改变事情的顺序,但我不断得到的错误是mToolsListFragment.setContext中的mToolsListFragment为null (C);

1 个答案:

答案 0 :(得分:0)

您只需使用以下代码创建NavigationDrawer即可。

public class MainActivity extends AppCompatActivity
{
private NavigationView mNavigationView;
private DrawerLayout mDrawerLayout;
private Toolbar toolbar;
private ActionBarDrawerToggle mDrawerToggle;
private FragmentActivity fragment;
private FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initDrawerNavigation();
    fragmentManager = getFragmentManager();
}

public void initDrawerNavigation()
{
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle(R.string.app_name);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
    {

        @Override
        public boolean onNavigationItemSelected(MenuItem item)
        {
            item.setChecked(true);

            switch (item.getItemId())
            {
                case R.id.st_menu:
                    fragment = new FirstFragment();
                    break;

                case R.id.nd_menu:
                    fragment = new Second Fragment();
                    break;
            }

            fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();

            mDrawerLayout.closeDrawers();
            return true;
        }
    });

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open,
            R.string.drawer_close)
    {
        // Called when a drawer has settled in a completely closed state.
        public void onDrawerClosed(View view)
        {
            super.onDrawerClosed(view);
        }

        // Called when a drawer has settled in a completely open state.
        public void onDrawerOpened(View drawerView)
        {
            super.onDrawerOpened(drawerView);
        }
    };

    mDrawerLayout.addDrawerListener(mDrawerToggle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState)
{
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){

    switch(item.getItemId())
    {
        case android.R.id.home:
            mDrawerLayout.openDrawer(GravityCompat.START);  // OPEN DRAWER
            return true;
    }
    return super.onOptionsItemSelected(item);
}
}

这里有activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <include
        layout="@layout/your_layout" />

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        app:menu="@menu/navigation_items" />
</android.support.v4.widget.DrawerLayout>

</LinearLayout>

your_layout可以是您想要的任何内容。现在,您可以像这里一样定义FirstFragment类:

public class FirstFragment extends Fragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    ViewGroup view = (ViewGroup) inflater.inflate(R.layout.target_layout, container, false);

    return view;
}
}

如果要在活动而非片段之间切换,也可以参考此问题Navigation Drawer to switch between activities。希望这会有所帮助。