Satan Pandeya
Satan Pandeya

Reputation: 3815

ListView leaves extra space below and above of the list

I have dynamic list of several name. But ListView leaves extra space below and above of the total list item.

At routes_recycler_listview.xml, Here, I have use this:

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

But, i got this ,

enter image description here

Edit

After removing android:layout_centerInParent="true" :

enter image description here

In my RecyclerViewRecyclerAdapter class, i use following:

At ViewHolder class :

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

At onBindViewHolder method:

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

At onCreateViewHolder method :

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

In my CustomAdapter class:

At getItem method :

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

At getView method :

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

My getter and setter method in Routes class. Here,

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

I have really hard time here. So, what can be done to remove that space from the the listview ?

Upvotes: 1

Views: 632

Answers (3)

SanthoshN
SanthoshN

Reputation: 629

you have android:layout_centerInParent="true" defined in your listview which is causing this, try removing that

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

Upvotes: 0

Pankaj Kumar
Pankaj Kumar

Reputation: 82968

This is not done by ListView, this is done by the custom relative layout and tag which which you are using with 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" />

So remove this attribute from ListView which will solve your problem.

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

You can use android:layout_alignParentTop="true" attribute into your ListView also.

Upvotes: 2

Alberto Cappellina
Alberto Cappellina

Reputation: 327

you should add a background color (eg. #F00) to see if the problem is with listview or with its container.

after that i will use this in listview

android:fillViewport="true"

not sure so just give it a try

Upvotes: 1

Related Questions