如何在ExpandableListView中删除父级和子级之间的特定空间

问题描述 投票:10回答:10

你能帮我弄清楚为什么团体和孩子之间有空间吗?在我的情况下,我想要所有组之间的空格,孩子应该在组的正下方没有空间(但当然空间到下一组。我的问题看起来像这样:

我已将Group divider设置为这个drawable(expandable_list_group_divider):

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:height="10dp"/>
</shape>

和子分隔符(@ drawable / expandable_list_child_divider):

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:height="0dp"/>
</shape>

我在xml中定义了这样的布局(它是一个复合控件):

<com.jws.MyExpandableListView
        android:id="@+id/expList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:scrollbars="none"
        android:divider="@drawable/expandable_list_group_divider"
        android:childDivider="@drawable/expandable_list_child_divider"/>

自定义视图类:

public class MyExpandableListView extends ExpandableListView {
    protected ArrayList<Departure> departures;

    public MyExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);

        setGroupIndicator(null);
        setChildIndicator(null);
        setHorizontalFadingEdgeEnabled(true);
        setVerticalFadingEdgeEnabled(true);
        setFadingEdgeLength(60);

        departures = new ArrayList<Departure>();
        this.setAdapter(new MyExpandableListAdapter(context, this, departures));
    }
}

最后是可扩展列表适配器

public class DeparturesExpandableListAdapter extends BaseExpandableListAdapter {
    private final int DIVIDER_HEIGHT = 20;
    private LayoutInflater inflater;
    private ArrayList<Departure> departures;
    private Context context;
    private ExpandableListView expListView;

    public DeparturesExpandableListAdapter(Context context, ExpandableListView expListView, ArrayList<Departure> departures)
    {
        this.context = context;
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        this.departures = departures;
        this.expListView = expListView;
    }


    // This Function used to inflate parent rows view
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parentView)
    {
        final Departure departure = departures.get(groupPosition);

        if(!isExpanded)
            expListView.setDividerHeight(DIVIDER_HEIGHT);
        else
            expListView.setDividerHeight(-4);

        expListView.setFooterDividersEnabled(false);

        // Inflate grouprow.xml file for parent rows
        convertView = inflater.inflate(R.layout.view_departure_overview, parentView, false);

        //Data handling in the view...

        return convertView;
    }


    // This Function used to inflate child rows view
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                             View convertView, ViewGroup parentView)
    {
        final Departure departure = departures.get(groupPosition);

        if(isLastChild)
            expListView.setDividerHeight(DIVIDER_HEIGHT);

        // Inflate childrow.xml file for child rows
        convertView = inflater.inflate(R.layout.view_departure_details, parentView, false);

        //Data handling...

        return convertView;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
        return departures.get(groupPosition);
    }

    //Call when child row clicked
    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
        return childPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition)
    {
        return 1;
    }


    @Override
    public Object getGroup(int groupPosition)
    {
        return departures.get(groupPosition);
    }

    @Override
    public int getGroupCount()
    {
        if(departures != null)
            return departures.size();

        return 0;
    }

    //Call when parent row clicked
    @Override
    public long getGroupId(int groupPosition)
    {
        return groupPosition;
    }

    @Override
    public void notifyDataSetChanged()
    {
        // Refresh List rows
        super.notifyDataSetChanged();
    }

    @Override
    public boolean isEmpty()
    {
        return ((departures == null) || departures.isEmpty());
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        return false;
    }

    @Override
    public boolean hasStableIds()
    {
        return false;
    }

    @Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }
}

评论图片:

java android expandablelistview
10个回答
5
投票

这是经过测试并证明是有效的,我想在几个月后再回到这一点,因为其他方法都没有给我我想要的结果。这些都是OP(和我自己)想要的

这是我的团体或类别或其他什么。基本上第一个线性布局只是一个视图的包装器,它作为类别之间的间隔,然后是另一个线性布局,处理我的所有格式

在ExpandableListView(未显示)中,我将分隔符高度设置为0.间距由视图处理

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <View
        android:layout_width="fill_parent"
        android:layout_height="6dp"
        android:background="@android:color/transparent"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp"
    android:background="#50601CBA">


    <TextView
        android:id="@+id/lblListHeader"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="17dp"
        android:textColor="#FFFFFF" />

</LinearLayout>
</LinearLayout>

然后在孩子我有这个。

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

    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:textColor="#FFFFFF"
        android:background="#50601CBA"
         />

</LinearLayout>

0
投票
Just set the child background color as same of exandableListView

example:


    <ExpandableListView
        android:id="@+id/expandableListView"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:indicatorLeft="@null"
        android:clipChildren="true"
        android:background="@color/colorGrayTransparent"
         />
and child item color like this :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    android:layout_marginBottom="12dp"
    android:background="@color/colorGrayTransparent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
....
</LinearLayout>

3
投票

我在xml文件中使用了以下设置,父级和子级之间没有间隙。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#f4f4f4" >

            <ExpandableListView
                android:id="@+id/expListView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/bg_black"
                android:divider="#FF8800"
                android:dividerHeight="2px" />

</RelativeLayout>

最有可能的是这是一个你有余量设置的问题。


2
投票

删除所有边距和分隔线高度,而在groupview本身顶部添加一个空格。它应该做的伎俩。


2
投票

假设你想要父母之间的间隔,而不是父母和孩子之间的间距。我没有找到实现它的正确方法。所以我做了一个不聪明的修复,不设置分隔符高度,而是因为我有每个父项的自定义布局,我在自定义父布局的顶部添加了一个固定高度为25dp的LinearLayout,因此我可以实现所需的功能,但是有副作用,当您触摸列表项时,间距以及列表项都会在触摸时突出显示。


2
投票

在view_departure_details中为子视图使用负边距。这样的事情:

<TextView
android:id="text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="-10dp"
android:text="@string/my_best_text"
android:background="#FF0000"
/>

我知道有些人认为使用负边际是一种不好的做法,但这似乎是最简单的方法。

其他方式(耗时的方式)是实现您自己的可扩展列表视图,以便您可以更好地控制事物的绘制方式。


2
投票

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

在XML中的ExapandableListView属性中添加android:childDivider =“#00000000”:

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

请参阅http://qtcstation.com/2011/03/working-with-the-expandablelistview-part-1/


0
投票

以编程方式设置子视图,如:

expandableListView.setDividerHeight(1);

expandableListView.setChildDivider(new ColorDrawable(Color.Black);

和组视图从xml像:

    <TextView
        android:id="@+id/tv_title_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <ImageView
        android:id="@+id/iv_arrow"
        android:layout_width="20dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_height="20dp"
         />


<View
    android:id="@+id/header_view"
    android:layout_width="match_parent"
    android:layout_marginTop="10dp"
    android:layout_alignParentBottom="true"
    android:layout_height="1dp" />

0
投票

您可以在它们之间设置透明度,例如..

        android:childDivider="@android:color/transparent"

0
投票

使用视图的public View getChildView()方法在方法setPadding()中修改视图的填充。

例:

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                             View convertView, ViewGroup parentView){
 final Departure departure = departures.get(groupPosition);

 if(isLastChild)
  expListView.setDividerHeight(DIVIDER_HEIGHT);

 // Inflate childrow.xml file for child rows
 convertView = inflater.inflate(R.layout.view_departure_details, parentView, false);

 //Data handling...

 convertView.setPadding(0, 0, 0, 0);

 return convertView;
}

同样,如果要修改组的填充,则必须修改getGroupView()方法中的填充。

© www.soinside.com 2019 - 2024. All rights reserved.