【问题标题】:setOnChildClickListener of ExpandableListView inside a Custom Alter dialog does not work自定义 Alter 对话框中 ExpandableListView 的 setOnChildClickListener 不起作用
【发布时间】:2015-04-23 18:26:26
【问题描述】:

我创建了一个自定义警报对话框,里面有一个 ExpandableListView。 ExpandableListView 子项是复选框。

每当用户单击其中一个复选框时,我都会尝试设置事件侦听器,但它不起作用。我可以选择和取消选择自定义警报对话框中的复选框,但事件处理程序不起作用。谁能帮帮我?

创建警报对话框的代码

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select the route or segments");

LinearLayout layout = (LinearLayout) findViewById(R.id.custom_alert_dialog);


LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_alert_dialog_routes, null);
ExpandableListView listView = (ExpandableListView) dialoglayout.findViewById(R.id.routesListView);


ExpandableListAdapter listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

// setting list adapter
listView.setAdapter(listAdapter);

listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
  @Override
  public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
       Toast.makeText(getApplicationContext(), "Clicked the checkbox", Toast.LENGTH_LONG).show();

       Log.d("Click", "I have clicked a checkbox.");

       return false;
  }
});

builder.setView(dialoglayout);
builder.setInverseBackgroundForced(true);

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialog, int which) {

                 }
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialog, int which) {

                 }
});

AlertDialog dialog = builder.create();
dialog.show();

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal" android:layout_width="match_parent"
      android:layout_height="wrap_content">

    <CheckBox
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="New CheckBox"
         android:id="@+id/expandedListItem"
         android:textColor="#000000"
         android:focusable="false"
         android:layout_marginLeft="20dp"
         android:paddingTop="10dp"
         android:paddingBottom="10dp"/>

</LinearLayout>

我尝试过使用 android:focusable 并且没有它,但它不起作用。

【问题讨论】:

    标签: android checkbox expandablelistview


    【解决方案1】:

    尝试使用setOnItemClickListener 而不是setOnChildClickListener

    【讨论】:

    • 我也试过了,还是不行。但是谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    相关资源
    最近更新 更多