如何使用可重用代码以android简单方式创建汉堡包菜单?
我知道如何创建安卓汉堡包菜单,有很多教程 我想在每个类中创建汉堡包菜单,在公共类中调用一个方法,该方法有汉堡包菜单代码。还有我的汉堡包菜单内的listview选项如何使用可重用代码以android简单方式创建汉堡包菜单?,android,listview,android-activity,hamburger-menu,Android,Listview,Android Activity,Hamburger Menu,我知道如何创建安卓汉堡包菜单,有很多教程 我想在每个类中创建汉堡包菜单,在公共类中调用一个方法,该方法有汉堡包菜单代码。还有我的汉堡包菜单内的listview选项 如何创建这样的汉堡包菜单。我想减少我的代码,并向其他人提供可重用的代码。请帮助我。您需要进行一项活动,并在该活动的布局文件中输入以下代码: <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/a
如何创建这样的汉堡包菜单。我想减少我的代码,并向其他人提供可重用的代码。请帮助我。您需要进行一项活动,并在该活动的布局文件中输入以下代码:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:elevation="4dp"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/tool_bar"
layout="@layout/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_new">
<put your layout here................>
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="@drawable/bg_all"
app:itemIconTint="@android:color/white"
app:itemTextColor="@android:color/white"
app:theme="@style/list_item_appearance"
app:menu="@menu/drawer_menu" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
然后在每个布局中添加相同导航菜单的片段,并调用它们,如下所示:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()) {
//Replacing the main content with ContentFragment Which is our Inbox View;
case R.id.drawer_home:
txt_title.setText("Home");
Intent intent=new Intent(HomeActivity.this, HomeActivity.class);
startActivity(intent);
overridePendingTransition(0, 0);
finish();
return true;
// For rest of the options we just show a toast on click
case R.id.drawer_artist:
txt_title.setText("Artists");
android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new ArtistsFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
return true;
}
});
在您的BaseActivity中实现导航抽屉,然后在您想用导航抽屉创建一个新的活动时扩展该活动,这样就不需要在每个活动中编写汉堡包菜单代码???…您应该阅读一些关于OOP、继承的知识,然后开始使用Android应用程序。回答你的问题,不。如果你在一个类中实现了一些东西,那么所有继承这个类的类都会有这个东西。
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()) {
//Replacing the main content with ContentFragment Which is our Inbox View;
case R.id.drawer_home:
txt_title.setText("Home");
Intent intent=new Intent(HomeActivity.this, HomeActivity.class);
startActivity(intent);
overridePendingTransition(0, 0);
finish();
return true;
// For rest of the options we just show a toast on click
case R.id.drawer_artist:
txt_title.setText("Artists");
android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new ArtistsFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
return true;
}
});