Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide row from ListView without taking up space

I have a ListView with an associated ArrayAdapter that displays its contents in several activities. Unfortunately it got necessary now, that my ListView in one of the settings does not display all its elements, but just the ones where "property" is not set to true. I would like to avoid to use two ArrayAdapters with different content, since then I somehow need to keep them in sync. I tried it like this (this method now just assumes that getView is called in the setting where we want to hide certain elements):

public View getView(int position, View convertView, Context context) {
....

    if(!arrayitems[position].isProperty()) { //arrayitems is the underlying array
        convertView.setVisibility(View.VISIBLE); 
    } 
    else {
        convertView.setVisibility(View.GONE); 
    }
    return convertView;
}

This works, however I get a white row if the element has isProperty == true. I would like to make the row invisible in the sense, that it does not take up any space anymore. Is that possible?

The used xml file for convertView looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/text_item_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="4dp"
            android:maxLines="1"
            android:ellipsize="none"
            android:textSize="12sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/text_item_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:textStyle="bold" />

    </LinearLayout>

     <TextView android:id="@+id/text_item_text"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:textSize="12sp"
         android:maxLines="3"
         android:ellipsize="none" />

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="2dp"
         android:orientation="horizontal" >

         <TextView
             android:id="@+id/text_item_rating"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginRight="4dp"
             android:textSize="10sp" />

         <TextView
             android:id="@+id/text_item_voted"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:textSize="10sp" />

     </LinearLayout>

</LinearLayout>

I tried to replay all android:layout_height="wrap_content" with "fill_parent" but it did not change anything...

Thanks!

like image 321
user1809923 Avatar asked Nov 30 '12 13:11

user1809923


2 Answers

Set the Visibility of all the contents of the list view to GONE and then set the visibility of the View to Gone.... this will hide the row without occupying space..... it worked for me even i have been searching for this but after a research i found this....

EDIT 1 : Setting the visibility of the View to GONE is enough. No need to set the child element's visibility.

like image 145
Gowtham Raj Avatar answered Sep 23 '22 10:09

Gowtham Raj


I'm having same problem as you've

I've figured it out by inflating another layout layout with no height and width

Adapter.java

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (YOUR_CONDITION) {
            return inflater.inflate(R.layout.blank_layout, parent,
                    false);
    } else {
            View vi = inflater.inflate(R.layout.list_item, parent, false);
            return vi;
    }

}

And blank layout will be like this,

blank_layout.xml

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


</LinearLayout>
like image 45
Jaydipsinh Zala Avatar answered Sep 22 '22 10:09

Jaydipsinh Zala