zoukankan      html  css  js  c++  java
  • android 伸缩控件ExpandableListView 展开失败的可能原因。

    (原创)转载请声明出处http://www.cnblogs.com/linguanh/

    问题原型:

         ExpandableListView 展开失效。

    --------------------直接看结论请拉置 红线下-------------------

          早在同年5月份的时候我写过一篇 自定义 ExpandableListView 收缩类的 博文,那时候我还没碰到这个问题,

    一切很顺利, 入口:http://www.cnblogs.com/linguanh/p/4521257.html

          直到今天,本来想做个日程表,考虑到月份是可选的,所以想重新使用 ExpandableListView,逐使用之。我们知道使用 ExpandableListView 要为它配置个 数据是配置器,也就是ExpandableListAdapter,它有9个接口函数要求重写,具体请转至我的的专题介绍了解它:http://www.cnblogs.com/linguanh/p/4521257.html

     其中有一个是:

    1 @Override
    2 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)

     它的作用是让我们返回一级目录的 view,通常使用五大布局中的一种,例如:

    LinearLayout Group =(LinearLayout) RelativeLayout.inflate(上下文, R.layout.布局, null);


    我们就在 getGroupView 函数中返回这个view,注意:里面 R.layout.布局 就是我们的自定义一级目录 xml 布局
    文件,也是我要说的坑所在。

    我在确定编码没问题之后,就点运行了,几秒后, getGroupView 加载的一级目录 xml 布局显示出来了,OK,很好,然后我就点击了,点了之后发现,妹的,没展开二级目录。然后就屁颠屁颠地回去找bug,代码确定没错,于是加入了很多log,再次运行,查看日志。我勒个去!
    1 @Override 
    2 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    
    
        这货居然没被执行,里面的log 没打印出。于是下意识地去查看我在getChildView引入的 xml 布局,我上面说的第一个 xml 布局是一级目录的,在getChildView 是二级目录的 布局。查看之后,实在找不出它有错的理由,于是乎,就找之前成功过的例子 xml 文件替换进去,运行,点击,还是不行,当时我就fuck 了 dog 了。  
        然后转至 getGroupView 一级目录 xml布局的引入函数,查看仍找不出错的理由,同上,用之前成功过的替换下,运行,点击,made,居然行了。然后我就开始 把原来不行的 布局文件 和 替换后可以的来对比。
    控件类型对比差异:不行的布局文件带有 button
    控件,可以的没有带有button,其它地方一样。

    看到这,突然觉得,是不是 button 的点击属性覆盖了原本的一级目录可点击属性?再看看 button 的宽和高,即
    它的有效点击范围,都是 wrap,按道理没占满整个父view,我点其他地方,不就是没点到它吗。
    可事实就是如此。button 的存在导致 ExpandableListView 一级目录可点击性失效。
    这真是天坑,马上百度百度,看看有没有相同案例,百度了才发现,有碰到和我相同问题的,但是都没有解决!!!
    心情有点小激动,哈哈。

    ---------------------------------------------------------
    总结:
    ExpandableListView 的 数据适配器 ExpandableListAdapter 中的 getGroupView 函数中所引入的自定义一级目录 xml 布局文件不能带有 button,否则会导致展开失效,ImageButton没尝试过,不过可能也是不行的。

    举例对比:
    反例,带有button的
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="horizontal"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent">
     6 
     7     <ImageView
     8         android:layout_marginLeft="@dimen/extend_x_left"
     9         android:id="@+id/entend_x"
    10         android:background="@drawable/extend_x"
    11         android:layout_width="wrap_content"
    12         android:layout_height="wrap_content" />
    13     <TextView
    14         android:alpha="0.8"
    15         android:id="@+id/time_coustom"
    16         android:layout_marginLeft="20dp"
    17         android:text="1:00pm"
    18         android:textSize="17dp"
    19         android:layout_width="wrap_content"
    20         android:layout_height="wrap_content"
    21         />
    22     <Button
    23         android:id="@+id/down"
    24         android:layout_marginTop="10dp"
    25         android:layout_marginRight="10dp"
    26         android:background="@drawable/right"
    27         android:layout_width="15dp"
    28         android:layout_height="15dp" />
    29 </LinearLayout>

    可行的:

    上面的 代码去掉 button







     
    
    
     
  • 相关阅读:
    假期十一
    假期十
    假期九
    假期八
    假期七
    假期六
    假期五
    假期四
    2020.02.11
    2020.02.10
  • 原文地址:https://www.cnblogs.com/linguanh/p/4596249.html
Copyright © 2011-2022 走看看