Android分组子级的不同视图布局之BUG奇遇记

Android分组子级的不同视图布局之BUG奇遇记

最近在使用按日期分类列表,二级条目可能不一样,于是就想到了ExpandableListView。

ExpandableListView的布局显示分割线问题:

 <ExpandableListView
                android:id="@+id/expandableListView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:childDivider="#ededed"
                android:divider="#ededed"
                android:dividerHeight="10dp"
                android:listSelector="@color/transparent" />
  • childDivider:二级条目的分割线 (可以是drawable或color)

  • divider:父级条目的分割线 (可以是drawable或color)

  • dividerHeight:分割线的高度

适配器的BaseExpandableListAdapter中需要注意的getChildType()类型需要从0~getChildTypeCount()-1;同理getGroupType要求也一样。看源码:

/**
     * Get the type of child View that will be created by
     * {@link android.widget.ExpandableListAdapter#getChildView(int, int, boolean, View, ViewGroup)}
     * for the specified child item.
     * 
     * @param groupPosition the position of the group that the child resides in
     * @param childPosition the position of the child with respect to other children in the group
     * @return An integer representing the type of child View. Two child views should share the same
     *         type if one can be converted to the other in
     *         {@link android.widget.ExpandableListAdapter#getChildView(int, int, boolean, View, ViewGroup)}
     *         Note: Integers must be in the range 0 to {@link #getChildTypeCount} - 1.
     *         {@link android.widget.Adapter#IGNORE_ITEM_VIEW_TYPE} can also be returned.
     * @see android.widget.Adapter#IGNORE_ITEM_VIEW_TYPE
     * @see #getChildTypeCount()
     */

    int getChildType(int groupPosition, int childPosition);
原文地址:https://www.cnblogs.com/denluoyia/p/9954184.html