如何实现OnChildClickListener?

时间:2013-11-20 07:49:57

标签: java android expandablelistview onclicklistener

我希望能够在ExpandableListView中单击菜单项(font / textColor),然后将第二个视图上的文本设置为适当的格式。我该如何实现监听器?我是ExpandableListView的新手,我不知道如何检索每个子项的信息(我的意思是点击)。

public class Second extends Activity {

ExpandableListView exv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    ExpandableListView exv = (ExpandableListView) findViewById(R.id.expandableListView1);
    exv.setAdapter(new MyAdapter(this));
    exv.setOnChildClickListener(new OnChildClickListener(){
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
                        //What should I put here?
            return false;
        }
    });


 }
 }

这是我的适配器:

public class MyAdapter extends BaseExpandableListAdapter {
private Context context;
String [] parentList = {"Font", "Style", "Color"};
String [][] childList = {{"10", "20", "30"}, {"Red", "Black", "Blue"}};
public MyAdapter(Context context) {
    this.context = context;
}

public Object getChild(int groupPosition, int childPosition) {
    return null;
}

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

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
        ViewGroup parent) {
    TextView tv = new TextView(context);
    tv.setText(childList[groupPosition][childPosition]);
    tv.setPadding(30, 10, 10, 10);
    tv.setTextSize(20);
    return tv;
}


public int getChildrenCount(int groupPosition) {
    return childList[groupPosition].length;
}


public Object getGroup(int groupPosition) {
    return groupPosition;
}


public int getGroupCount() {
    return parentList.length;
}


public long getGroupId(int groupPosition) {
    return groupPosition;
}


public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    TextView tv = new TextView(context);
    tv.setText(parentList[groupPosition]);
    tv.setPadding(30, 10, 10, 10);
    tv.setTextSize(20);
    return tv;
}


public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}


public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

}

2 个答案:

答案 0 :(得分:0)

在自定义适配器(MyAdapter)的getChildView

中实现View.OnClickListener

答案 1 :(得分:0)

测试此代码:

exv.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                return false;
            }
        });

        exv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });