java Android ListView单元格在滚动ListView时添加了更多的ImageView
我有一个布局,它定义了单元格在ListView中的外观,但当我滚动时,单元格中唯一在滚动时发生变化的部分是位于LinearLayout视图中的ImageView
应该发生的是,在这个线性布局中应该只显示5颗星,但当我滚动时,我会在我的线性布局中看到大量的这些星星图像,而此时应该只显示5颗星
这是我的自定义阵列适配器:
public class ThreadArrayAdapter extends ArrayAdapter<ThreadObj> {
Context context;
int layoutResourceId;
public ArrayList<ThreadObj> data = null;
public ThreadArrayAdapter(Context context, int layoutResourceId, ArrayList<ThreadObj> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
//WeatherHolder holder = null;
ThreadHolder t = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
//TextView tv = (TextView)row.findViewById(R.id.threadTitle);
//t = new ThreadObj(tv.getText().toString());
t = new ThreadHolder();
//holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
t.threadUsernameTV = (TextView)row.findViewById(R.id.threadUsername);
t.threadDateTV = (TextView)row.findViewById(R.id.threadDate);
t.threadRatingIV = (ImageView)row.findViewById(R.id.threadRating);
t.threadTitleTV = (TextView)row.findViewById(R.id.threadTitle);
t.threadCategoryTV = (TextView)row.findViewById(R.id.threadCategory );
t.threadSubCategoryTV = (TextView)row.findViewById(R.id.threadSubCategory);
t.threadCountryTV = (TextView)row.findViewById(R.id.threadCountry);
t.threadStarLL = (LinearLayout)row.findViewById(R.id.threadStarContainer);
t.threadFlagIV = (ImageView)row.findViewById(R.id.threadFlag);
row.setTag(t);
}
else
{
t = (ThreadHolder)row.getTag();
}
ThreadObj thread = data.get(position);
t.threadUsernameTV.setText(thread.firstname + " " + thread.lastname);
t.threadDateTV.setText(thread.date);
t.threadTitleTV.setText(thread.title);
t.threadCategoryTV.setText(thread.category);
t.threadSubCategoryTV.setText(thread.subCategory);
t.threadCountryTV.setText(thread.country);
if(thread.ageRating == 4){
t.threadRatingIV.setImageResource(R.drawable.agerating17);
}
else if(thread.ageRating == 3){
t.threadRatingIV.setImageResource(R.drawable.agerating12);
}
else if(thread.ageRating == 2){
t.threadRatingIV.setImageResource(R.drawable.agerating7);
}
else{
t.threadRatingIV.setImageResource(R.drawable.ageratingpg);
}
//holder.imgIcon.setImageResource(weather.icon);
for(int i = 0; i < 5; i++){
ImageView star1 = new ImageView(this.context);
star1.setImageResource(R.drawable.star1);
ImageView star2 = new ImageView(this.context);
star2.setImageResource(R.drawable.star2);
if(i < thread.rating){
t.threadStarLL.addView(star2);
}else{
t.threadStarLL.addView(star1);
}
}
if(thread.flags > 0){
t.threadFlagIV.setImageResource(R.drawable.flag2);
}else{
t.threadFlagIV.setImageResource(R.drawable.flag1);
}
return row;
}
static class ThreadHolder
{
//ImageView imgIcon;
TextView threadUsernameTV;
TextView threadDateTV;
ImageView threadRatingIV;
TextView threadTitleTV;
TextView threadCategoryTV;
TextView threadSubCategoryTV;
TextView threadCountryTV;
LinearLayout threadStarLL;
ImageView threadFlagIV;
}
}
# 1 楼答案
您总是创建新的
ImageView
,并将它们添加到t.threadStarLL
布局中,即使在重新使用现有视图时也是如此。解决这个问题的一种方法是在添加新星号之前清除threadStarLL
布局尝试在创建星星的
for
循环之前添加这一行: