我正在尝试在expandablelist
中放入navigation drawer
。我一直在遵循此Adding expandableListView to NavigationView教程,但是当我在Android设备上运行该应用程序并单击标题时,没有任何反应。没有Toast
条消息或有关child的方法被执行。
这是我的适配器:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<ExpandedMenuModel> mListDataHeader; // header titles
// child data in format of header title, child title
private HashMap<ExpandedMenuModel, List<String>> mListDataChild;
private ExpandableListView expandList;
public ExpandableListAdapter(Context context, List<ExpandedMenuModel> listDataHeader, HashMap<ExpandedMenuModel, 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(final int groupPosition, final boolean isExpanded, View convertView, final 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 = convertView.findViewById(R.id.submenu);
/*lblListHeader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ExpandableListView expandableListView = (ExpandableListView) parent;
if (!isExpanded) {
expandableListView.expandGroup(groupPosition);
} else {
expandableListView.collapseGroup(groupPosition);
}
}
});*/
ImageView headerIcon = (ImageView) convertView.findViewById(R.id.iconimage);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle.getIconName());
headerIcon.setImageResource(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"?>
<androidx.drawerlayout.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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer">
<ExpandableListView
android:id="@+id/navigationmenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="192dp"
android:background="@android:color/white" />
</com.google.android.material.navigation.NavigationView>
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.drawerlayout.widget.DrawerLayout>
这是我的导航班:
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
ExpandableListAdapter mMenuAdapter;
ExpandableListView expandableList;
List<ExpandedMenuModel> listDataHeader;
HashMap<ExpandedMenuModel, List<String>> listDataChild;
DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
expandableList = findViewById(R.id.navigationmenu);
NavigationView navigationView = findViewById(R.id.nav_view);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
prepareListData();
mMenuAdapter = new ExpandableListAdapter(MainActivity.this, listDataHeader, listDataChild, expandableList);
// setting list adapter
expandableList.setAdapter(mMenuAdapter);
expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
//Log.d("DEBUG", "submenu item clicked");
Toast.makeText(MainActivity.this, "Clicked submenu", Toast.LENGTH_SHORT).show();
return false;
}
});
expandableList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
//Log.d("DEBUG", "heading clicked");
Toast.makeText(MainActivity.this, "Clicked heading", Toast.LENGTH_SHORT).show();
return false;
}
});
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
private void prepareListData() {
listDataHeader = new ArrayList<ExpandedMenuModel>();
listDataChild = new HashMap<ExpandedMenuModel, List<String>>();
ExpandedMenuModel item1 = new ExpandedMenuModel();
item1.setIconName("heading1");
item1.setIconImg(android.R.drawable.ic_delete);
// Adding data header
listDataHeader.add(item1);
ExpandedMenuModel item2 = new ExpandedMenuModel();
item2.setIconName("heading2");
item2.setIconImg(android.R.drawable.ic_delete);
listDataHeader.add(item2);
ExpandedMenuModel item3 = new ExpandedMenuModel();
item3.setIconName("heading3");
item3.setIconImg(android.R.drawable.ic_delete);
listDataHeader.add(item3);
// Adding child data
List<String> heading1 = new ArrayList<>();
heading1.add("Submenu of item 1");
List<String> heading2 = new ArrayList<>();
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);
}
private void setupDrawerContent(NavigationView navigationView) {
//revision: this don't works, use setOnChildClickListener() and setOnGroupClickListener() above instead
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
drawer.closeDrawers();
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}