Hide row from ListView without taking up space

感情迁移 提交于 2019-12-04 02:14:35

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.

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

So what Romain meant was to add an id to your LinearLayout

<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"
    android:id="@+id/linear_layout">

Then in your Java you would do

    public View getView(int position, View convertView, Context context) {
    ....
    holder.lLayout = (LinearLayout) convertView.findViewById(R.id.linear_layout);
    ....
    if(!arrayitems[position].isProperty()) { //arrayitems is the underlying array
        holder.lLayout.setVisibility(View.VISIBLE); 
    } 
    else {
        holder.lLayout.setVisibility(View.GONE); 
    }
    return convertView;
    }

You may need to change it a bit if your inflating your views differently. I've done this myself and it works really well.

You can use the getview layout of android:layout_height="wrap_content" and do use Visibility gone to hide your layout

sascha10000

Another user had the same problem and he solved it this way: https://stackoverflow.com/a/7928923/1548679 .

I hope this could help you.

Greets

By modifying getCount() and also the position in getView() with your logic you can make it work for sample Check http://www.sherif.mobi/2012/01/listview-with-ability-to-hide-rows.html by @sherif-elkhatib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!