我是Android新手。我在ExpandableListView
中创建了NavigationView
。
在里面,我有一个图像下拉列表,显示有一个subMenu。
列表可以正确显示,但是当我单击项目以查看子菜单时,所有项目都显示为下拉图像。它应该仅显示少数项目。
这是我的代码:
适配器:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<MenuModel> listDataHeader;
private HashMap<MenuModel, List<MenuModel>> listDataChild;
private int icon= -1;
public ExpandableListAdapter(Context context, List<MenuModel> listDataHeader,
HashMap<MenuModel, List<MenuModel>> listChildData) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listDataChild = listChildData;
}
@Override
public int getGroupCount() {
return this.listDataHeader.size();
}
@Override
public int getChildrenCount(int groupPosition) {
if (this.listDataChild.get(this.listDataHeader.get(groupPosition)) == null)
return 0;
else
return this.listDataChild.get(this.listDataHeader.get(groupPosition))
.size();
}
@Override
public MenuModel getGroup(int groupPosition) {
return this.listDataHeader.get(groupPosition);
}
@Override
public MenuModel getChild(int groupPosition, int childPosition) {
return this.listDataChild.get(this.listDataHeader.get(groupPosition))
.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return groupPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Log.d("groupPosition",""+groupPosition);
String headerTitle = getGroup(groupPosition).menuName;
boolean hasChildren = getGroup(groupPosition).hasChildren;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group_header, null);
}
TextView lblListHeader = convertView.findViewById(R.id.lblListHeader);
// lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
ImageView imageView = convertView.findViewById(R.id.dropdown);
if (hasChildren) {
imageView.setVisibility(View.VISIBLE);
imageView.setImageResource(R.mipmap.ic_dropdown);
}
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = getChild(groupPosition, childPosition).menuName;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group_child, null);
}
TextView txtListChild = convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
MainActivity:
private void prepareMenuData() {
MenuModel menuModel = new MenuModel("Home", true, false); //Menu of Android Tutorial. No sub menus
headerList.add(menuModel);
if (!menuModel.hasChildren) {
childList.put(menuModel, null);
}
menuModel = new MenuModel("My account", true, true); //Menu of Java Tutorials
headerList.add(menuModel);
List<MenuModel> childModelsList = new ArrayList<>();
MenuModel childModel = new MenuModel("My Order", false, false);
childModelsList.add(childModel);
childModel = new MenuModel("Delivery Address", false, false);
childModelsList.add(childModel);
childModel = new MenuModel("My Profile", false, false);
childModelsList.add(childModel);
childModel = new MenuModel("My Loyalty Points", false, false);
childModelsList.add(childModel);
childModel = new MenuModel("Logout", false, false);
childModelsList.add(childModel);
if (menuModel.hasChildren) {
Log.d("API123","here");
childList.put(menuModel, childModelsList);
}
childModelsList = new ArrayList<>();
menuModel = new MenuModel("Niche Market", true, true); //Menu of Python Tutorials
headerList.add(menuModel);
childModel = new MenuModel("Organic items", false, false);
childModelsList.add(childModel);
childModel = new MenuModel("Pollachi Special", false, false);
childModelsList.add(childModel);
childModel = new MenuModel("Imported Items", false, false);
childModelsList.add(childModel);
if (menuModel.hasChildren) {
childList.put(menuModel, childModelsList);
}
menuModel = new MenuModel("Smart Basket", true, false);
headerList.add(menuModel);
if (!menuModel.hasChildren) {
childList.put(menuModel, null);
}
childModelsList = new ArrayList<>();
menuModel = new MenuModel("Category", true, true);
headerList.add(menuModel);
childModel = new MenuModel("Python AST – Abstract Syntax Tree", false, false);
childModelsList.add(childModel);
childModel = new MenuModel("Python Fractions", false, false);
childModelsList.add(childModel);
if (menuModel.hasChildren) {
childList.put(menuModel, childModelsList);
}
menuModel = new MenuModel("Offers", true, false);
headerList.add(menuModel);
if (!menuModel.hasChildren) {
childList.put(menuModel, null);
}
menuModel = new MenuModel("Cookbook", true, false);
headerList.add(menuModel);
if (!menuModel.hasChildren) {
childList.put(menuModel, null);
}
menuModel = new MenuModel("Food and Sharing", true, false);
headerList.add(menuModel);
if (!menuModel.hasChildren) {
childList.put(menuModel, null);
}
menuModel = new MenuModel("Speciality Store", true, false);
headerList.add(menuModel);
if (!menuModel.hasChildren) {
childList.put(menuModel, null);
}
menuModel = new MenuModel("Scheduled Shopping", true, false);
headerList.add(menuModel);
if (!menuModel.hasChildren) {
childList.put(menuModel, null);
}
menuModel = new MenuModel("Notifications", true, false);
headerList.add(menuModel);
if (!menuModel.hasChildren) {
childList.put(menuModel, null);
}
childModelsList = new ArrayList<>();
menuModel = new MenuModel("Customer service", true, true);
headerList.add(menuModel);
childModel = new MenuModel("Contact", false, false);
childModelsList.add(childModel);
childModel = new MenuModel("Feedback", false, false);
childModelsList.add(childModel);
childModel = new MenuModel("FAQ", false, false);
childModelsList.add(childModel);
if (menuModel.hasChildren) {
childList.put(menuModel, childModelsList);
}
}
private void populateExpandableList() {
expandableListAdapter = new ExpandableListAdapter(MainActivity.this, headerList, childList);
for (int i=0;i<headerList.size();i++) {
Log.d("has",""+headerList.get(i).hasChildren);
}
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if (headerList.get(groupPosition).isGroup) {
if (!headerList.get(groupPosition).hasChildren) {
loadHomeFragment();
onBackPressed();
}
}
return false;
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
return false;
}
});
}
答案 0 :(得分:1)
因为视图是ListView / ExpandableListView / RecyclerView等的重用/回收,所以if-else必须同时具备这两个条件。尝试更改:
var array = [1,2,5,1,2,5,5,3];
var array2 = [2,5,5,3,1,10];
到
if (hasChildren) {
imageView.setVisibility(View.VISIBLE);
imageView.setImageResource(R.mipmap.ic_dropdown);
}
希望有帮助!