【问题标题】:ListView leaves extra space below and above of the listListView 在列表下方和上方留下额外空间
【发布时间】:2016-10-20 05:57:43
【问题描述】:

我有几个名字的动态列表。但是ListView 在整个列表项的下方和上方留下了额外的空间。

routes_recycler_listview.xml,这里,我用过这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="center_vertical"
            android:paddingEnd="0dp"
            android:paddingStart="10dp"
            android:textColor="@color/md_black_1000"
            android:textSize="20sp" />
    </RelativeLayout>

    <com.github.aakira.expandablelayout.ExpandableRelativeLayout
        android:id="@+id/expandableLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/header"
        android:background="@drawable/recycler_item_bg"
        app:ael_duration="400"
        app:ael_expanded="true"
        app:ael_orientation="vertical">

        <ListView
            android:id="@+id/lv_routes_assigned"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textColor="@color/fontColor"
            android:textSize="16sp" />
    </com.github.aakira.expandablelayout.ExpandableRelativeLayout>
</RelativeLayout>

但是,我明白了,

编辑

删除android:layout_centerInParent="true"后:

在我的RecyclerViewRecyclerAdapter 课程中,我使用以下内容:

ViewHolder 班级:

public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;
        public ListView routesAssign;
        public ExpandableRelativeLayout expandableLayout;

        public ViewHolder(View v) {
            super(v);
            textView = (TextView) v.findViewById(R.id.textView);
            routesAssign = (ListView) v.findViewById(R.id.lv_routes_assigned);
            expandableLayout = (ExpandableRelativeLayout) v.findViewById(R.id.expandableLayout);
        }
    }

onBindViewHolder方法:

holder.textView.setText(item.getDateString());
        CustomAdapter testAdapter = new CustomAdapter(context, item.getRoutes());

        holder.routesAssign.setAdapter(testAdapter);
        holder.routesAssign.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                Intent i = new Intent(context, RoutesShapesActivity.class);
                Log.e("id - onItemClick", item.getRoutes().get(pos).getId() + "-- Test --");
                i.putExtra("route_id", item.getRoutes().get(pos).getId());
                i.putExtra("assigned_id", item.getRoutes().get(pos).getAssignId());
                i.putExtra("update", item.getRoutes().get(pos).getIsUpdated());
                context.startActivity(i);
            }
        });

        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                item.getRoutes().size() * 200);

        p.addRule(RelativeLayout.BELOW, R.id.header);
        p.addRule(RelativeLayout.CENTER_IN_PARENT, R.id.header);

        holder.expandableLayout.setLayoutParams(p);
        holder.expandableLayout.setInterpolator(Utils.createInterpolator(Utils.DECELERATE_INTERPOLATOR));
        holder.expandableLayout.setExpanded(true);

onCreateViewHolder 方法:

@Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
        this.context = parent.getContext();
        return new ViewHolder(LayoutInflater.from(context)
                .inflate(R.layout.routes_recycler_listview, parent, false));
    }

在我的CustomAdapter 课堂上:

getItem 方法:

@Override
    public Routes getItem(int position) {
        return data.get(position);
    }

getView 方法:

public View getView(int position, View convertView, ViewGroup parent) {

        // Get the data item for this position
        Routes route = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.route_assigned_list, parent, false);
        }
        // Lookup view for data population
        TextView tvName = (TextView) convertView.findViewById(R.id.route_assign_text_view);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.route_assign_pause_state);

        Log.e("Test --- ", route.getRouteName());
        tvName.setText(route.getRouteName());
return convertView;
}

Routes 类中的 getter 和 setter 方法。这里,

public class Routes {
//
//    "assignId": 22,
//            "routeId": 15,
//            "routeName": "NY-CLSTR23",
//            "createdDate": "2016-04-15 09:51:45"

    @SerializedName("assignId")
    private int assignId;
    @SerializedName("assignDate")
    private String assignDate;
    @SerializedName("routeId")
    private int id;
    @SerializedName("routeName")
    private String routeName;
    @SerializedName("createdDate")
    private String createdDate;
    @SerializedName("isUpdated")
    private String isUpdated;

    /*"assignId": 167,
            "assignDate": "2016-06-17",
            "routeId": 137,
            "routeName": "Test June 16",
            "createdDate": "2016-06-16 00:14:27",
            "isUpdated": "true"*/

    public int getAssignId() {
        return assignId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getRouteName() {
        return routeName;
    }

    public void setRouteName(String routeName) {
        this.routeName = routeName;
    }

    public String getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }

    public void setAssignId(int assignId) {
        this.assignId = assignId;
    }

    public String  getAssignDate() {
        return assignDate;
    }

    public void setAssignDate(String assignDate) {
        this.assignDate = assignDate;
    }

    public String getIsUpdated() {
        return isUpdated;
    }

    public void setIsUpdated(String isUpdated) {
        this.isUpdated = isUpdated;
    }
}

我在这里真的很难过。那么,如何从 listview 中删除该空间?

【问题讨论】:

  • marginTop="-15dp" 添加到ExpandableRelativeLayout

标签: android xml listview android-recyclerview


【解决方案1】:

这不是由 ListView 完成的,这是由您与 ListView 一起使用的自定义相对布局和标签完成的 android:layout_centerInParent="true"

<ListView
            android:id="@+id/lv_routes_assigned"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textColor="@color/fontColor"
            android:textSize="16sp" />

因此,从 ListView 中删除此属性将解决您的问题。

<ListView
                android:id="@+id/lv_routes_assigned"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"              
                android:textColor="@color/fontColor"
                android:textSize="16sp" />

您也可以在 ListView 中使用android:layout_alignParentTop="true" 属性。

【讨论】:

  • 删除android:layout_centerInParent="true"后,会删除列表上方的空间,但列表下方仍有空间。
  • 为此,您可以将android:layout_height="wrap_content" 更改为android:layout_height="match_parent"。或者android:fillViewport="true"也可以使用。
  • android:layout_height="match_parent" 之后,列表下方仍有空格。删除 android:layout_centerInParent="true" 后,我添加了另一个列表图像。该列表采用了一些默认高度。现在我该怎么办?
  • 当我在我身边检查时它可以工作,但我使用的是 RelativeLayout 而不是自定义的。只需将父RelativeLayout的android:layout_height="wrap_content"更改为android:layout_height="match_parent",再试一次。
【解决方案2】:

您应该添加背景颜色(例如#F00)以查看问题出在列表视图还是其容器。

之后我将在列表视图中使用它

android:fillViewport="true"

不确定,试试看

【讨论】:

    【解决方案3】:

    您的列表视图中定义了 android:layout_centerInParent="true" 导致此问题,请尝试删除它

        <com.github.aakira.expandablelayout.ExpandableRelativeLayout
           android:id="@+id/expandableLayout"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:minHeight="0dp" // this is just in case if your customview is setting some minheight
           android:layout_below="@id/header"
           android:background="@drawable/recycler_item_bg"
           app:ael_duration="400"
           app:ael_expanded="true"
           app:ael_orientation="vertical">
    
        <ListView
            android:id="@+id/lv_routes_assigned"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/fontColor"
            android:textSize="16sp" />
    </com.github.aakira.expandablelayout.ExpandableRelativeLayout>
    

    【讨论】:

    • 使用android:layout_centerInParent="true" this 后,列表上方的空格被删除,但列表下方有空格。
    • 您可以在ExpandableRelativeLayout 上使用android:layout_height="wrap_content" 也可以用于您拥有的设计,您根本不需要RelativeLayout。您可以使用具有垂直方向的 LinearLayout 和 TextView 作为标题,然后使用 ListView 作为列表内容。
    • 如何删除列表下方的空格?
    • 设置 ExpandableRelativeLayout 高度为wrap_content