Android 用片段替换NavigationView

Android 用片段替换NavigationView,android,android-fragments,android-toolbar,navigationview,Android,Android Fragments,Android Toolbar,Navigationview,我正在用片段替换NavigationView,并进一步处理该视图。(因为NavigationView是FrameLayout的子级) 单击导航视图中的任何项目时。。我用一个片段覆盖导航视图。如果有人点击后退按钮。。我想回到导航视图 用片段替换NavigationView时,我放置了一个工具栏 问题 a。工具栏显示了一个“返回”链接,但它不起作用 b。工具栏显示“设置”菜单-我不想在片段上显示这个。但是我仍然需要MainActivity工具栏上的“设置”菜单 c。工具栏似乎位于状态栏的下方-如何将

我正在用片段替换NavigationView,并进一步处理该视图。(因为NavigationView是FrameLayout的子级)

单击导航视图中的任何项目时。。我用一个片段覆盖导航视图。如果有人点击后退按钮。。我想回到导航视图

用片段替换NavigationView时,我放置了一个工具栏

问题

a。工具栏显示了一个“返回”链接,但它不起作用

b。工具栏显示“设置”菜单-我不想在片段上显示这个。但是我仍然需要MainActivity工具栏上的“设置”菜单

c。工具栏似乎位于状态栏的下方-如何将工具栏放置在片段中状态栏的下方

已尝试选项

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

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    ItemsFragment fragment = new ItemsFragment();
    android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.nav_view, fragment);
//                fragmentTransaction.setTransition();
    fragmentTransaction.addToBackStack("LeftMainNavDrawer");
    fragmentTransaction.commit();

//        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
//        drawer.closeDrawer(GravityCompat.START);
    return true;
}
a。MainActivity的OnBackPressed方法-fragmentManager.popUpStack()

b。片段-fragmentManager.popUpStack()的onoptionItemSelected()方法

c。在工具栏上的onClick方法中创建了一个侦听器:fragmentManager.popUpStack()

截图:

这是我的密码:

MainActivity:>onNavigationItemSelected()

片段

package com.example.deep_kulshreshtha.expandablelistnavdrawer;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.deep_kulshreshtha.expandablelistnavdrawer.dummy.DummyContent;
import com.example.deep_kulshreshtha.expandablelistnavdrawer.dummy.DummyContent.DummyItem;

import java.util.List;

/**
 * A fragment representing a list of Items.
 * <p/>
 * Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener}
 * interface.
 */
public class ItemsFragment extends Fragment {

    // TODO: Customize parameter argument names
    private static final String ARG_COLUMN_COUNT = "column-count";
    // TODO: Customize parameters
    private int mColumnCount = 1;
    private OnListFragmentInteractionListener mListener;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public ItemsFragment() {
    }

    // TODO: Customize parameter initialization
    @SuppressWarnings("unused")
    public static ItemsFragment newInstance(int columnCount) {
        ItemsFragment fragment = new ItemsFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_COLUMN_COUNT, columnCount);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
        }

        setHasOptionsMenu(false);

    }

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

        View view = inflater.inflate(R.layout.fragment_item_list, container, false);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);

        // Set the adapter
//        if (view instanceof RecyclerView) {
            Context context = view.getContext();
//            RecyclerView recyclerView = (RecyclerView) view;

            if (mColumnCount <= 1) {
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
            } else {
                recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
            }

            recyclerView.setAdapter(new MyItemRecyclerViewAdapter(DummyContent.ITEMS, mListener));
//        }

        Toolbar toolbar = (Toolbar) view.findViewById(R.id.itemsToolbarNavDrawer);
        /*toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "Inside Toolbar click", Toast.LENGTH_LONG).show();
            }
        });*/
        AppCompatActivity appCompatActivity = (AppCompatActivity) getActivity();
        appCompatActivity.setSupportActionBar(toolbar);
        appCompatActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        appCompatActivity.getSupportActionBar().setDisplayShowHomeEnabled(true);

        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.removeItem(R.id.action_settings);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnListFragmentInteractionListener) {
            mListener = (OnListFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnListFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnListFragmentInteractionListener {
        // TODO: Update argument type and name
        void onListFragmentInteraction(DummyItem item);
    }
}
package com.example.deep_kulshreshtha.expandablelistnavdrawer;
导入android.content.Context;
导入android.os.Bundle;
导入android.support.annotation.Nullable;
导入android.support.v4.app.Fragment;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.GridLayoutManager;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入android.support.v7.widget.Toolbar;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.MenuInflater;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Toast;
导入com.example.deep_kulshreshtha.expandablelistnavdrawer.dummy.DummContent;
导入com.example.deep_kulshreshtha.expandablelistnavdrawer.dummy.DummContent.DummyItem;
导入java.util.List;
/**
*表示项目列表的片段。
*

*包含此片段的活动必须实现{@link OnListFragmentInteractionListener} *接口。 */ 公共类ItemsFragment扩展了片段{ //TODO:自定义参数参数名称 私有静态最终字符串ARG\u COLUMN\u COUNT=“COLUMN COUNT”; //TODO:自定义参数 私有int mColumnCount=1; 私有OnListFragmentInteractionListener-MLListener; /** *片段管理器实例化 *碎片(如屏幕方向改变时)。 */ 公共项目片段(){ } //TODO:自定义参数初始化 @抑制警告(“未使用”) 公共静态项片段newInstance(int columnCount){ ItemsFragment片段=新的ItemsFragment(); Bundle args=新Bundle(); args.putInt(ARG\u COLUMN\u COUNT,columnCount); fragment.setArguments(args); 返回片段; } @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); 如果(getArguments()!=null){ mColumnCount=getArguments().getInt(ARG\u COLUMN\u COUNT); } 设置选项菜单(错误); } @凌驾 创建视图上的公共视图(布局、充气机、视图组容器、, Bundle savedInstanceState){ 视图=充气机。充气(R.layout.fragment\u item\u list,container,false); RecyclerView RecyclerView=(RecyclerView)view.findViewById(R.id.list); //设置适配器 //如果(查看RecyclerView的实例){ Context=view.getContext(); //RecyclerView RecyclerView=(RecyclerView)视图;


如果(mColumnCount在主活动中添加1个框架布局并将其id作为容器,那么在java代码中

fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_容器,new HomeFragment());

fragmentTransaction.commit();
在主活动中添加1个框架布局并将其id作为容器。然后在java代码中

fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_容器,new HomeFragment());

fragmentTransaction.commit();
我想出了一个解决方案..看起来“Toolbar”类对于整个应用程序只有一个实例

当我使用活动的onOptionsItemSelected方法时…我得到了控件。在这里,我可以弹出Upstack()并转到NavigationView

#
还没有弄清楚如何在状态栏下保持布局。:android:fitsystemwindows=“true”…似乎不起作用。

我想出了一个解决方案。.看起来“Toolbar”类对整个应用程序只有一个实例

当我使用活动的onOptionsItemSelected方法时…我得到了控件。在这里,我可以弹出Upstack()并转到NavigationView

#
还需要弄清楚如何将布局保持在状态栏下。:android:fitsystemwindows=“true”…似乎不起作用。

想法是用片段替换导航视图-这是可行的。不起作用的是…从片段返回导航视图的返回按钮。fragmentTransaction.addToBackStack(“null”);是的,这没有帮助。我想了解的是……如果我们从片段中按下后退按钮->我们可以转到原始框架布局吗?是的,我们可以,但我用不同的方式完成。这是主活动。我给空容器,每当我执行事务时,我都用我想要的片段替换容器。想法是替换Na带片段的vigationView-这正在工作。不工作的是…返回按钮从片段到NavigationView.fragmentTransaction.addToB
<LinearLayout 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:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/itemsToolbarNavDrawer"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.v7.widget.RecyclerView

        android:id="@+id/list"
        android:name="com.example.deep_kulshreshtha.expandablelistnavdrawer.ItemsFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        app:layoutManager="LinearLayoutManager"
        tools:context="com.example.deep_kulshreshtha.expandablelistnavdrawer.ItemsFragment"
        tools:listitem="@layout/fragment_item"
        android:background="@android:color/white"/>

</LinearLayout>