gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:00:42 25 4
gpt4 key购买 nike

我的抽屉导航在应用程序启动时打开,我找不到让它进入的方法,即使按下后退按钮也没有,我如何让它在 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/

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