【问题标题】:How to implement an OnChildClickListener?如何实现 OnChildClickListener?
【发布时间】:2013-11-20 07:49:56
【问题描述】:

我希望能够单击 ExpandableListView 中的菜单项(字体/文本颜色),然后将第二个视图上的文本设置为适当的格式。我应该如何实现监听器?我是 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;
}

}

【问题讨论】:

    标签: java android expandablelistview onclicklistener


    【解决方案1】:

    在您的自定义适配器(MyAdapter)的 getChildView 中实现 View.OnClickListener

    【讨论】:

      【解决方案2】:

      测试这段代码:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多