下拉图像未在ExpandableListView中正确显示

问题描述 投票:0回答:1

我是Android新手。我在ExpandableListView创造了NavigationView。在里面,我有一个图像下拉列表,显示有一个subMenu。列表正常,但当我单击项目以查看子菜单时,显示所有Items.it的下拉图像应仅显示少数项目。

这是我的代码:

适配器:

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;
}

}

主要内容:

 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;
        }
    });
}
android navigation-drawer expandablelistview expandablelistadapter
1个回答
1
投票

因为视图是ListView / ExpandableListView / RecyclerView等的重用/回收,所以你需要为if-else设置两个条件。尝试更改:

if (hasChildren) {
    imageView.setVisibility(View.VISIBLE);
    imageView.setImageResource(R.mipmap.ic_dropdown);
}

if (hasChildren) {
    imageView.setVisibility(View.VISIBLE);
    imageView.setImageResource(R.mipmap.ic_dropdown);
} else {
    imageView.setVisibility(View.INVISIBLE);
}

希望有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.