带有底部导航视图的导航抽屉布局

时间:2019-09-30 07:22:10

标签: android navigation-drawer android-navigationview android-bottomnavigationview

AOA, 我遇到了一个问题,但在其中进行了很多搜索,但找不到合适的答案, 我正在使用NavigationView(抽屉布局)和BottomNavigationView 当我从DrawerLayout屏幕中选择一个项目时,但未选择底部导航项时, 我在MainActivity中使用了BottomNavigationView,在MenuFragment中使用了DrawerLayout,这就是为什么我不能使用bottomnavigation.selectitemid();的原因 我该如何解决这个问题?

这是我的MainActivity + Main XML

public class MainActivity extends AppCompatActivity  {


    BottomNavigationView bottomNavigationView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("Find", "Main Activity is Running");
        bottomNavigationView = (BottomNavigationView) findViewById(R.id.BottomNavView_id);
        bottomNavigationView.setSelectedItemId(R.id.menu_All_id);
        getSupportFragmentManager().beginTransaction().replace(R.id.FrameLayout_id, new All()).commit();
        BottomNavigationView.OnNavigationItemSelectedListener ItemSelecter = new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                Fragment SelectFragment = null;
                Log.i("Find", "Main Activity botom navigation method is Running");

                switch (menuItem.getItemId()) {

                    case R.id.menu_All_id :
                            SelectFragment = new All();

                        break;
                    case R.id.menu_Poets_id:
                        SelectFragment = new Menu();
                        break;

                    case R.id.menu_Setting_id:
                        SelectFragment = new Setting();
                        break;
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.FrameLayout_id, SelectFragment).commit();
                return true;
            }
        };
        bottomNavigationView.setOnNavigationItemSelectedListener(ItemSelecter);
    }
}



 /////////////////////////////   Here is My Main XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:background="#000"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <FrameLayout android:id="@+id/FrameLayout_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        android:layout_above="@id/BottomNavView_id"
        xmlns:android="http://schemas.android.com/apk/res/android" /> 

    <android.support.design.widget.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:id="@+id/BottomNavView_id"
        app:menu="@menu/bottomnavigation_items"
        android:background="@color/White"
        app:labelVisibilityMode="labeled"
        />
</RelativeLayout>

这是菜单片段Java类+菜单XML

package com.example.pocketcompanies.sufiquotes;

import android.content.Context;
import android.content.Intent;
import android.drm.DrmStore;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.CheckableImageButton;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.Checkable;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;

public class Menu extends Fragment{


    RecyclerView recyclerView;
    MenuRecyclerViewAdapter Adapter;
    List<MenuGetterSetter> MenuGT;
    DrawerLayout drawerLayout;
    android.support.v7.widget.Toolbar menutoolbar;
    NavigationView navigationView;


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_menu, container, false);
        drawerLayout = (DrawerLayout) v.findViewById(R.id.Menu_DrawerLayout_id);
        navigationView = (NavigationView) v.findViewById(R.id.Menu_NavigationView_id);
        MenuGT = new ArrayList<>();
        recyclerView = (RecyclerView) v.findViewById(R.id.Menu_RV);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(v.getContext()));
        MenuGT.add(new MenuGetterSetter("Smile", R.drawable.ic_navigate_next_black_24dp, R.id.menulayout_id));
        MenuGT.add(new MenuGetterSetter("Attitude", R.drawable.ic_navigate_next_black_24dp, R.id.menulayout_id));
        MenuGT.add(new MenuGetterSetter("Confidence", R.drawable.ic_navigate_next_black_24dp, R.id.menulayout_id));
        MenuGT.add(new MenuGetterSetter("Love", R.drawable.ic_navigate_next_black_24dp, R.id.menulayout_id));
        MenuGT.add(new MenuGetterSetter("Sad", R.drawable.ic_navigate_next_black_24dp, R.id.menulayout_id));
        MenuGT.add(new MenuGetterSetter("Failure", R.drawable.ic_navigate_next_black_24dp, R.id.menulayout_id));
        MenuGT.add(new MenuGetterSetter("Motivational", R.drawable.ic_navigate_next_black_24dp, R.id.menulayout_id));
        MenuGT.add(new MenuGetterSetter("Happiness", R.drawable.ic_navigate_next_black_24dp, R.id.menulayout_id));
        Adapter = new MenuRecyclerViewAdapter(v.getContext(), MenuGT);
        recyclerView.setAdapter(Adapter);
        menutoolbar = (android.support.v7.widget.Toolbar) v.findViewById(R.id.poetToolbar_id);
        // Drawer Coding
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, menutoolbar, R.string.Navigation_drawer_open, R.string.Navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);

        toggle.syncState();
        // click listener




        /////
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem)
    {
        Fragment fragment = null;
        switch (menuItem.getItemId())
        {
            case R.id.Nav_TrendingQuote_id:
                fragment = new All();
                break;
            case R.id.Nav_Menu_id :
                fragment = new Menu();
                break;
            case R.id.Nav_Setting_id :
                fragment = new Setting();
               break;
            case R.id.Nav_RateUs_id :
                Intent rateusintent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getActivity().getPackageName()));
                startActivity(rateusintent);
                break;
            case R.id.Nav_DownloadOtherApp_id :
                Intent doimtent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getActivity().getPackageName()));
                startActivity(doimtent);
                break;
            case R.id.Nav_AboutUs_id :
                Intent aboutusintent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getActivity().getPackageName()));
                startActivity(aboutusintent);
                break;
        }

        getChildFragmentManager().beginTransaction().replace(R.id.MenuFrameLayout_id, fragment).commit();
                drawerLayout.closeDrawer(GravityCompat.START);



        return true;
    }
});


        return v;

    }


}




////////////////////////////////////  Here is Menu XML




<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/Menu_DrawerLayout_id"
    tools:context=".Menu"
    tools:openDrawer="start">

    <FrameLayout
        android:id="@+id/MenuFrameLayout_id"
        android:layout_width="match_parent"
        android:layout_height="512dp"
        android:layout_above="@id/BottomNavView_id"
        >
        <RelativeLayout 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:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            tools:context=".Menu">


            <android.support.v7.widget.Toolbar
                android:id="@+id/poetToolbar_id"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary">

                <TextView
                    android:id="@+id/Poets_textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="40sp"
                    android:text="Menu "
                    android:textColor="@color/White"
                    android:textSize="30sp" />

            </android.support.v7.widget.Toolbar>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/Menu_RV"
                android:layout_width="match_parent"

                android:layout_height="wrap_content"
                android:layout_below="@id/poetToolbar_id"
                android:layout_marginBottom="10sp">


            </android.support.v7.widget.RecyclerView>
        </RelativeLayout>
    </FrameLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/Menu_NavigationView_id"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@id/poetToolbar_id"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:itemIconTint="@color/TextColorRed"
        app:itemTextColor="@color/Black"
        app:menu="@menu/navigationmenu"



        >


    </android.support.design.widget.NavigationView>


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

0 个答案:

没有答案