ExpandableListView分割线处理

来源:互联网 发布:开发题库软件思路 编辑:程序博客网 时间:2024/06/06 01:50

本人是android开发新手,现在开始记录在实际开发中遇到的问题,希望有一天成为自己崇拜的大牛。

ExpandableListView

在一个项目中碰见了需要分组展示数据的需求,类似于QQ分组,如下:
这里写图片描述
于是第一 反应就是使用ExpandableListView控件,但是问题来了,需求是父类列表之间的分割线是自己定义的样子,子项之间没有分割线。

android:divider="@drawable/list_item_divider"

结果发现只要setDivider之后,父列表和子项之间都没有分割线了,网上查了很久,发现都没什么好的办法,只能用setChildDivider把子项分割线设置为空,父列表之间的分割线一直没办法改变,当我想着去重写这个控件的时候突然想到了可以把父列表和子列表的分割线都设置为空,然后在父列表的布局文件中写个分割线代码如下:

fragment_contract.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="match_parent"    android:background="@color/bg_color"    android:orientation="vertical">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="@dimen/activity_layout_48"        android:background="@color/white">        <ImageView            android:id="@+id/call_log"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:layout_centerVertical="true"            android:layout_marginRight="@dimen/activity_layout_8"            android:src="@drawable/calling" />    </RelativeLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="@dimen/activity_layout_100"        android:layout_marginTop="@dimen/activity_layout_8"        android:background="@color/white"        android:gravity="center">        <GridView            android:id="@+id/address_book_gv"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_marginTop="@dimen/activity_layout_15"            android:numColumns="3"></GridView>    </RelativeLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_marginTop="@dimen/activity_layout_8"        android:background="@color/white">        <TextView            android:id="@+id/address_book_group"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="@dimen/activity_layout_8"            android:layout_marginTop="@dimen/activity_layout_10"            android:text="翰中集团"            android:textColor="@color/black"            android:textSize="@dimen/textSize_16" />        <ExpandableListView            android:id="@+id/address_list"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_below="@id/address_book_group"            android:layout_marginTop="@dimen/activity_layout_11"            android:divider="@null"            ></ExpandableListView>    </RelativeLayout></LinearLayout>

注意将分割线设置为空

  android:divider="@null"

下面是父类列表的布局
address_group_itme.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <LinearLayout        android:id="@+id/ll"        android:layout_width="match_parent"        android:layout_height="@dimen/activity_layout_56"        android:gravity="center|left"        android:orientation="horizontal">        <ImageView            android:id="@+id/logo"            android:layout_width="@dimen/activity_layout_35"            android:layout_height="@dimen/activity_layout_35"            android:layout_marginLeft="@dimen/activity_layout_8"            android:src="@drawable/logo" />        <TextView            android:id="@+id/account_name"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="@dimen/activity_layout_7"            android:text="haha"            android:textColor="@color/black"            android:textSize="@dimen/textSize_16" />    </LinearLayout>    <View        android:layout_width="match_parent"        android:layout_height="@dimen/activity_layout_0.5"        android:layout_below="@id/ll"        android:background="@drawable/list_item_divider" /></RelativeLayout>

list_item_divider.xml

<?xml version="1.0" encoding="utf-8"?><inset xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@color/color_line"    android:insetLeft="@dimen/activity_layout_51"></inset>

写个高度为0.5dp的View当做分割线,在 getGroupView里面设置address_group_itme.xml 这个布局就行了。
最终效果:
这里写图片描述

第一次写博客,不知道怎么写比较好,有不明白的同学,可以加我QQ:1318387070一起学习讨论。

0 0
原创粉丝点击