【问题标题】:Android - ListView with ArrayAdapter and hidden TextViewsAndroid - 带有 ArrayAdapter 和隐藏 TextViews 的 ListView
【发布时间】:2015-08-24 21:23:57
【问题描述】:

我的 Android 应用程序的 ListView 中的 ArrayAdapter 有问题。应该显示的 TextView 有两种可能,带有信息文本或不带信息文本。滚动时出现问题。当我再次向下和向上滚动时,没有信息文本的 TextView 现在有一个随机信息文本(来自带有信息文本的 TextView 之一)。问题出现是因为 ListView 的内存使用情况。现在我正在寻找解决问题的可能性。结果应该在没有信息文本的滚动后显示 TextViews(没有信息文本)。那么在 ListView 中使用 ArrayAdapter 有什么好的解决方案,或者还有其他 Android 小部件可以使用吗?

Java 代码(Activity 类):

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        View view = convertView;
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(layout, null);

            holder.text = (TextView) view.findViewById(R.id.list_layout_textview);
            holder.infoText = (TextView) view.findViewById(R.id.list_layout_infos);
            view.setTag(holder);

        } else {
            holder = (ViewHolder) view.getTag();
        }

        final OutputTweet outputTweet = tweetObjects.get(position);

        if (outputTweet.getText() != null) {
            holder.text.setText(outputTweet.getText());
        }

        if (!outputTweet.getInfoText().equals("")) {
            holder.infoText.setVisibility(View.VISIBLE);
            holder.infoText.setText(outputTweet.getInfoText());
        }


        return view;
    }
}

布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_layout"
>

<TextView
    android:id="@+id/list_layout_infos"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/text_info_color"
    android:layout_marginTop="2dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:textSize="12sp"
    android:textStyle="italic"
    android:visibility="gone"
    />

<TextView
    android:id="@+id/list_layout_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/textColor"
    android:layout_marginTop="2dp"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:fontFamily="sans-serif-light"
    android:typeface="sans"
    android:textSize="15sp"
     />

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"/>

</LinearLayout>

感谢您的每一个解决方案:)

【问题讨论】:

    标签: java android listview android-listview android-arrayadapter


    【解决方案1】:

    您需要在最后一个 if 子句中添加一个 else 子句,将视图设置回 View.GONE 或 View.INVISIBLE。该视图正在被回收,并且由于之前已显示,因此在将其传递回给您时仍然可见。

    通常在绑定时您需要绑定所有状态,包括可见性更改。不要因为视图的重用而从通货膨胀中假设任何状态。

    试试这个:

        if (outputTweet.getText() != null) {
            holder.text.setText(outputTweet.getText());
        } else {
            holder.text.setText("");
        }
    
        if (!outputTweet.getInfoText().equals("")) {
            holder.infoText.setVisibility(View.VISIBLE);
            holder.infoText.setText(outputTweet.getInfoText());
        } else {
            holder.infoText.setVisibility(View.GONE);
        }
    

    【讨论】:

    • 是的,这很完美。感谢您的解决方案! :)
    猜你喜欢
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多