ListView在列表的下方和上方留出额外的空间

时间:2016-10-20 05:57:43

标签: android xml listview android-recyclerview

我有几个名字的动态列表。但是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>

但是,我明白了,

enter image description here

修改

删除android:layout_centerInParent="true"后:

enter image description here

在我的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;
    }
}

我在这里真的很难过。那么,从列表视图中删除该空间可以做些什么呢?

3 个答案:

答案 0 :(得分:2)

这不是由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"属性。

答案 1 :(得分:0)

你应该添加一个背景颜色(例如#F00)来查看问题是与listview有关还是与容器有关。

之后我会在listview中使用它

机器人:fillViewport =&#34;真&#34;

不确定所以试一试

答案 2 :(得分:0)

您在列表视图中定义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>
相关问题