gpt4 book ai didi

android-actionbar - Android Navigation 组件 'Up button' 打开抽屉导航

转载 作者:行者123 更新时间:2023-12-03 23:13:50 25 4
gpt4 key购买 nike

我正在尝试使用带有导航图和导航 Controller 的导航组件来实现一个简单的导航。
我的 MainActivity 带有带有汉堡包图标和应用程序标题、抽屉导航和内容区域的操作栏。

内容区域是我展示不同片段的地方。

内容区域首先加载我的 homescreenFragment,其中包含 4 个按钮(新闻、事件、时间表、配置文件)——每个按钮都将片段加载到我的 MainActivity 的内容区域中。
它工作正常,唯一的问题是:
当我按下 新闻按钮并从我的 导航主屏幕 到我的 新闻片段它加载片段 OK 并将操作栏标题更改为新闻。汉堡图标更改为“向上按钮”图标,但是当我按下它时,应用程序会打开抽屉导航,就像我按下汉堡图标而不是导航回主屏幕一样。

如何使向上按钮向上而不显示左侧抽屉导航?

这是我的 MainActivity.java:

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

public class MainActivity extends AppCompatActivity {

Toolbar m_toolbar;
DrawerLayout m_drawer;
ActionBarDrawerToggle m_toggle;
NavigationView m_navigationView;
NavController m_navController;

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

setContentView(R.layout.activity_main);

m_toolbar = findViewById(R.id.toolbar);
setSupportActionBar(m_toolbar);

m_drawer = findViewById(R.id.drawer_layout);

m_navigationView = findViewById(R.id.nav_drawer_view);

m_navController = Navigation.findNavController(this, R.id.content);

m_toggle = new ActionBarDrawerToggle(
this, m_drawer, m_toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
m_drawer.addDrawerListener(m_toggle);
//m_toggle.syncState();

//connect actionbar icon and drawer with nav controller
NavigationUI.setupWithNavController(m_navigationView, m_navController);
NavigationUI.setupActionBarWithNavController(this, m_navController);
NavigationUI.setupActionBarWithNavController(this, m_navController, m_drawer);

}

@Override
public boolean onSupportNavigateUp() {
return Navigation.findNavController(this, R.id.nav_drawer_view).navigateUp() || super.onSupportNavigateUp();
}

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


}

我的activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<LinearLayout
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

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

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

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"/>

</FrameLayout>

</LinearLayout>

<android.support.design.widget.NavigationView
android:id="@+id/nav_drawer_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />

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

导航图:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
app:startDestination="@id/homeScreenFragment">

<fragment
android:id="@+id/homeScreenFragment"
android:name="com.mysample.meganews.Fragments.HomeScreenFragment"
android:label="@string/app_name"
tools:layout="@layout/home_screen_fragment" >
<action
android:id="@+id/action_to_news"
app:destination="@id/newsFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim" />
<action
android:id="@+id/action_to_events"
app:destination="@id/eventsFragment"
app:enterAnim="@anim/nav_default_pop_enter_anim"
app:exitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/newsFragment"
android:name="com.mysample.meganews.Fragments.NewsFragment"
android:label="@string/hs_title_news"
tools:layout="@layout/news_fragment" />
<fragment
android:id="@+id/eventsFragment"
android:name="com.mysample.meganews.Fragments.EventsFragment"
android:label="events_fragment"
tools:layout="@layout/events_fragment" />
</navigation>

HomeScreenFragment.java 文件:
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;


/**
* A simple {@link Fragment} subclass.
*/
public class HomeScreenFragment extends Fragment {


public HomeScreenFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.home_screen_fragment, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

RelativeLayout rl_news = view.findViewById(R.id.ll_news_btn);
RelativeLayout rl_events = view.findViewById(R.id.ll_events_btn);

rl_news.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_to_news));
rl_events.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_to_events));
}
}

build.gradle 文件:
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
applicationId "com.mysample.meganews"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'android.arch.lifecycle:extensions:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha11'
implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha11'
}

最佳答案

这个问题是由于不同 API 的混合造成的。这是正确的代码:

事件_main.xml:


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

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark" />
<fragment
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"/>

</LinearLayout>

<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_drawer_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

MainActivity.java:
import android.os.Bundle;


import com.google.android.material.navigation.NavigationView;

import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

import android.view.MenuItem;
import android.view.ViewParent;

public class MainActivity extends AppCompatActivity
{
private Toolbar mToolbar;
private DrawerLayout mDrawer;
private ActionBarDrawerToggle mToggle;
private NavigationView mNavigationView;
private NavController mNavController;

private MainViewModel mViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
mViewModel = ViewModelProviders.of(this).get(MainViewModel.class);

setupNavigation();
}

private void setupNavigation()
{
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

mDrawer = findViewById(R.id.drawerLayout);
mNavigationView = findViewById(R.id.nav_drawer_view);

mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {

// // we need this line to handle the navigation
// boolean handled = NavigationUI.onNavDestinationSelected(menuItem, mNavController);
// if (handled) {
// ViewParent parent = mNavigationView.getParent();
// if (parent instanceof DrawerLayout) {
// ((DrawerLayout) parent).closeDrawer(mNavigationView);
// }
// }

return true;
}
});

mNavController = Navigation.findNavController(this, R.id.content);
NavigationUI.setupActionBarWithNavController(this, mNavController, mDrawer);
// Tie nav graph to items in nav drawer
NavigationUI.setupWithNavController(mNavigationView, mNavController);
}


@Override
public boolean onSupportNavigateUp() {
//return Navigation.findNavController(this, R.id.content).navigateUp();
//return mNavController.navigateUp() || super.onSupportNavigateUp();
return NavigationUI.navigateUp(mNavController, mDrawer);
}

@Override
public void onBackPressed() {
if (mDrawer.isDrawerOpen(GravityCompat.START)) {
mDrawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

关于android-actionbar - Android Navigation 组件 'Up button' 打开抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54461326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com