【问题标题】:Hide row from ListView without taking up space从 ListView 隐藏行而不占用空间
【发布时间】:2012-11-18 17:29:18
【问题描述】:

我有一个带有关联 ArrayAdapter 的 ListView,它在多个活动中显示其内容。不幸的是,现在有必要在其中一个设置中的 ListView 不显示其所有元素,而只是显示“属性”未设置为 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;
}

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

我试图用“fill_parent”重播所有 android:layout_height="wrap_content" 但它没有改变任何东西......

谢谢!

【问题讨论】:

  • 我不明白,布尔属性对于所有元素都是相同的,因为它是适配器的属性。它怎么知道要隐藏哪一行?
  • 对不起,isProperty()当然是数组中项的方法
  • 如果仍有问题,请检查我的答案,它肯定会起作用。

标签: android android-listview


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我遇到了和你一样的问题

    我已经被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;
        }
    
    }
    

    而空白布局会是这样的,

    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”。
    【解决方案3】:

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

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

    【讨论】:

    • 尽管我使用了View.GONE,但我在列表视图的末尾有一堆空白空间,用户可以滚动浏览。为了解决这个问题,我更新了getCount() 以从总数中排除隐藏的行。一个更简洁的实现是保留两个数组列表:一个用于原始数据,另一个用于排除隐藏行数据的适配器。
    • 我以前做过,我不喜欢的是你当时的宝宝坐了额外的数据。
    • 是的,我同意 - 这就是为什么我没有为这个特定实例走这条路 :-) 我忘了在上面的评论中提到:一旦 getCount() 返回一个不包括您的数据行的值要隐藏,您应该将隐藏的数据行定位到适配器使用的数组的末尾。
    【解决方案4】:

    您可以使用 android:layout_height="wrap_content" 的 getview 布局并使用 Visibility Got 隐藏您的布局

    【讨论】:

    • 对不起,我不明白你在说什么。你能澄清你的解决方案吗?
    【解决方案5】:

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

    我希望这可以帮助你。

    问候

    【讨论】:

    • 谢谢,但我无法转移他的解决方案。我为上面的 convertView 添加了我用过的 xml 文件。
    【解决方案6】:

    通过使用您的逻辑修改 getCount() 以及 getView() 中的位置,您可以使其适用于 @sherif-elkhatib 的示例 Check http://www.sherif.mobi/2012/01/listview-with-ability-to-hide-rows.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 2017-06-29
      • 2015-07-25
      • 2011-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多