在ListView中隐藏行而不占用空间

10

我有一个带有关联ArrayAdapter的ListView,它在多个活动中显示其内容。不幸的是,现在在其中一个设置中,我的ListView不显示所有元素,而只显示其中“property”未设置为true的元素。我想避免使用具有不同内容的两个ArrayAdapters,因为那样我需要以某种方式保持它们同步。我尝试了以下方法(此方法现在假设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;
}

这段代码能够运行,但是如果元素的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_height="wrap_content"替换为"fill_parent",但是没有任何变化...

谢谢!


我不明白,布尔属性对于所有元素都是相同的,因为它是适配器的属性。它怎么知道要隐藏哪一行? - Romain Piel
抱歉,isProperty() 当然是数组中项目的一个方法。 - user1809923
如果仍然有问题,请检查我的答案,它一定能起作用。 - Jaydipsinh Zala
6个回答

9
将列表视图中的所有内容的可见性设置为GONE,然后将View的可见性设置为GONE......这将隐藏行而不占用空间......即使我一直在搜索这个问题,但经过研究我找到了这个解决办法。
编辑1:只需要将View的可见性设置为GONE。无需设置子元素的可见性。

7

我遇到了和你一样的问题。

我通过充气另一个没有高度和宽度的布局解决了这个问题。

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

}

空白布局将会是这样的,
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>

很好的解决方案,根据条件只需传递“blank_layout”。 - garish

4
所以Romain的意思是给你的LinearLayout加一个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">

那么在你的Java代码中,你需要这样做:
    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;
    }

如果您的页面展示方式不同,您可能需要稍微更改一下。我亲自尝试过这种方法,效果非常好。


1
尽管我使用了 View.GONE,但是在列表视图的末尾仍然有很多空白空间,用户可以滚动查看。为了解决这个问题,我更新了 getCount() 方法,将隐藏行从总数中排除。更清晰的实现方式是保留两个 ArrayList:一个用于原始数据,另一个用于适配器,该适配器排除了隐藏行的数据。 - Someone Somewhere
我以前做过这个,我不喜欢的是你需要一直监管额外的数据。 - MinceMan
是的,我同意 - 这就是为什么我没有选择这种方法来处理这个特定实例的原因 :-) 我在上面的评论中忘记提到一点:一旦 getCount() 返回一个排除你想要隐藏的数据行的值,你应该将隐藏的数据行定位到适配器使用的数组的末尾。 - Someone Somewhere

3
你可以使用 Android 的 getview 布局,将 android:layout_height 设为 "wrap_content",并使用 Visibility gone 来隐藏你的布局。

抱歉,我不明白你在说什么。你能否澄清一下你的解决方案? - user1809923

1

谢谢,但是我无法转移他的解决方案。我已经添加了我的用于convertView的xml文件。 - user1809923

-2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接