ExpandableListView中子项的子菜单

时间:2017-08-22 14:35:39

标签: android expandablelistview expandablelistadapter

我的XML中有一个ExpandableListView,通过使用BaseExpandableListAdapter,我创建了一个父列表和子列表。当我单击父列表项时,子菜单将打开(子列表)。

现在我的问题是我想为子列表创建子菜单,当我点击每个子菜单(子列表)时打开另一个子菜单。例如,对于 red 1 ,我想创建一个子菜单,当我点击"红色1"链接父列表打开子菜单

enter image description here

public class MyAdapter extends BaseExpandableListAdapter {

String [] parentList= {"red","blue","green"};

String [][] childList = {{"red 1","red 2","red 3"},
                        {"blue 1","blue 2","blue 3"},
                        {"green 1","green 2","green 3"}};


Context context;

public MyAdapter(Context context){

    this.context=context;

}
@Override

public int getGroupCount() {
    return parentList.length;

}

@Override
public int getChildrenCount(int parentPosition) {
    return childList[parentPosition].length;

}

@Override
public Object getGroup(int parentPosition) {
    return parentList[parentPosition];
}

@Override
public Object getChild(int parentPosition, int childPosition) {
    return childList[parentPosition][childPosition] ;
}

@Override
public long getGroupId(int parentPosition) {
    return parentPosition;
}

@Override
public long getChildId(int parentPosition, int childPosition) {
    return childPosition;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int parentPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    TextView textView = new TextView(context);
    textView.setText(parentList[parentPosition]);
    textView.setPadding(140,0,0,0);
    textView.setTextSize(30);
    return textView;

}

@Override
public View getChildView(int parentPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    final TextView textView = new TextView(context);
    textView.setText(childList[parentPosition][childPosition]);
    textView.setPadding(175, 0, 0, 0);
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(context, textView.getText().toString(), Toast.LENGTH_SHORT).show();

        }
    });
    return textView;}

@Override
public boolean isChildSelectable(int parentPosition, int childPosition) {
    return false;
}

}

0 个答案:

没有答案