gpt4 book ai didi

java - 如何删除 ExpandableListView 中父子之间的特定空间

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:54 30 4
gpt4 key购买 nike

你能帮我确定为什么小组和 child 之间有空间吗?在我的例子中,我希望所有组之间都有空格, child 应该在组的正下方没有空格(但当然是下一组的空间。我的问题是这样的: enter image description here

我已将组分隔线设置为此可绘制对象 (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"/>

自定义 View 类:

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;
}
}

评论图片: enter image description here

最佳答案

这已经过测试并证明有效,我什至想在几个月后回到这个问题,因为没有其他方法能给我提供我想要的结果。这些显示的正是 OP(和我自己)想要的

这是我的群组或类别之类的。基本上第一个线性布局只是一个 View 的包装器,它充当类别之间的间隔,然后是另一个处理我所有格式的线性布局

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

<?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>

然后在 child 身上我有这个。

<?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>

关于java - 如何删除 ExpandableListView 中父子之间的特定空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24955173/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com