gpt4 book ai didi

android - ListFragment 中的自定义行元素在滚动后显示不正确

转载 作者:行者123 更新时间:2023-11-29 22:00:37 26 4
gpt4 key购买 nike

我有一个 ListFragment,其中包含使用自定义 ArrayAdapter 填充的 Earthquake 对象列表。每个元素要么“突出显示”,要么不“突出显示”,具体取决于地震的震级是否高于某个值。问题是滚动后,“突出显示”的行颜色将应用于其他行。我怀疑这与 ListView 中使用的缓存有关。我怎样才能防止这种情况发生?

这是我的 EarthquakeArrayAdapter 类的 getView 方法:

public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, parent, false);
}

Earthquake quake = mQuakes.get(position);
if (quake != null) {
TextView itemView = (TextView) convertView.findViewById(R.id.magnitude);
if (itemView != null) {
itemView.setText(quake.getFormattedMagnitude());
if (quake.getRoundedMagnitude() >= mMinHighlight)
itemView.setTextColor(Color.RED);
}
// Set other views in the layout, no problems here...
}
return convertView;
}

这是我的 row.xml 布局文件:

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

<TextView
android:id="@+id/magnitude"
style="@style/ListFont.Magnitude" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/date"
style="@style/ListFont.NonMagnitude"
android:gravity="center_horizontal|top"
android:textSize="18sp" />
<TextView
android:id="@+id/location"
style="@style/ListFont.NonMagnitude"
android:gravity="center_horizontal|bottom"
android:textSize="14sp" />
</LinearLayout>

</LinearLayout>

我看过this ListView 小部件上的视频,我正在考虑执行 Adam Powell 最后建议的操作 - 在 ScrollView 中动态填充和扩展 LinearLayout 和简单地使用它。我的数据目前限制在 0 到 30 个项目之间(我还没有对此进行测试,所以不知道性能差异可能是什么)。然而,这些界限可能并不总是保持不变 - 所以如果可以的话,我想解决这个问题。

最佳答案

如果通过突出显示你的意思是在幅度 TextView 上设置颜色,那么如果幅度不是所需的,你只需恢复任何更改:

if (quake.getRoundedMagnitude() >= mMinHighlight) {
itemView.setTextColor(Color.RED);
} else {
itemView.setTextColor(/*the default color*/);
}

关于android - ListFragment 中的自定义行元素在滚动后显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12024665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com