在所有活动中显示导航抽屉问题



我正在制作一个具有不同活动的导航抽屉,我想在所有活动中显示相同的导航抽屉,我使用相同的代码并将其放入所有活动中,但是当我单击活动时,我的应用程序一直崩溃,我不知道为什么

尝试以下代码在所有活动上显示导航抽屉,希望此示例对您有所帮助...!

基本活动

public class BaseActivity extends AppCompatActivity  {
ArrayList<String> menuList;
protected FrameLayout frameLayout;
protected ListView mDrawerList;
protected static int position;
private static boolean isLaunch = true;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
@SuppressLint("ResourceAsColor")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.navigation_drawer_base_layout);
prepareListItems();

frameLayout = findViewById(R.id.content_frame);
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerList = findViewById(R.id.left_drawer);
View header = getLayoutInflater().inflate(R.layout.header, null);
mDrawerList.addHeaderView(header);
mDrawerList.setAdapter(new ArrayAdapter<>(this, R.layout.drawer_list_item, menuList));
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
openActivity(position);
}
});
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.menu);
actionBarDrawerToggle = new ActionBarDrawerToggle(
this,                        /* host Activity */
mDrawerLayout,                /* DrawerLayout object */
R.mipmap.ic_launcher_round,     /* nav drawer image to replace 'Up' caret */
R.string.open_drawer,       /* "open drawer" description for accessibility */
R.string.close_drawer)      /* "close drawer" description for accessibility */ {
@Override
public void onDrawerClosed(View drawerView) {
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
Objects.requireNonNull(getSupportActionBar()).setTitle(getString(R.string.app_name));
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
@Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
}
};
mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
if (isLaunch) {
isLaunch = false;
openActivity(1);
}
}
private void prepareListItems() {
menuList = new ArrayList<>();
menuList.add(" Activity1 ");
menuList.add(" Activity2 ");
menuList.add(" Activity3 ");
menuList.add(" Activity4 ");
}
/**
* @param position Launching activity when any list item is clicked.
*/
protected void openActivity(int position) {
mDrawerList.setItemChecked(position - 1, true);
mDrawerLayout.closeDrawer(mDrawerList);
BaseActivity.position = position; //Setting currently selected position in this field so that it will be available in our child activities.

switch (position) {
case 0:
break;
case 1:
startActivity(new Intent(this, Activity1.class));
break;
case 2:
startActivity(new Intent(this, Activity2.class));
break;
case 3:
startActivity(new Intent(this, Activity3.class));
break;
case 4:
startActivity(new Intent(this, Activity4.class));
break;
default:
break;
}
//        Toast.makeText(this, "Selected Item Position::" + position, Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
}

活动1

public class Activity1 extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.activity_main, frameLayout);
mDrawerList.setItemChecked(position, true);
}

@Override
protected void onDestroy() {
super.onDestroy();
}
}

navigation_drawer_base_layout

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@drawable/simple_brushed_metal"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</androidx.drawerlayout.widget.DrawerLayout>

页眉

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shade">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="serif"
android:gravity="center"
android:text="@string/app_name"
android:textColor="@color/textColorPrimary"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/developed_by_nd_software_solution"
android:textColor="@color/textColorPrimary"
android:textSize="12sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="8dp"
android:background="@color/textColorPrimary" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

drawer_list_item

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:fontFamily="serif"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@color/textColorPrimary" />

activity_main

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="serif"
android:gravity="center"
android:text="@string/app_name"
android:textColor="@color/textColorPrimary"
android:textSize="18sp" />
</androidx.constraintlayout.widget.ConstraintLayout>

在这里,我为一个活动(活动1(提供了代码,为活动2,活动3等其他活动编写代码。

相关内容

  • 没有找到相关文章

最新更新