Android 单击导航抽屉中的菜单项后如何打开子菜单?
我实现了一个带有导航视图的导航抽屉。我正在通过menu.xml文件在导航视图中添加值Android 单击导航抽屉中的菜单项后如何打开子菜单?,android,navigation-drawer,android-menu,Android,Navigation Drawer,Android Menu,我实现了一个带有导航视图的导航抽屉。我正在通过menu.xml文件在导航视图中添加值 <android.support.design.widget.NavigationView android:id="@+id/nvView" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:itemTextColor="@android:c
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextColor="@android:color/white"
android:background="?attr/colorAccent"
app:menu="@menu/drawer_view"
app:headerLayout="@layout/nav_header"
>
</android.support.design.widget.NavigationView>
现在的问题是,用这种方式我不能点击菜单,只有子菜单可以点击这里
我想在这里显示子菜单,只在点击菜单后显示,否则子菜单会显示给其他人,或者说它处于不可见状态我希望您应该使用侧导航抽屉中的自定义布局
以下是示例链接-
您还需要在侧导航抽屉中为您的场景实现可扩展列表视图,这是可扩展列表视图的示例
您需要将这两种功能结合起来,才能获得具有可扩展列表的导航抽屉。最佳解决方案是在导航视图中具有可扩展列表视图。请参阅下面的代码
活动\导航\视图.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<include layout="@layout/navigation_view_fragment_container"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/navigation_view_header">
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@android:color/white"
android:layout_marginTop="192dp"
android:id="@+id/navigationmenu">
</ExpandableListView>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
布局导航标题如下所示
导航\视图\标题.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="192dp"
android:background="#ff5722"
android:padding="16dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:orientation="vertical"
android:gravity="bottom">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Username"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
</LinearLayout>
在导航视图活动中,为可展开列表视图设置适配器。
NavigationViewActivity.java
public class NavigationViewActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
ExpandableListAdapter mMenuAdapter;
ExpandableListView expandableList;
List<ExpandedMenuModel> listDataHeader;
HashMap<ExpandedMenuModel, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_view);
final ActionBar ab = getSupportActionBar();
/* to set the menu icon image*/
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
expandableList= (ExpandableListView) findViewById(R.id.navigationmenu);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
prepareListData();
mMenuAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild, expandableList);
// setting list adapter
expandableList.setAdapter(mMenuAdapter);
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding data header
listDataHeader.add("heading1");
listDataHeader.add("heading2");
listDataHeader.add("heading3");
// Adding child data
List<String> heading1= new ArrayList<String>();
heading1.add("Submenu of item 1");
List<String> heading2= new ArrayList<String>();
heading2.add("Submenu of item 2");
heading2.add("Submenu of item 2");
heading2.add("Submenu of item 2");
listDataChild.put(listDataHeader.get(0), heading1);// Header, Child data
listDataChild.put(listDataHeader.get(1), heading2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
return true;
}
});
}
@Override
public void onFragmentInteraction(Boolean isDataSaved) {
}
}
公共类NavigationViewActivity扩展了AppCompatActivity{
私人抽屉布局mDrawerLayout;
可扩展列表适配器;
可扩展列表查看可扩展列表;
列表列表数据头;
HashMapListDataChild;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u navigation\u视图);
最终ActionBar ab=getSupportActionBar();
/*设置菜单图标图像的步骤*/
ab.setHomeAsUpIndicator(R.drawable.ic_菜单);
ab.setDisplayHomeAsUpEnabled(真);
mDrawerLayout=(抽屉布局)findViewById(R.id.抽屉布局);
expandableList=(ExpandableListView)findViewById(R.id.navigationmenu);
NavigationView NavigationView=(NavigationView)findViewById(R.id.nav_视图);
if(navigationView!=null){
setupDrawerContent(导航视图);
}
prepareListData();
MMENUAAdapter=新的ExpandableListAdapter(此,listDataHeader,listDataChild,expandableList);
//设置列表适配器
expandableList.setAdapter(mMenuAdapter);
}
私有void prepareListData(){
listDataHeader=新的ArrayList();
listDataChild=newHashMap();
//添加数据头
listDataHeader.add(“heading1”);
listDataHeader.add(“heading2”);
listDataHeader.add(“heading3”);
//添加子数据
列表标题1=新的ArrayList();
标题1.添加(“第1项子菜单”);
列表标题2=新的ArrayList();
标题2.添加(“第2项子菜单”);
标题2.添加(“第2项子菜单”);
标题2.添加(“第2项子菜单”);
listDataChild.put(listDataHeader.get(0),heading1);//头,子数据
listDataChild.put(listDataHeader.get(1),heading2);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.menu,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
mDrawerLayout.openDrawer(重力compat.START);
返回true;
}
返回super.onOptionsItemSelected(项目);
}
私有void setupDrawerContent(导航视图导航视图){
navigationView.setNavigationItemSelectedListener(
新建NavigationView.OnNavigationItemSelectedListener(){
@凌驾
公共布尔值onNavigationItemSelected(MenuItem MenuItem){
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
返回true;
}
});
}
@凌驾
公共void onFragmentInteraction(布尔值isDataSaved){
}
}
可扩展列表视图的适配器如下所示
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<String> mListDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> mListDataChild;
ExpandableListView expandList;
public ExpandableListAdapter(Context context, List<String> listDataHeader,HashMap<String, List<String>> listChildData,ExpandableListView mView)
{
this.mContext = context;
this.mListDataHeader = listDataHeader;
this.mListDataChild = listChildData;
this.expandList=mView;
}
@Override
public int getGroupCount() {
int i= mListDataHeader.size();
Log.d("GROUPCOUNT",String.valueOf(i));
return this.mListDataHeader.size();
}
@Override
public int getChildrenCount(int groupPosition) {
int childCount=0;
if(groupPosition!=2)
{
childCount=this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
.size();
}
return childCount;
}
@Override
public Object getGroup(int groupPosition) {
return this.mListDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
Log.d("CHILD",mListDataChild.get(this.mListDataHeader.get(groupPosition))
.get(childPosition).toString());
return this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ExpandedMenuModel headerTitle = (ExpandedMenuModel) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.listheader, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.submenu);
ImageView headerIcon= (ImageView)convertView.findViewById(R.id.iconimage);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle.getIconName());
headerIcon.setImageDrawable(headerTitle.getIconImg());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_submenu, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.submenu);
txtListChild.setText(childText);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#000000"
android:layout_marginLeft="20dp"
android:textSize="18sp"
android:id="@+id/submenu"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:id="@+id/iconimage"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#000000"
android:textSize="20sp"
android:id="@+id/submenu"/>
</LinearLayout>
</LinearLayout>
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私有上下文;
私有列表mListDataHeader;//标题
//标题标题、子标题格式的子数据
私有HashMap mListDataChild;
可扩展列表视图可扩展列表;
公共ExpandableListAdapter(上下文上下文、列表listDataHeader、HashMap listChildData、ExpandableListView mView)
{
this.mContext=上下文;
this.mListDataHeader=listDataHeader;
this.mListDataChild=listChildData;
this.expandList=mView;
}
@凌驾
public int getGroupCount(){
int i=mListDataHeader.size();
Log.d(“GROUPCOUNT”,String.valueOf(i));
返回此.mListDataHeader.size();
}
@凌驾
公共整数getChildrenCount(整数组位置){
int childCount=0;
if(groupPosition!=2)
{
childCount=this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
.size();
}
返回childCount;
}
@凌驾
公共对象getGroup(int-groupPosition){
返回此.mListDataHeader.get(groupPosition);
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
Log.d(“CHILD”,mListDataChild.get(this.mListDataHeader.get(groupPosition))
.get(childPosition.toString());
返回this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
.get(儿童位置);
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共视图getGroupView(int groupPosition、布尔isExpanded、视图convertView、视图组父级){
ExpandedMenuModel headerTitle=(ExpandedMenuModel)getGroup(groupPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInf
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:id="@+id/iconimage"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#000000"
android:textSize="20sp"
android:id="@+id/submenu"/>
</LinearLayout>
</LinearLayout>