android - 将子项添加到 ExpandableListView 的特定 header

标签 android expandablelistview android-adapter

我正在开发一个使用 ExpandableListview 的应用程序。首先,我添加 header ,然后将子级添加到特定 header 。但是当我添加 childs 时,ExpandableListView 的每个 header 都会更新。 (截图说明)

我使用 BaseExpandableListView 适配器,但无法解决此问题。任何帮助对我来说都非常值得。谢谢

这是我的适配器类;

public class SYCriteraAdapter extends BaseExpandableListAdapter {

private LayoutInflater inflater;
private ArrayList<SYCriteraModel> mParent;
private ArrayList<ArrayList<SYCriteraModel>> mChild;
private View view;


public ArrayList<SYCriteraModel> getMParent() {
    return mParent;
}


public SYCriteraAdapter(Context context,ArrayList<SYCriteraModel> parentList) {
    this.mParent = parentList;
    this.inflater = LayoutInflater.from(context);

}

public SYCriteraAdapter(Context context,ArrayList<SYCriteraModel> parentList,ArrayList<ArrayList<SYCriteraModel>> childList) {
    this.mParent = parentList;
    this.inflater = LayoutInflater.from(context);
    this.mChild = childList;

}

// counts the number of group/parent items so the list knows how many
// times calls getGroupView() method
public int getGroupCount() {
    return mParent.size();
}

// counts the number of children items so the list knows how many times
// calls getChildView() method
public int getChildrenCount(int parentPosition) {
    int childCount = 0;
    try {
        childCount = mChild.get(parentPosition).size();
    }
    catch (Exception e){
        System.out.println("Exception " + e);
    }
    return childCount;
}

// gets the title of each parent/group
public Object getGroup(int i) {
    return null;
}

// gets the name of each item
public Object getChild(int parentPosition, int childPosition) {

    return null;
}

public long getGroupId(int parentPosition) {
    return 0;
}

public long getChildId(int i, int childPosition) {
    return 0;
}

public boolean hasStableIds() {
    return false;
}

// in this method you must set the text to see the parent/group on the list

public View getGroupView(int parentPosition, boolean b, View view, ViewGroup viewGroup) {
    if (view == null) {
        view = inflater.inflate(R.layout.critera_upper_adapter, viewGroup, false);
    }
    TextView uppercritera = (TextView) view.findViewById(R.id.etCritera);
    this.notifyDataSetChanged();
    uppercritera.setText(mParent.get(parentPosition).getParent());
    return view;

}

// in this method you must set the text to see the children on the list

public View getChildView(int parentPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
    if (view == null) {
        view = inflater.inflate(R.layout.critera_lower_adapter, viewGroup, false);
    }
    TextView lowerCritera = (TextView) view.findViewById(R.id.etCritera);
    lowerCritera.setText(mChild.get(parentPosition).get(childPosition).getChildren());

    return view;
}

public boolean isChildSelectable(int i, int i1) {
    return true;
}


}

enter image description here enter image description here

最佳答案

BaseExpandable Adapter 中有一个方法 getChildView 检查该方法中的父位置。

public View getChildView(int parentPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
    view = inflater.inflate(R.layout.critera_lower_adapter, viewGroup, false);
}

//这里你必须检查parentPosition child 的位置是否应该添加i,e

if(父位置==1 || 父位置==3 || 父位置==5)

    TextView lowerCritera = (TextView) view.findViewById(R.id.etCritera);
lowerCritera.setText(mChild.get(parentPosition).get(childPosition).getChildren());

return view;

关于android - 将子项添加到 ExpandableListView 的特定 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35694172/

相关文章:

android - fragment 中 RecyclerView 上的 SearchView

android - 如何使用 LinearLayout 将 TextView 右对齐

java - Android:不允许后台执行:接收Intent

Android ExpandableListView获取bindChildView内的组号

android - 布局中的多个 Android 可扩展列表

java - 防止过滤适配器数据时重新加载图像 [Android]

android - 网页自动登录

android - ExpandableListView - 父项及其子项周围的边界

java - 在自定义适配器notifyDataSetChanged上获取NullPointerException

android - Adapter在android中的使用