ExpandableListView孩子点击监听器不点火

ExpandableListView孩子点击监听器不点火

本文介绍了ExpandableListView孩子点击监听器不点火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道为什么我的onChildClick不点火。一切完美,只是当孩子的项目之一被窃听,绝对没有任何反应。否则,按预期的方式扩展组工作。

我已经追查这回我使用的复选框中的孩子xml文件。当我删除此复选框,则onChildClick火灾预期。但我需要此复选框这项活动的功能。我是我做错了什么?谢谢!

 公共类MySettings延伸活动{

    私人ExpandListAdapter expAdapter;
    私人的ArrayList< ExpandListGroup> expListItems;
    私人ExpandableListView expandableList;
    私人字符串的客户端;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_my_settings);

        expandableList =(ExpandableListView)findViewById(R.id.expandable_list);
        expListItems = SetStandardGroups(); //工作正常 - 可以显示code。如果需要的
        expAdapter =新ExpandListAdapter(MySettings.this,expListItems);
        expandableList.setAdapter(expAdapter);

        expandableList.setOnChildClickListener(新ExpandableListView.OnChildClickListener(){

            @覆盖
            公共布尔onChildClick(ExpandableListView父,视图V,INT groupPosition,诠释childPosition,长ID){
                //这里没有什么永远闪光
                通信System.err.println(子点击);
                Toast.makeText(getApplicationContext(),子点击,Toast.LENGTH_SHORT).show();
                返回true;
            }
        });

    }
 

下面是XML文件:

activity_my_settings.xml

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    机器人:背景=@可绘制/背景>

    < ImageView的
        机器人:ID =@ + ID /标志
        机器人:layout_marginTop =5dip
        机器人:layout_alignParentTop =真
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =70dp
        机器人:layout_gravity =左
        机器人:contentDescription =@字符串/空白
        机器人:SRC =@生/标志>
    < / ImageView的>

    <的TextView
        机器人:ID =@ + ID / my_settings
        机器人:文字颜色=#000000
        机器人:layout_below =@ ID /标志
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginTop =10dp
        机器人:重力=中心
        机器人:文本=@字符串/ my_settings
        机器人:TEXTSIZE =30sp
        机器人:TEXTSTYLE =黑体/>

    <的LinearLayout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_below =@ ID / my_settings>

        < ExpandableListView
            机器人:ID =@ + ID / expandable_list
            机器人:scrollingCache =假
            机器人:layout_marginTop =20dip
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =match_parent/>

    < / LinearLayout中>

< / RelativeLayout的>
 

expandlist_group_item.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =55dip
    机器人:后台=#FF7C7C7C>

    <的TextView
        机器人:ID =@ + ID / group_header
        机器人:layout_marginLeft =40dp
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =40dp
        机器人:重力=center_vertical
        机器人:文字颜色=#000000
        机器人:TEXTSIZE =22sp/>

< / LinearLayout中>
 

expandlist_child_item.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =55dip
    机器人:方向=横向>

    <复选框
        机器人:ID =@ + ID / check_box
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>

    <的TextView
        机器人:ID =@ + ID / expand_list_item
        机器人:以下属性来=10dp
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =@扪/ smart_finder_settings_font_size
        机器人:文字颜色=#FFFFFF/>

< / LinearLayout中>
 

解决方案

我知道了。我所要做的就是添加

 机器人:可调焦=假
 

我expandlist_child_item.xml文件的复选框部分中。

我希望这可以帮助别人。

Not sure why my onChildClick isn't firing. Everything works perfectly, except that when one of the child items is tapped, absolutely nothing happens. Otherwise, the expandable groups work as expected.

I've traced this back to my usage of the checkbox in the child xml file. When I remove this checkbox, the onChildClick fires as expected. But I need this checkbox for the functionality of this activity. What I am I doing wrong? Thanks!

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

    }

Here are the xml files:

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>
解决方案

I got it. All I had to do was add

android:focusable="false"

within the CheckBox section of my expandlist_child_item.xml file.

I hope that this helps somebody.

这篇关于ExpandableListView孩子点击监听器不点火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:14