ExpandableListView - 如何仅在父元素之间设置分隔符
我只想在父元素之间放置分隔线。当我设置
android:divider="@drawable/divider"
android creates divider between parent elements, but creates divider between child elements too. When i addandroid:childDivider="@color/transparent"
android removes the divider between child elements, but the free space between them remains. Why? I have tried toandroid: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
为了仅从子视图中删除分隔符,而不是在可扩展列表中的父视图之间删除分隔符,
请在 XML 的
ExapandableListView
属性中添加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 theExapandableListView
attributes in XML:Refer this page for more information
指定分隔线颜色及其高度(当组折叠时分隔线将可见)
Give Divider color and its height (dividers will be visible when groups are collapsed)
诀窍是为折叠组布局和最后一个子布局创建一个额外的
视图
。crash_group.xml:
expanded_group.xml:
并对子视图应用相同的技术,为最后一个子视图插入额外的视图
The trick is to create an extra
view
for collapse group layout and last child layout.collapse_group.xml:
expanded_group.xml:
And apply the same technique to the child view, for the last child view insert the extra view
我所做的是将 ExpandableListView 的分隔符设置为 0,然后将“分隔符”组插入到它的 ExpandableListAdapter 中:
What I did was to set the ExpandableListView's divider to 0 and then insert "divider" groups into it's ExpandableListAdapter:
如果您只想删除子分隔线,一个简单的技巧是您可以创建一个与子项目背景颜色相同颜色的可绘制对象。然后将其设置为子分隔符。
或者使用 xml:
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.
Or using xml:
如果您想在 ExpandableListView (标题)的父元素之间有分隔符,并且不想在 Children 和 Parent 之间有分隔符(这显然是最自然的情况),那么您必须执行以下操作:
在您的父 xml 中文件在布局底部添加一个视图,其中包含您需要的分隔线高度和分隔线颜色。
示例:
在子 xml 文件中执行相同操作。在布局底部添加一个视图,其中包含您需要的分隔线高度和分隔线颜色。确保分隔线颜色和高度与父 xml 中的相同。
进入您的 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 是父级的背景颜色
从 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:
In your parent xml file add one View at the bottom of the layout with divider height and divider color that you need.
Example:
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.
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
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"
我通过添加两个视图解决了这个问题,一个添加到标题,另一个添加到子项目,如下所示。
Header.xml
child.xml
现在将此代码添加到您的 Adapter 类中。这个想法是在组展开/折叠时显示/隐藏视图,在子项的最后一项上显示。
I solved it by adding two views one to header and other to child item like shown below.
Header.xml
child.xml
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.
像这样设置
android:divider="@null"
没有父母的分隔符android:divider="2dp"
设置父母的分隔符set like this
android:divider="@null"
no divider for parentsandroid:divider="2dp"
set divider for parents