android - 抽屉导航在 Activity 开始时打开

标签 android

我的抽屉导航在应用程序启动时打开,我找不到让它进入的方法,即使按下后退按钮也没有,我如何让它在 Activity 启动时不打开,并通过滑动手势隐藏它?

这是我在显示抽屉导航的 Activity 中的代码。

public class BottomActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {


RadioGroup radioGroup;
RadioButton Rd1, Rd2, Rd3, Rd4;
DrawerLayout drawer;
ImageView ProfilePic;

@SuppressLint({"RtlHardcoded", "ClickableViewAccessibility"})
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_bottom );

    Objects.requireNonNull( getSupportActionBar() ).setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM );
    getSupportActionBar().setCustomView( R.layout.abs_layout_home );


    radioGroup = findViewById( R.id.radioGroup );
    Rd1 = findViewById( R.id.radioButton );
    Rd2 = findViewById( R.id.radioButton2 );
    Rd3 = findViewById( R.id.radioButton3 );
    Rd4 = findViewById( R.id.radioButton4 );
    radioGroup.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (Rd1.isChecked()) {
                Intent intent = new Intent( getApplicationContext(), BottomActivity.class );
                startActivity( intent );
            }
            if (Rd2.isChecked()) {
                Intent intent1 = new Intent( getApplicationContext(), DashBoard.class );
                startActivity( intent1 );
            }
            if (Rd3.isChecked()) {
                Intent intent2 = new Intent( getApplicationContext(), SettingsActivity.class );
                startActivity( intent2 );
            } else {
                if (Rd4.isChecked()) {
                    Intent intent3 = new Intent( getApplicationContext(), Messages.class );
                    startActivity( intent3 );
                }
            }
        }
    } );

       Toolbar Toolbar = findViewById( R.id.toolbar );
    DrawerLayout Drawer = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, Drawer, Toolbar, "Drawer is opened", "Drawer is closed");
    Drawer.addDrawerListener(toggle);
    toggle.syncState();

}



}


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


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected( item );
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_profile) {
        startActivity( new Intent( BottomActivity.this, ProfileActivity.class ) );
    } else if (id == R.id.nav_settings) {
        startActivity( new Intent( BottomActivity.this, SettingsActivity.class ) );

    } else if (id == R.id.nav_share) {

    }

    return true;
}

public void onClickButtonListener() {

    ProfilePic = findViewById( R.id.ProfilePicture );

    ProfilePic.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent( BottomActivity.this, ProfileActivity.class );
            startActivity( intent );
        }
    } );


}
}

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"
tools:openDrawer="start">


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

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header_nav_drawer"
    app:menu="@menu/activity_nav_drawer_drawer" />


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

预计将在向左滑动手势时打开,而不是在 Activity 开始时启动,我们将不胜感激。

最佳答案

尝试添加 ActionBarDrawerToggle在 Activity 的 onCreate() 方法上。

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

    ...

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, mDrawer, mToolbar, "Drawer is opened", "Drawer is closed");
    mDrawer.addDrawerListener(toggle);
    toggle.syncState();
    ...
}

这将起到作用并为您处理显示/隐藏操作。你仍然可以在没有 ActionBarDrawerToggle 的情况下完成它,但我认为这是不值得的(检查 this question and its answers 以获取更多信息)

关于android - 抽屉导航在 Activity 开始时打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55994928/

相关文章:

android - 什么是现有 SQLite 数据库的完整 Android 数据库助手类?

android - 棘手的解析查询

android - 广义函数以避免重复

android - 我们如何让 BroadcastReceiver 在启用 Activity 中的复选框时响应?

android - 在 Phonegap Android 应用程序中启用视口(viewport)捏合缩放无效

java - toast 线从未被调用

android - 从 Activity 外部调用 findViewById()

android - dayOfMonth 的值 29 必须在 [1,28] JODA 时间范围内

android - 如何获取res文件夹的Uri?

android - DJI Mobile Android SDK 是否支持三星 S7、S8 或 Google Pixel?