ExpandableListView - 如何仅在父元素之间设置分隔符

发布于 2024-11-24 01:05:25 字数 524 浏览 2 评论 0原文

我只想在父元素之间放置分隔线。当我设置

android:divider="@drawable/divider"
android creates divider between parent elements, but creates divider between child elements too. When i add
android:childDivider="@color/transparent"
android removes the divider between child elements, but the free space between them remains. Why? I have tried to
android:dividerHeight="0dp"
but nothing happened.

我根本想在父元素之间设置分隔线,但我不希望子元素之间有任何分隔线或空白空间。

任何想法如何做到这一点?

I want to put divider only between parent elements. When i set

android:divider="@drawable/divider"

android creates divider between parent elements, but creates divider between child elements too. When i add

android:childDivider="@color/transparent"

android removes the divider between child elements, but the free space between them remains. Why?
I have tried to

android:dividerHeight="0dp"

but nothing happened.

At all i want to set divider between parent elements, but i do not want any divider or empty space between child elements.

any ideas how to do that??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

南汐寒笙箫 2024-12-01 01:05:25

为了仅从子视图中删除分隔符,而不是在可扩展列表中的父视图之间删除分隔符,

请在 XML 的 ExapandableListView 属性中添加 android:childDivider="#00000000"

<ExpandableListView
    android:id="@+id/elv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:childDivider="#00000000" />

请参阅此页面了解更多信息

In order to remove dividers just from the child views and not between the parents in the expandable list:

add android:childDivider="#00000000" in the ExapandableListView attributes in XML:

<ExpandableListView
    android:id="@+id/elv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:childDivider="#00000000" />

Refer this page for more information

那片花海 2024-12-01 01:05:25

指定分隔线颜色及其高度(当组折叠时分隔线将可见)

<ExpandableListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@color/color_black"
        android:dividerHeight="1dp"
        android:groupIndicator="@null" />

Give Divider color and its height (dividers will be visible when groups are collapsed)

<ExpandableListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@color/color_black"
        android:dividerHeight="1dp"
        android:groupIndicator="@null" />
追星践月 2024-12-01 01:05:25

诀窍是为折叠组布局和最后一个子布局创建一个额外的视图

crash_group.xml:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent">

    <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textSize="17sp"/>

    <!-- this is the extra view-->  
    <View 
       android:layout_width="fill_parent"
       android:layout_height="10dp"
       android:background="@android:color/transparent"/>        

</LinearLayout>

expanded_group.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent">

    <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textSize="17sp"/>

    <!-- no extra view-->       

</LinearLayout>

并对子视图应用相同的技术,为最后一个子视图插入额外的视图

The trick is to create an extra view for collapse group layout and last child layout.

collapse_group.xml:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent">

    <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textSize="17sp"/>

    <!-- this is the extra view-->  
    <View 
       android:layout_width="fill_parent"
       android:layout_height="10dp"
       android:background="@android:color/transparent"/>        

</LinearLayout>

expanded_group.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent">

    <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textSize="17sp"/>

    <!-- no extra view-->       

</LinearLayout>

And apply the same technique to the child view, for the last child view insert the extra view

浅黛梨妆こ 2024-12-01 01:05:25

我所做的是将 ExpandableListView 的分隔符设置为 0,然后将“分隔符”组插入到它的 ExpandableListAdapter 中:

// To get dividers between the groups we add 'divider views' as if they were  
// themselves groups.
public int getGroupCount() {
// Actual groups are at even groupPositions, dividers at odd
    return (this.data.size() * 2) - 1;
}

public long getGroupId(int groupPosition) {
    if(groupPosition % 2 != 0) return R.id.divider;
    else return R.id.group;
}

public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    if(groupPosition % 2 != 0) {
        View divider = convertView;
        if(divider == null || divider.getId() != R.id.divider) {
            divider = this.inflater.inflate(R.layout.divider, null);
        }

        return divider;
    }

    View group = convertView;
    if(group == null || group.getId() != R.id.group) {
        group = this.inflater.inflate(R.layout.group, null);
    }

    return group;
}

What I did was to set the ExpandableListView's divider to 0 and then insert "divider" groups into it's ExpandableListAdapter:

// To get dividers between the groups we add 'divider views' as if they were  
// themselves groups.
public int getGroupCount() {
// Actual groups are at even groupPositions, dividers at odd
    return (this.data.size() * 2) - 1;
}

public long getGroupId(int groupPosition) {
    if(groupPosition % 2 != 0) return R.id.divider;
    else return R.id.group;
}

public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    if(groupPosition % 2 != 0) {
        View divider = convertView;
        if(divider == null || divider.getId() != R.id.divider) {
            divider = this.inflater.inflate(R.layout.divider, null);
        }

        return divider;
    }

    View group = convertView;
    if(group == null || group.getId() != R.id.group) {
        group = this.inflater.inflate(R.layout.group, null);
    }

    return group;
}
烙印 2024-12-01 01:05:25
expListView.setDividerHeight(10);
expListView.setChildDivider(getResources().getDrawable(
            android.R.color.white));
expListView.setDividerHeight(10);
expListView.setChildDivider(getResources().getDrawable(
            android.R.color.white));
这样的小城市 2024-12-01 01:05:25

如果您只想删除子分隔线,一个简单的技巧是您可以创建一个与子项目背景颜色相同颜色的可绘制对象。然后将其设置为子分隔符。

ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
    sd1.getPaint().setColor(YOUR CHILD ITEM BACKGROUND COLOR);
    mExpListView.setChildDivider(sd1);

或者使用 xml:

android:childDivider="@color/child_bg_color"

If you want to remove child divider only, One simple trick is You can create a drawable with same color as of your child item's background color. Then set it as child divider.

ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
    sd1.getPaint().setColor(YOUR CHILD ITEM BACKGROUND COLOR);
    mExpListView.setChildDivider(sd1);

Or using xml:

android:childDivider="@color/child_bg_color"
長街聽風 2024-12-01 01:05:25

如果您想在 ExpandableListView (标题)的父元素之间有分隔符,并且不想在 Children 和 Parent 之间有分隔符(这显然是最自然的情况),那么您必须执行以下操作:

  1. 在您的父 xml 中文件在布局底部添加一个视图,其中包含您需要的分隔线高度和分隔线颜色。
    示例:

  2. 在子 xml 文件中执行相同操作。在布局底部添加一个视图,其中包含您需要的分隔线高度和分隔线颜色。确保分隔线颜色和高度与父 xml 中的相同。

  3. 进入您的 ExpandableListView Adapter 方法
    公共视图 getGroupView(int groupPosition, boolean isExpanded,
    视图convertView,ViewGroup父级)
    像这样管理视图分隔符的状态:
    示例:

    如果(已展开){
    headerDividerView.setBackgroundColor(ContextCompat.getColor(context,R.color.darker_grey));
    }别的{
    headerDividerView.setBackgroundColor(ContextCompat.getColor(context,android.R.color.transparent));
    }

:在我的例子中,R.color.darker_grey 是父级的背景颜色

  1. 从 ExpandableListView xml 中删除所有分隔线和分隔线高度。使 ExpandableListView 没有分隔线和分隔线高度。
    您可以像这样使用属性:

    android:groupIndicator="@null"
    android:childIndicator="@null"
    机器人:分频器=“@null”
    android:dividerHeight="0dp"

If you want to have divider between parent element of the ExpandableListView (Header) and don't want to have divider between children and parent , which is obviously the most natural case, then you have to do following:

  1. In your parent xml file add one View at the bottom of the layout with divider height and divider color that you need.
    Example:

  2. Do same in the child xml file. Add one View at the bottom of the layout with divider height and divider color that you need. Make sure divider color and height are same like in parent xml.

  3. Into your ExpandableListView Adapter method
    public View getGroupView(int groupPosition, boolean isExpanded,
    View convertView, ViewGroup parent)
    manage the state of the View divider like this:
    Example:

    if(isExpanded){
    headerDividerView.setBackgroundColor(ContextCompat.getColor(context,R.color.darker_grey));
    }else{
    headerDividerView.setBackgroundColor(ContextCompat.getColor(context,android.R.color.transparent));
    }

BTW : In my case R.color.darker_grey is background color of the parent

  1. Remove from your ExpandableListView xml all dividers and dividers height. Make ExpandableListView to not have dividers and divider height.
    You can use attrs like this :

    android:groupIndicator="@null"
    android:childIndicator="@null"
    android:divider="@null"
    android:dividerHeight="0dp"

街道布景 2024-12-01 01:05:25

我通过添加两个视图解决了这个问题,一个添加到标题,另一个添加到子项目,如下所示。

Header.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">

<RelativeLayout
android:id="@+id/rl_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/square_box"
android:padding="@dimen/padding_10">

<ImageView
    android:id="@+id/expandableIndicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:src="@drawable/ic_downarrow" />
</RelativeLayout>

<View
android:id="@+id/view_hidden"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_below="@+id/rl_header"
android:background="@android:color/transparent" />
</RelativeLayout>

child.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">

<RelativeLayout
android:id="@+id/rl_childView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/square_box"
android:padding="@dimen/padding_10">

<TextView
    android:id="@+id/tv_startDate"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:gravity="left"
    android:paddingTop="@dimen/padding_5"
    android:paddingBottom="@dimen/padding_5"
    android:text="Start Date \nSep. 23, 2019"
    android:textSize="@dimen/text_16" />

<ImageView
    android:id="@+id/iv_arrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:src="@drawable/right_arrow" />
</RelativeLayout>

<View
    android:id="@+id/view_hidden"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:layout_below="@+id/rl_childView"
    android:background="@android:color/transparent" />
</RelativeLayout>

现在将此代码添加到您的 Adapter 类中。这个想法是在组展开/折叠时显示/隐藏视图,在子项的最后一项上显示。

@Override
public View getGroupView(int listPosition, boolean isExpanded, View convertView, 
ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
String listCount = "(" + getChildrenCount(listPosition) + ")";

if (convertView == null) {
    LayoutInflater layoutInflater = (LayoutInflater) 
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = layoutInflater.inflate(R.layout.expandable_header, null);
}
View view_hidden = convertView.findViewById(R.id.view_hidden);

if (isExpanded) {
    view_hidden.setVisibility(View.GONE);
} else {
    view_hidden.setVisibility(View.VISIBLE);
}

return convertView;
}

@Override
public View getChildView(int listPosition, final int expandedListPosition,
                     boolean isLastChild, View convertView, ViewGroup parent) {
final Medication medication = (Medication) getChild(listPosition, 
expandedListPosition);
if (convertView == null) {
    LayoutInflater layoutInflater = (LayoutInflater) 
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = layoutInflater.inflate(R.layout.expandable_item, null);
}

View view_hidden = convertView.findViewById(R.id.view_hidden);
if (isLastChild) {
    view_hidden.setVisibility(View.VISIBLE);
} else {
    view_hidden.setVisibility(View.GONE);
}

return convertView;
}

I solved it by adding two views one to header and other to child item like shown below.

Header.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">

<RelativeLayout
android:id="@+id/rl_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/square_box"
android:padding="@dimen/padding_10">

<ImageView
    android:id="@+id/expandableIndicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:src="@drawable/ic_downarrow" />
</RelativeLayout>

<View
android:id="@+id/view_hidden"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_below="@+id/rl_header"
android:background="@android:color/transparent" />
</RelativeLayout>

child.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">

<RelativeLayout
android:id="@+id/rl_childView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/square_box"
android:padding="@dimen/padding_10">

<TextView
    android:id="@+id/tv_startDate"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:gravity="left"
    android:paddingTop="@dimen/padding_5"
    android:paddingBottom="@dimen/padding_5"
    android:text="Start Date \nSep. 23, 2019"
    android:textSize="@dimen/text_16" />

<ImageView
    android:id="@+id/iv_arrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:src="@drawable/right_arrow" />
</RelativeLayout>

<View
    android:id="@+id/view_hidden"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:layout_below="@+id/rl_childView"
    android:background="@android:color/transparent" />
</RelativeLayout>

Now add this code to your Adapter class. The idea is to show/hide the View when group is expanded/collapsed, show on child last item.

@Override
public View getGroupView(int listPosition, boolean isExpanded, View convertView, 
ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
String listCount = "(" + getChildrenCount(listPosition) + ")";

if (convertView == null) {
    LayoutInflater layoutInflater = (LayoutInflater) 
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = layoutInflater.inflate(R.layout.expandable_header, null);
}
View view_hidden = convertView.findViewById(R.id.view_hidden);

if (isExpanded) {
    view_hidden.setVisibility(View.GONE);
} else {
    view_hidden.setVisibility(View.VISIBLE);
}

return convertView;
}

@Override
public View getChildView(int listPosition, final int expandedListPosition,
                     boolean isLastChild, View convertView, ViewGroup parent) {
final Medication medication = (Medication) getChild(listPosition, 
expandedListPosition);
if (convertView == null) {
    LayoutInflater layoutInflater = (LayoutInflater) 
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = layoutInflater.inflate(R.layout.expandable_item, null);
}

View view_hidden = convertView.findViewById(R.id.view_hidden);
if (isLastChild) {
    view_hidden.setVisibility(View.VISIBLE);
} else {
    view_hidden.setVisibility(View.GONE);
}

return convertView;
}
久伴你 2024-12-01 01:05:25

像这样设置 android:divider="@null" 没有父母的分隔符 android:divider="2dp" 设置父母的分隔符

 <ExpandableListView
        android:id="@+id/expandable_list"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:divider="2dp"
        android:groupIndicator="@drawable/group_indicator" />

set like this android:divider="@null" no divider for parents android:divider="2dp" set divider for parents

 <ExpandableListView
        android:id="@+id/expandable_list"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:divider="2dp"
        android:groupIndicator="@drawable/group_indicator" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文