当我试图点击任何菜单项时,为什么导航抽屉会关闭?

rslzwgfq  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(248)

当我点击汉堡包图标时,我的导航抽屉会打开,但当我点击菜单项时,它只会关闭抽屉,不会带我去看片段。在我将appbarconfiguration添加到主活动并添加片段之前,它工作得非常好。以下是我的导航布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/space120"
        android:clickable="true"
        android:foreground="?selectableItemBackground"
        tools:layout_height="@dimen/space120">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.android.volley.toolbox.NetworkImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/side_nav_bar"
                android:scaleType="centerCrop"
                tools:ignore="ContentDescription" />

        </FrameLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/navigation_drawer_account_information"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/space100"
                android:layout_alignParentBottom="false"
                android:paddingStart="@dimen/space30"
                android:paddingLeft="@dimen/space30"
                android:paddingRight="@dimen/space0"
                android:paddingEnd="@dimen/space0"
                android:paddingTop="35dp"
                android:layout_centerVertical="true"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/nav_bar_display_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/text_size_20"
                    android:textColor="@color/white"
                    android:text="@string/placeholder"
                    tools:fontFamily="sans-serif-medium"/>

                <TextView
                    android:id="@+id/nav_bar_display_email"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/text_size_14"
                    android:textColor="@color/white"
                    android:text="@string/placeholder"
                    tools:fontFamily="sans-serif-medium"
                    android:paddingTop="@dimen/space10"/>

            </LinearLayout>

            <ImageView
                android:layout_width="@dimen/space64"
                android:layout_height="@dimen/space64"
                android:visibility="gone"
                android:layout_above="@id/navigation_drawer_account_information"
                android:src="@drawable/ic_instructions"
                android:contentDescription="@string/app_name"/>

        </RelativeLayout>

    </FrameLayout>

    <!-- Drawer items -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/navigation_drawer_background"
        android:clipToPadding="false"
        android:paddingBottom="@dimen/space8"
        android:paddingTop="@dimen/space8">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <FrameLayout
                android:id="@+id/instructions"
                style="@style/NavDrawerRowLayout">

                <com.koupr.coinr.views.TintOnStateImageView
                    style="@style/NavDrawerRowItemIcon"
                    android:src="@drawable/ic_instructions"
                    tools:ignore="ContentDescription"/>

                <TextView
                    style="@style/NavDrawerRowItemText"
                    android:text="@string/instructions"
                    tools:fontFamily="sans-serif-medium"/>

            </FrameLayout>

            <FrameLayout
                android:id="@+id/refer"
                style="@style/NavDrawerRowLayout">

                <com.koupr.coinr.views.TintOnStateImageView
                    style="@style/NavDrawerRowItemIcon"
                    android:src="@drawable/ic_refer"
                    tools:ignore="ContentDescription"/>

                <TextView
                    style="@style/NavDrawerRowItemText"
                    android:text="@string/refer"
                    tools:fontFamily="sans-serif-medium"/>

            </FrameLayout>

            <FrameLayout
                android:id="@+id/redeem"
                style="@style/NavDrawerRowLayout">

                <com.koupr.coinr.views.TintOnStateImageView
                    style="@style/NavDrawerRowItemIcon"
                    android:src="@drawable/ic_redeem"
                    tools:ignore="ContentDescription"/>

                <TextView
                    style="@style/NavDrawerRowItemText"
                    android:text="@string/redeem"
                    tools:fontFamily="sans-serif-medium"/>

            </FrameLayout>

            <FrameLayout
                android:id="@+id/nav_transactions"
                style="@style/NavDrawerRowLayout">

                <com.koupr.coinr.views.TintOnStateImageView
                    style="@style/NavDrawerRowItemIcon"
                    android:src="@drawable/ic_user_redeem"
                    tools:ignore="ContentDescription"/>

                <TextView
                    style="@style/NavDrawerRowItemText"
                    android:text="@string/transactions"
                    tools:fontFamily="sans-serif-medium"/>

            </FrameLayout>

            <FrameLayout
                android:id="@+id/about"
                style="@style/NavDrawerRowLayout">

                <com.koupr.coinr.views.TintOnStateImageView
                    style="@style/NavDrawerRowItemIcon"
                    android:src="@drawable/ic_about"
                    tools:ignore="ContentDescription"/>

                <TextView
                    style="@style/NavDrawerRowItemText"
                    android:text="@string/about"
                    tools:fontFamily="sans-serif-medium"/>

            </FrameLayout>

            <View android:layout_width="match_parent"
                android:layout_height="@dimen/space1"
                android:layout_marginBottom="@dimen/space8"
                android:layout_marginTop="@dimen/space8"
                android:background="@color/nav_divider"/>

            <FrameLayout
                android:id="@+id/nav_share"
                style="@style/NavDrawerRowLayout">

                <com.koupr.coinr.views.TintOnStateImageView
                    style="@style/NavDrawerRowItemIcon"
                    android:src="@drawable/ic_share"
                    tools:ignore="ContentDescription"/>

                <TextView
                    style="@style/NavDrawerRowItemText"
                    android:text="@string/share"
                    tools:fontFamily="sans-serif-medium"/>

            </FrameLayout>

            <FrameLayout
                android:id="@+id/rate_this_app"
                style="@style/NavDrawerRowLayout">

                <com.koupr.coinr.views.TintOnStateImageView
                    style="@style/NavDrawerRowItemIcon"
                    android:src="@drawable/ic_rate"
                    tools:ignore="ContentDescription"/>

                <TextView
                    style="@style/NavDrawerRowItemText"
                    android:text="@string/rate_this_app"
                    tools:fontFamily="sans-serif-medium"/>

            </FrameLayout>

            <FrameLayout
                android:id="@+id/policy"
                style="@style/NavDrawerRowLayout">

                <com.koupr.coinr.views.TintOnStateImageView
                    style="@style/NavDrawerRowItemIcon"
                    android:src="@drawable/ic_policy"
                    tools:ignore="ContentDescription"/>

                <TextView
                    style="@style/NavDrawerRowItemText"
                    android:text="@string/policy"
                    tools:fontFamily="sans-serif-medium"/>

            </FrameLayout>

            <FrameLayout
                android:id="@+id/contact"
                style="@style/NavDrawerRowLayout">

                <com.koupr.coinr.views.TintOnStateImageView
                    style="@style/NavDrawerRowItemIcon"
                    android:src="@drawable/ic_contact"
                    tools:ignore="ContentDescription"/>

                <TextView
                    style="@style/NavDrawerRowItemText"
                    android:text="@string/contact"
                    tools:fontFamily="sans-serif-medium"/>

            </FrameLayout>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

以下是我主要活动的导航抽屉代码部分以及appbarconfig:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = this;

    init_v3();

    BottomNavigationView navView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_home, R.id.nav_tasks)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
    NavigationUI.setupWithNavController(navView, navController);

}

//这里有些不相关的代码

void initViews(){

    Toolbar toolbar = findViewById(R.id.tool_bar);

    setSupportActionBar(toolbar);
    int Numboftabs = 1;

    if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
        toolbar.setElevation(4);
    }

    ViewPager pager = findViewById(R.id.pager);
    SlidingTabLayout tabs = findViewById(R.id.tabs);
    CharSequence Titles[] = {getResources().getString(R.string.home)};
    adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);

    if(getInstance().get("APP_TABS_ENABLE",false)){

        Numboftabs = 2;
        CharSequence Titles2[] = {getResources().getString(R.string.home), getResources().getString(R.string.transactions)};
        adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles2,Numboftabs);
        tabs.setVisibility(View.VISIBLE);

        if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            tabs.setElevation(4);
        }

    }

    pager.setAdapter(adapter);
    tabs.setDistributeEvenly(true);
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.tabsScrollColor);
        }
    });
    tabs.setViewPager(pager);

    // Navigation Drawer
    mDrawerLayout = findViewById(R.id.main_activity_DrawerLayout);
    mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.colorPrimary));
    mScrimInsetsFrameLayout = findViewById(R.id.navigation_drawer_Layout);

    mActionBarDrawerToggle = new ActionBarDrawerToggle(context,mDrawerLayout,toolbar,R.string.navigation_drawer_opened,R.string.navigation_drawer_closed)
    {   @Override
        public void onDrawerSlide(View drawerView, float slideOffset)
        {
            // Disables the burger/arrow animation by default
            super.onDrawerSlide(drawerView, 0);
        }
    };

    mDrawerLayout.addDrawerListener(mActionBarDrawerToggle);

    if (getSupportActionBar() != null)
    {
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setHomeButtonEnabled(false);
        getSupportActionBar().setTitle(R.string.app_name);
    }

    mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);

    if(getInstance().get("APP_NAVBAR_ENABLE",true)){

        Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.menu_icon, context.getTheme());
        mActionBarDrawerToggle.setHomeAsUpIndicator(drawable);
        mActionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mDrawerLayout.isDrawerVisible(GravityCompat.START)){
                    mDrawerLayout.closeDrawer(GravityCompat.START);
                }else{
                    mDrawerLayout.openDrawer(GravityCompat.START);
                }
            }
        });

    }else{
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    }

    mActionBarDrawerToggle.syncState();

    // Navigation Drawer layout width
    int possibleMinDrawerWidth = AppUtils.getScreenWidth(context) - UtilsMiscellaneous.getThemeAttributeDimensionSize(context, android.R.attr.actionBarSize);
    int maxDrawerWidth = getResources().getDimensionPixelSize(R.dimen.space280);
    mScrimInsetsFrameLayout.getLayoutParams().width = Math.min(possibleMinDrawerWidth, maxDrawerWidth);

}

void refreshDisplay(){

    TextView fullname = findViewById(R.id.nav_bar_display_name);
    TextView email = findViewById(R.id.nav_bar_display_email);

    fullname.setText(getInstance().getFullname());
    email.setText(getInstance().getEmail());

}

void initNavDrawer() {

    invalidateOptionsMenu();

    // Instructions
    FrameLayout instructions = findViewById(R.id.instructions);
    if(!getInstance().get("INSTRUCTIONS_ACTIVE",true)){ instructions.setVisibility(View.GONE); }
    instructions.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            openInstructions();
            mDrawerLayout.closeDrawers();
        }
    });

    // Refer & Earn
    FrameLayout refer = findViewById(R.id.refer);
    if(!getInstance().get("REFER_ACTIVE",true)){ refer.setVisibility(View.GONE); }
    refer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            openRefer();
            mDrawerLayout.closeDrawers();
        }
    });

    // Redeem
    FrameLayout redeem = findViewById(R.id.redeem);
    redeem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            openRedeem();
            mDrawerLayout.closeDrawers();
        }
    });

    // About
    FrameLayout about = findViewById(R.id.about);
    about.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            openAbout();
            mDrawerLayout.closeDrawers();
        }
    });

    // Transactions
    FrameLayout reward_his = findViewById(R.id.nav_transactions);
    reward_his.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            openTransactions();
            mDrawerLayout.closeDrawers();

        }
    });

    // Share
    FrameLayout share = findViewById(R.id.nav_share);
    if(!getInstance().get("SHARE_APP_ACTIVE",true)){ share.setVisibility(View.GONE); }
    share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AppUtils.shareApplication(context);
            mDrawerLayout.closeDrawers();
        }
    });

    // Rate App
    FrameLayout rate = findViewById(R.id.rate_this_app);
    if(!getInstance().get("RATE_APP_ACTIVE",true)){ rate.setVisibility(View.GONE); }
    rate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AppUtils.gotoMarket(context);
            mDrawerLayout.closeDrawers();
        }
    });

    // Privacy Policy
    FrameLayout policy = findViewById(R.id.policy);
    if(!getInstance().get("POLICY_ACTIVE",true)){ policy.setVisibility(View.GONE); }
    policy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            parseURL(getInstance().get("APP_POLICY_URL",""));
            mDrawerLayout.closeDrawers();
        }
    });

    // Contact Us
    FrameLayout contact = findViewById(R.id.contact);
    if(!getInstance().get("CONTACT_US_ACTIVE",true)){ contact.setVisibility(View.GONE); }
    contact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            parseURL(getInstance().get("APP_CONTACT_US_URL",""));
            mDrawerLayout.closeDrawers();
        }
    });

}

}
什么问题???

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题