在子项的最后一项和下一个Groupheader expandableListview之间实现Divider

时间:2013-12-30 10:23:25

标签: android expandablelistview divider

我在android app中实现了expandablelistview,并在其中实现了divider。

我有一个问题是没有在孩子的最后一个项目和下一个组头之间获得分隔符。

以下图片是我想要的:

enter image description here

但以下是我得到的:

enter image description here

如果您比较两个图像,那么CID和About Set之间的分隔符不会出现如何实现该分隔符?

尽管在android:state_expanded和android:state_empty中包含带有2个不同图像的item标签的groupindicator中提供了xml,groupIndicator也没有改变。

但是android:state_expanded和android:state_empty的属性不会出现。

3 个答案:

答案 0 :(得分:5)

实施起来非常容易 1-我们将在groub_item_layout和child_item_layout中添加divider:
group_item_layout:

<?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="wrap_content"
    android:background="@color/white"
    android:minHeight="50dp"
    android:orientation="vertical">

    <View
        android:id="@+id/adapter_divider_top"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#B6B6B6" />

    <TextView
        android:id="@+id/tv_adapter_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:paddingBottom="10dp"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        android:text="Home"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/primary_text" />

</LinearLayout>


child_item_layout:

<?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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_adapter_desc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:text="description"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#212121" />

    <View
        android:id="@+id/adapter_divider_bottom"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/divider" />
</LinearLayout>


2-在您的适配器getChildView()getGroupView()方法中:

 @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup viewGroup) {

        if (view == null) {

            view = mInflater.inflate(R.layout.group_item_layout, null);

            mGroupViewHolder = new GroupViewHolder();

            mGroupViewHolder.tvTitle = (TextView) view.findViewById(R.id.tv_adapter_title);

            mGroupViewHolder.dividerTop = view.findViewById(R.id.adapter_divider_top);

            view.setTag(mGroupViewHolder);

        } else {

            mGroupViewHolder = (GroupViewHolder) view.getTag();

        }

        // That if you want to show divider in expanded group only.
        // If you want to show divider in all group items remove this block.
        if (isExpanded) {

            mGroupViewHolder.dividerTop.setVisibility(View.VISIBLE);

        } else {

            mGroupViewHolder.dividerTop.setVisibility(View.INVISIBLE);

        }
        // That if you want to show divider in expanded group only.

        String myTitle = getGroup(groupPosition);

        if(myTitle != null) {

            mGroupViewHolder.tvTitle.setText(myTitle);

        }

        return view;
    }


@Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {


        if (view == null) {

            view = mInflater.inflate(R.layout.child_item_layout, null);

            mChildViewHolder = new ChildViewHolder();

            mChildViewHolder.tvDesc = (TextView) view.findViewById(R.id.tv_adapter_desc);

            mChildViewHolder.dividerBottom = view.findViewById(R.id.adapter_divider_bottom);

            view.setTag(mChildViewHolder);

        } else {

            mChildViewHolder = (ChildViewHolder) view.getTag();

        }

        if(isLastChild) {

            mChildViewHolder.dividerBottom.setVisibility(View.VISIBLE);

        } else {

            mChildViewHolder.dividerBottom.setVisibility(View.INVISIBLE);

        }

        Timesheet timesheet = getChild(groupPosition, childPosition);

        if(timesheet != null) {

            mChildViewHolder.tvDesc.setText(timesheet.getDescription());

        }


        return view;
    }

希望这有助于任何人。

答案 1 :(得分:0)

首先制作自定义可扩展列表视图... 然后为标题及其子标题添加不同的分隔符

参考代码(expandable listview xml的标题): -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="#000000" >

    <ImageView
        android:id="@+id/divider_top"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/divider_vertical" />

    <TextView
        android:id="@+id/lblListHeader"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/divider_top"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textColor="#c5e26d"
        android:textSize="17dp" />

    <ImageView
        android:id="@+id/divider_bottom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/lblListHeader"
        android:layout_centerHorizontal="true"
        android:background="@drawable/divider" />

</RelativeLayout>

答案 2 :(得分:0)

您可以创建自己的分隔符,而不是使用ExpandableListView的分隔符。将列表的父元素的背景设置为

android:background="@drawable/top_divider"

drawable / top_divider.xml包含

的位置
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:left="-1dp" android:right="-1dp" android:bottom="-1dp">
      <shape>
            <solid android:color="@android:color/transparent" />
            <stroke android:width="1dp" android:color="@android:color/black" />
      </shape>
    </item>
</layer-list>