【问题标题】:ExpandableListView child click listener not firingExpandableListView 子点击侦听器未触发
【发布时间】:2013-06-03 00:20:57
【问题描述】:

不知道为什么我的 onChildClick 没有触发。一切正常,除了当其中一个子项目被点击时,绝对没有任何反应。否则,可扩展组将按预期工作。

我已将此追溯到我在子 xml 文件中对复选框的使用。当我删除此复选框时,onChildClick 会按预期触发。但是我需要这个复选框来实现这个活动的功能。我做错了什么?谢谢!

public class MySettings extends Activity {

    private ExpandListAdapter expAdapter;
    private ArrayList<ExpandListGroup> expListItems;
    private ExpandableListView expandableList;
    private String client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_settings);

        expandableList = (ExpandableListView) findViewById(R.id.expandable_list);
        expListItems = SetStandardGroups();  //works fine - can show code if needed
        expAdapter = new ExpandListAdapter(MySettings.this, expListItems);
        expandableList.setAdapter(expAdapter);

        expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                //Nothing here ever fires
                System.err.println("child clicked");
                Toast.makeText(getApplicationContext(), "child clicked", Toast.LENGTH_SHORT).show();
                return true;
            }
        });

    }

这里是 xml 文件:

activity_my_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/background" >

    <ImageView
        android:id="@+id/logo"
        android:layout_marginTop="5dip"
        android:layout_alignParentTop="true"
        android:layout_width="wrap_content"
        android:layout_height="70dp"
        android:layout_gravity="left"
        android:contentDescription="@string/blank"
        android:src="@raw/logo" >
    </ImageView>

    <TextView
        android:id="@+id/my_settings"
        android:textColor="#000000"
        android:layout_below="@id/logo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="@string/my_settings"
        android:textSize="30sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/my_settings" >

        <ExpandableListView
            android:id="@+id/expandable_list"
            android:scrollingCache="false"
            android:layout_marginTop="20dip"
            android:layout_height="wrap_content"
            android:layout_width="match_parent" />

    </LinearLayout>

</RelativeLayout>

expandlist_group_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="55dip"
    android:background="#FF7C7C7C" >

    <TextView
        android:id="@+id/group_header"
        android:layout_marginLeft="40dp"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:gravity="center_vertical"
        android:textColor="#000000"
        android:textSize="22sp" />

</LinearLayout>

expandlist_child_item.xml

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

    <CheckBox
        android:id="@+id/check_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/expand_list_item"
        android:paddingLeft="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="@dimen/smart_finder_settings_font_size"
        android:textColor="#FFFFFF" />

</LinearLayout>

【问题讨论】:

    标签: android expandablelistview onclicklistener


    【解决方案1】:

    请务必覆盖可扩展列表适配器的 isChildSelectable 方法并返回 true,如下所示:

    public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    ...
    
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    ...
    }
    

    【讨论】:

    • 在许多使用自定义布局来填充父级的情况下,这似乎是个问题,自定义布局中的视图将窃取焦点,并将在那里实现非常自己的 ClickListners 和 LongClickListeners....
    【解决方案2】:

    我明白了。我所要做的就是添加

    android:focusable="false"
    

    在我的 expandlist_child_item.xml 文件的 CheckBox 部分中。

    我希望这对某人有所帮助。

    【讨论】:

    • 是的,它对我有用,但是当我们恰好在那个时候单击复选框项目时,onlick 侦听器也没有触发。?
    • @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; }
    【解决方案3】:

    看起来还不错,仍然:

    1. 检查您是否没有为 listview 的任何父视图设置点击侦听器。

    2. 检查适配器的isChildSelectable() 是否返回true。 areAllItemsEnabled() 也应该返回 true。

    【讨论】:

      【解决方案4】:

      复选框不应该是可聚焦的,也不应该是可点击的。

      【讨论】:

        【解决方案5】:

        复选框不应该是可聚焦和可点击的..

        <CheckBox
                    android:focusable="false"
                    android:clickable="false"
                    android:focusableInTouchMode="false"
                    android:id="@+id/expandedListItem"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp" />
        

        【讨论】:

          【解决方案6】:

          我认为你必须使用 onItemClickListener 并使用传递的参数来查看是否是组点击

          【讨论】:

            猜你喜欢
            • 2019-11-24
            • 1970-01-01
            • 2022-06-10
            • 2015-12-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-27
            相关资源
            最近更新 更多