java - Android - 带有 ArrayAdapter 和隐藏 TextView 的 ListView

标签 java android listview android-listview android-arrayadapter

我的 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>

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

最佳答案

您需要在最后一个 if 子句中添加一个 else 子句,将 View 设置回 View.GONEView.INVISIBLE。该 View 正在被回收,并且由于它之前已显示过,因此当它传回给您时它仍然是可见的。

一般来说,绑定(bind)时您需要绑定(bind)所有状态,包括可见性更改。不要因为 View 的重用而假设通货膨胀的任何状态。

试试这个:

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

关于java - Android - 带有 ArrayAdapter 和隐藏 TextView 的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32191964/

相关文章:

java - 当其他 fragment 更改第一个 fragment 模型时刷新 fragment

java - Android:获取当前打开的应用程序的栈(数组)

java - 在 Java 中请求输入时忽略 nextLine

wpf - 如何让 ListView(或 DataGrid)与 TextWrapping (WPF) 一起使用

jQuery 移动 : Pull to refresh list view

android - 无法使用自定义适配器单击 ListView 中的项目

java - JSF 是一个框架还是一个 API?

android-activity - Android 'kill' 一个Activity 可以不杀掉应用吗?

android - 更改升级时的默认首选项

android - 动态设置 FAB anchor 重力