Android 从ListView隐藏行而不占用空间

Android 从ListView隐藏行而不占用空间,android,android-listview,Android,Android Listview,我有一个带有关联ArrayAdapter的ListView,它在几个活动中显示其内容。不幸的是,现在有必要在其中一个设置中显示我的ListView,它不会显示它的所有元素,而只显示那些“属性”未设置为true的元素。我希望避免使用两个具有不同内容的ArrayAdapter,因为那时我需要以某种方式使它们保持同步。我这样尝试过(该方法现在只假设在要隐藏某些元素的设置中调用getView): 这是可行的,但是如果元素的isProperty==true,我会得到一个白色的行。我想让这一行在某种意义上看

我有一个带有关联ArrayAdapter的ListView,它在几个活动中显示其内容。不幸的是,现在有必要在其中一个设置中显示我的ListView,它不会显示它的所有元素,而只显示那些“属性”未设置为true的元素。我希望避免使用两个具有不同内容的ArrayAdapter,因为那时我需要以某种方式使它们保持同步。我这样尝试过(该方法现在只假设在要隐藏某些元素的设置中调用getView):

这是可行的,但是如果元素的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>

我试图用“填充父对象”重播所有android:layout\u height=“wrap\u content”,但它没有改变任何东西


谢谢

另一个用户也有同样的问题,他通过以下方式解决了此问题:

我希望这能帮助你


问候你可以使用android的getview布局:layout\u height=“wrap\u content”并使用Visibility Goe隐藏你的布局,所以Romain的意思是在你的线性布局中添加一个id

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

如果你以不同的方式夸大你的观点,你可能需要改变一下。我自己也做过,效果非常好。

将列表视图所有内容的可见性设置为GONE,然后将视图的可见性设置为GONE。。。。这将隐藏行而不占用空间。。。。。即使我一直在寻找它,它也对我有效,但经过研究,我发现了这个

编辑1:
将视图的可见性设置为“消失”就足够了。无需设置子元素的可见性

我和你有同样的问题

我已经通过
膨胀另一个没有高度和宽度的布局来解决了这个问题

Adapter.java

空白的布局是这样的

blank_layout.xml



通过修改getCount()和getView()中的位置以及您的逻辑,您可以通过@sherif elkhatib使其用于示例检查

我不明白,布尔属性对于所有元素都是相同的,因为它是适配器的一个属性。它怎么知道要隐藏哪一行?抱歉,isProperty()当然是数组中项的一种方法。如果仍然有问题,请检查我的答案,它肯定会工作。谢谢,但是我无法转移他的解决方案。我在上面为convertView添加了我用过的xml文件。对不起,我不明白你在说什么。您能澄清一下您的解决方案吗?尽管我使用了
View.GONE
listview的末尾有一堆空白,用户可以滚动浏览。为了解决这个问题,我更新了
getCount()
,将隐藏行从总计数中排除。一个更干净的实现是保留两个ArrayList:一个用于原始数据,另一个是适配器,它排除隐藏行的数据。我以前做过,但我不喜欢的是,你当时正在照看婴儿的额外数据。是的,我同意-这就是为什么我没有在这个特定实例中使用该方法:-)我忘了在上面的评论中提到:once
getCount()
返回一个值,该值不包括要隐藏的数据行,您应该将隐藏的数据行定位到适配器使用的数组的末尾。好的解决方案只是根据条件传递“blank_布局”。
<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">
    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;
    }
@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;
    }

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