从ListView隐藏行而不占用空间

use*_*923 10 android android-listview

我有一个ListView与一个关联的ArrayAdapter,它在几个活动中显示其内容.不幸的是现在有必要,我的ListView在其中一个设置中不会显示其所有元素,而只是"property"未设置为true的那些元素.我想避免使用两个具有不同内容的ArrayAdapter,因为那时我不知何故需要保持它们同步.我这样尝试过(这个方法现在假设在我们想要隐藏某些元素的设置中调用getView):

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;
}
Run Code Online (Sandbox Code Playgroud)

这有效,但是如果元素的isProperty == true,我会得到一个白色的行.我想在这个意义上让这行不可见,它不再占用任何空间.那可能吗?

用于convertView的xml文件如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

我尝试用"fill_parent"重放所有android:layout_height ="wrap_content",但它没有改变任何东西......

谢谢!

Gow*_*Raj 9

将列表视图的所有内容的可见性设置为GONE,然后设置View to Gone的可见性....这将隐藏行而不占用空间.....它对我有效甚至我一直在搜索这个,但经过一项研究,我发现了这....

编辑1: 将视图的可见性设置为GONE就足够了.无需设置子元素的可见性.


Jay*_*ala 5

我和你有同样的问题

我已经知道了 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;
    }

}
Run Code Online (Sandbox Code Playgroud)

空白布局将是这样,

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>
Run Code Online (Sandbox Code Playgroud)


小智 1

另一位用户也遇到了同样的问题,他这样解决了: https: //stackoverflow.com/a/7928923/1548679

我希望这可以帮助你。

问候