Android ListView项与填充重复使用
我有一个Android ListView项与填充重复使用,android,listview,Android,Listview,我有一个列表视图,其中包含具有不同状态的项。在其中一种状态下,项目具有带有填充的自定义背景的TextView: <?xml version="1.0" encoding="UTF-8" ?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="@color/..." /> <padding
列表视图
,其中包含具有不同状态的项。在其中一种状态下,项目具有带有填充的自定义背景的TextView
:
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid
android:color="@color/..." />
<padding
android:left="8dp"
android:right="8dp" />
<corners
android:radius="10dp" />
</shape>
当我滚动此列表时,一些没有背景的项目在重复使用后有填充
我找到的唯一解决方案是重置适配器中的填充和背景:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View result = convertView != null
? convertView
: getLayoutInflater().inflate(R.layout.item, parent, false);
final View statusView = result.findViewById(R.id.status);
statusView.setPadding(0, 0, 0, 0);
if (position % 4 == 0) {
statusView.setBackground(null);
statusView.setBackgroundResource(R.drawable.target);
} else {
statusView.setBackground(null);
}
return result;
}
但它看起来像一个黑客。
有人遇到过同样的问题吗?或者可能知道如何在没有黑客攻击的情况下修复它?根据,这似乎不是黑客攻击,您需要显式重置填充 将背景设置为给定的可绘制背景,或删除背景。如果 背景有填充,此视图的填充设置为 背景的填充。但是,当删除背景时 视图的填充未被触摸。如果需要设置填充, 请使用setPadding(int,int,int,int)
如果我正确理解代码,即使在没有填充的情况下设置新背景也不会更改当前视图的填充如果使用两种或更多类型的行,则必须复制所有自定义元素,并为每一行设置它。因此,基本上每行需要2个字符,可绘图、文本、
PADDING
等。。或者适配器会回收一些项目,有时不会
例如,在这里我更改每行上的图标
if (sqLite.imageReport(list.get(arg0).getId(), id_shop) == null) {
holder.imageView.setImageDrawable(ContextCompat.getDrawable(context,
R.drawable.ic_add_circle_green_36dp));
} else {
holder.imageView.setImageDrawable(ContextCompat.getDrawable(context,
R.drawable.ic_check_circle_blue_36dp));
}
if (sqLite.imageReport(list.get(arg0).getId(), id_shop) == null) {
holder.imageView.setImageDrawable(ContextCompat.getDrawable(context,
R.drawable.ic_add_circle_green_36dp));
} else {
holder.imageView.setImageDrawable(ContextCompat.getDrawable(context,
R.drawable.ic_check_circle_blue_36dp));
}