繁体   English   中英

导航抽屉:如何使用自定义工具栏设置导航项选择的侦听器?

[英]Navigation drawer: How do I set the navigation item selected listener with custom tool bar?

打开导航抽屉时我没有看到任何错误,但每当我 select 一个项目时它就会一直关闭。 编辑:我的抽屉在选择项目时自动关闭,而不显示NavigationItemSelectedListener中提到的Toast 消息 不显示 toast 消息。 我使用了一个自定义工具栏,我在其中添加了一个图像视图作为侧边菜单按钮。

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
tools:context=".CustomToolbar"
android:id="@+id/drawer_layout">

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:headerLayout="@layout/header_file"
        app:menu="@menu/main_manu"
        android:layout_gravity="start"/>
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bggradient">
    
            <include
                android:id="@+id/toolbar"
                layout="@layout/layout_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
</androidx.drawerlayout.widget.DrawerLayout>

public class CustomToolbar extends AppCompatActivity {

    private Toolbar toolbar;
    private DrawerLayout drawer;
    private NavigationView navigationView;
    private ActionBarDrawerToggle toggle;
    private ImageView side_menu;
    
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (toggle.onOptionsItemSelected(item)){
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_toolbar);
    
        toolbar = (Toolbar) findViewById(R.id.navigation_bar);
        side_menu = findViewById(R.id.side_menu);
        drawer = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.navigation_view);
    
        setSupportActionBar(toolbar);
        toggle = new ActionBarDrawerToggle(this, drawer, R.string.open, R.string.close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
    
        side_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawer.openDrawer(Gravity.LEFT);
            }
        });
    
            navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.home_menu: {
                            Toast.makeText(CustomToolbar.this, "Home Selected", Toast.LENGTH_SHORT).show();
                        }
                        case R.id.community_menu: {
                            Toast.makeText(CustomToolbar.this, "Community Selected", Toast.LENGTH_SHORT).show();
                        }
                        case R.id.posts_menu: {
                            Toast.makeText(CustomToolbar.this, "Posts Selected", Toast.LENGTH_SHORT).show();
                        }
                        case R.id.website_menu: {
                            Toast.makeText(CustomToolbar.this, "Website Selected", Toast.LENGTH_SHORT).show();
                        }
                        case R.id.share_menu: {
                            Toast.makeText(CustomToolbar.this, "Share Selected", Toast.LENGTH_SHORT).show();
                        }
                        case R.id.feedback_menu: {
                            Toast.makeText(CustomToolbar.this, "Feedback Selected", Toast.LENGTH_SHORT).show();
                        }
                        case R.id.about_menu: {
                            Toast.makeText(CustomToolbar.this, "About us Selected", Toast.LENGTH_SHORT).show();
                        }
                        case R.id.logout_menu: {
                            Toast.makeText(CustomToolbar.this, "Logout Selected", Toast.LENGTH_SHORT).show();
                        }
                    }
                    return false;
                }
            });
    }
    
    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)){
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

我已经尝试使用默认工具栏的其他方法,它工作正常,但我无法找出自定义工具栏的问题。 我想在导航抽屉内显示对项目选择的祝酒词。 请帮我找到解决方案...

谢谢你。

onNavigationItemSelected中,更改此

return false;

return true;

经过这么长时间,我终于找到了解决问题的方法。 但我不知道为什么会这样,但我很高兴它终于奏效了。 我也很高兴与那些将来可能遇到同样问题的人分享它,我希望他们不会。

我参考了来自 Git 集线器的链接以获得此解决方案。 链接: https://github.com/priyalbhatewara123/Navigation-Drawer--Android

最后这是我找到的解决方案,

我刚刚在我的activity_custom_toolbar.xml中使用的有问题的代码中的<ScrollView>上方的</ScrollView>下方添加了以下代码行。

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

我也在更改后添加代码,

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
    tools:context=".CustomToolbar"
    android:id="@+id/drawer_layout">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bggradient">

            <include
                android:id="@+id/toolbar"
                layout="@layout/layout_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

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

</androidx.drawerlayout.widget.DrawerLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM