首页 > 解决方案 > 当我在导航抽屉外单击时,图标不会改变

问题描述

我正在创建一个 Android 应用程序并实现了导航抽屉,但我遇到了问题。我创建了一个 view_headline 按钮,并在右侧添加了它,具有打开或关闭导航抽屉的功能。当我打开导航抽屉时,图标变为后退箭头。但是当导航抽屉打开并且我在它外部(在活动中)单击时,它会关闭,但图标仍然是箭头并且不会更改为 view_headline。有人能帮我吗?非常感谢

我的代码是这样的:

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="@drawable/fundo"
    tools:context=".MainActivity">

    <include
        layout="@layout/navigationdrawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbar"
        android:background="@color/colorPrimary">

        <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@android:color/white"
                android:gravity="left"
                android:minHeight="?actionBarSize"
                android:padding="@dimen/appbar_padding"
                android:text="@string/app_name"
                android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />

            <ImageButton
                android:id="@+id/definicoes"
                android:layout_width="100px"
                android:layout_height="100px"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_gravity="right"
                android:onClick="abrirDefinicoes"
                android:layout_marginTop="10dp"
                android:layout_marginEnd="17dp"
                android:layout_marginRight="17dp"
                android:layout_marginBottom="19dp"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_definicoes" />

    </androidx.appcompat.widget.Toolbar>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

导航抽屉.xml

 <?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:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="730dp"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="65dp">
    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        app:itemTextColor="@android:color/darker_gray"
        app:itemIconTint="@android:color/darker_gray"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:headerLayout="@layout/header"
        app:menu="@menu/drawermenu"
        android:layout_gravity="end"
        />

</androidx.drawerlayout.widget.DrawerLayout>

MainActivity.java

 public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private ImageButton btn;
    private NavigationView navigationView;

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

        btn = (ImageButton) findViewById(R.id.definicoes);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.Open, R.string.Close);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

        navigationView = (NavigationView) findViewById(R.id.navigationView);
        navigationView.setNavigationItemSelectedListener(this);

    }


    //when you click on one of the Navigation drawer items
    @Override
    public boolean onNavigationItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.contactos:
                System.out.println("CONTACTOS!!!!!!!!!");

        }
       return true;
    }

    //open navigation drawer
    public void abrirDefinicoes(View v) {

        // change icon when going back and opening settings

        if (drawerLayout.isDrawerOpen(GravityCompat.END)) {
            drawerLayout.closeDrawer(GravityCompat.END);
            btn.setImageResource(R.drawable.ic_definicoes);
        } else {
            drawerLayout.openDrawer(GravityCompat.END);
            btn.setImageResource(R.drawable.ic_seta_voltar_atras_48dp);
        }

    }

}

结果

连接APP后

点击 view_headline 后

单击导航抽屉外部后

标签: androidnavigation-drawer

解决方案


推荐阅读