将子项添加到ExpandableListView的特定标头

时间:2016-02-29 07:36:49

标签: android expandablelistview android-adapter

我正在开发一个用于ExpandableListview的应用程序。首先我添加标题,然后我将childs添加到特定标题。但是当我添加childs时,ExpandableListView的每个头都会更新。 (截图解释)

我使用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

2 个答案:

答案 0 :(得分:2)

通过设置clicklistener

来试试这个
   expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            Toast.makeText(MainActivity.this,headers.get(groupPosition)+"--"+headeritems.get(headers.get(groupPosition)).get(childPosition),Toast.LENGTH_SHORT).show();
            return false;
        }
    });

有关详细信息,请参阅此处http://coderzpassion.com/android-expandable-listview-tutorial/

答案 1 :(得分:1)

在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孩子应该添加或不添加的位置i,e

if(parentPosition == 1 || parentPosition == 3 || parentPosition == 5)

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

return view;