在我的适配器中,我有三种不同的视图,使用黄色,橙色和红色作为不同的文本颜色。为了配合这些,我试图将三个不同的图像作为缩略图(hhy,hho,hhr)。当它运行时,图像与它们对应的视图项不匹配,我在列表视图中得到hho和hhy任何一个项目过早或过晚。我的imageview布局名称正确,所以我不确定它们为什么显示不正确。
PostItemAdapter.java
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
int listViewItemType = getItemViewType(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.postitem, null);
viewHolder = new ViewHolder();
//viewHolder.itemThumbView = (ImageView) convertView.findViewById(R.id.itemThumb);
if (listViewItemType == TYPE_short) {
viewHolder.itemTitleView = (TextView) convertView.findViewById(R.id.itemTitleLabel);
viewHolder.itemThumbView = (ImageView) convertView.findViewById(R.id.itemThumb);
} else if (listViewItemType == TYPE_med) {
viewHolder.itemTitleView = (TextView) convertView.findViewById(R.id.itemTitleLabel1);
viewHolder.itemThumbView = (ImageView) convertView.findViewById(R.id.itemThumb1);
} else {
viewHolder.itemTitleView = (TextView) convertView.findViewById(R.id.itemTitleLabel2);
viewHolder.itemThumbView = (ImageView) convertView.findViewById(R.id.itemThumb2);
}
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (listViewItemType == TYPE_short) {
viewHolder.itemTitleView.setText(datas.get(position).itemTitle);
viewHolder.itemThumbView.setImageResource(R.drawable.hhy);
} else if (listViewItemType == TYPE_med) {
viewHolder.itemTitleView.setText(datas.get(position).itemTitle);
viewHolder.itemThumbView.setImageResource(R.drawable.hho);
} else {
viewHolder.itemTitleView.setText(datas.get(position).itemTitle);
viewHolder.itemThumbView.setImageResource(R.drawable.hhr);
}
return convertView;
}
}
和相关的布局postitem.xml
<ImageView
android:id="@+id/itemThumb"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/hhy"
android:scaleType="centerCrop"
android:layout_marginRight="5dp"/>
<ImageView
android:id="@+id/itemThumb1"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/hho"
android:scaleType="centerCrop"
android:layout_marginRight="5dp"/>
<ImageView
android:id="@+id/itemThumb2"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/hhr"
android:scaleType="centerCrop"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/itemTitleLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/itemThumb"
android:layout_toRightOf="@id/itemThumb"
android:maxLines="2"
android:textIsSelectable="false"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#ffff00"
android:ellipsize="end"
/>
<TextView
android:id="@+id/itemTitleLabel1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/itemThumb"
android:layout_toRightOf="@id/itemThumb"
android:maxLines="2"
android:textIsSelectable="false"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#ffa500"
android:ellipsize="end"
/>
<TextView
android:id="@+id/itemTitleLabel2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/itemThumb"
android:layout_toRightOf="@id/itemThumb"
android:maxLines="2"
android:textIsSelectable="false"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#ff0000"
android:ellipsize="end"
/>
答案 0 :(得分:4)
您的问题中出现了一些问题。回收视图仅用于充气/创建足够的次数,以便第一次完全填充ListView的可见部分。然后在滚动时将它们回收,而不是再次创建。
这意味着此块仅在前几次被调用:
if (convertView == null) {
convertView = inflater.inflate(R.layout.postitem, null);
viewHolder = new ViewHolder();
//viewHolder.itemThumbView = (ImageView) convertView.findViewById(R.id.itemThumb);
if (listViewItemType == TYPE_short) {
viewHolder.itemTitleView = (TextView) convertView.findViewById(R.id.itemTitleLabel);
viewHolder.itemThumbView = (ImageView) convertView.findViewById(R.id.itemThumb);
} else if (listViewItemType == TYPE_med) {
viewHolder.itemTitleView = (TextView) convertView.findViewById(R.id.itemTitleLabel1);
viewHolder.itemThumbView = (ImageView) convertView.findViewById(R.id.itemThumb1);
} else {
viewHolder.itemTitleView = (TextView) convertView.findViewById(R.id.itemTitleLabel2);
viewHolder.itemThumbView = (ImageView) convertView.findViewById(R.id.itemThumb2);
}
convertView.setTag(viewHolder);
}
对于后续回收,您的ViewHolder可能指向非预期的资源。如果当前项是TYPE_med,则可以使用不同的资源引用(即创建时的TYPE_short)创建viewHolder.itemThumbView。修复此问题仍然无法解决您的问题,因为您还有其他两个ImageViews / TextViews。
我没有看到任何明显的需要拥有多个ImageViews / TextViews,而不是在所有更接受的方法上设置可见性:
static class ViewHolder
{
private TextView mTextViewTitle;
private ImageView mImageViewThumb;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
Item i = datas.get(position);
int listViewItemType = i.viewtype;
if (convertView == null) {
convertView = inflater.inflate(R.layout.postitem, null);
viewHolder = new ViewHolder();
viewHolder.mTextViewTitle = (TextView) convertView.findViewById(R.id.itemTitleLabel);
viewHolder.mImageViewThumb = (ImageView) convertView.findViewById(R.id.itemThumb);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.mTextViewTitle.setText(i.itemTitle);
if (listViewItemType == TYPE_short) {
viewHolder.mTextViewTitle.setTextColor(Color.parseColor("#ffff00"));
viewHolder.mImageViewThumb.setImageResource(R.drawable.hhy);
} else if (listViewItemType == TYPE_med) {
viewHolder.mTextViewTitle.setTextColor(Color.parseColor("#ffff00"));
viewHolder.mImageViewThumb.setImageResource(R.drawable.hho);
} else {
viewHolder.mTextViewTitle.setTextColor(Color.parseColor("#ff0000"));
viewHolder.mImageViewThumb.setImageResource(R.drawable.hhr);
}
return convertView;
}
最好在资源中定义颜色,但我会将其保存为另一个讨论。
您的新XML:
<ImageView
android:id="@+id/itemThumb"
android:layout_width="48dp"
android:layout_height="48dp"
android:scaleType="centerCrop"
android:layout_marginRight="5dp" />
<TextView
android:id="@+id/itemTitleLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/itemThumb"
android:layout_toRightOf="@id/itemThumb"
android:maxLines="2"
android:textIsSelectable="false"
android:textSize="16sp"
android:textStyle="bold"
android:ellipsize="end" />
答案 1 :(得分:0)
好的,我的问题是,当listview视图被回收时,它们会覆盖或无法覆盖预先存在的图像/文本。我相当讨厌的解决方案是使用以下代码。如果有人知道你应该如何解决这个问题,我会有兴趣听到它:P
PostItemAdapter.java(ViewHolder)
static class ViewHolder
{
private TextView itemTitleViewY, itemTitleViewO, itemTitleViewR;
private ImageView itemThumbViewY, itemThumbViewO, itemThumbViewR;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
Item i = datas.get(position);
int listViewItemType = i.viewtype;
if (convertView == null) {
convertView = inflater.inflate(R.layout.postitem, null);
viewHolder = new ViewHolder();
viewHolder.itemTitleViewY = (TextView) convertView.findViewById(R.id.itemTitleLabel);
viewHolder.itemThumbViewY = (ImageView) convertView.findViewById(R.id.itemThumb);
viewHolder.itemTitleViewO = (TextView) convertView.findViewById(R.id.itemTitleLabel1);
viewHolder.itemThumbViewO = (ImageView) convertView.findViewById(R.id.itemThumb1);
viewHolder.itemTitleViewR = (TextView) convertView.findViewById(R.id.itemTitleLabel2);
viewHolder.itemThumbViewR = (ImageView) convertView.findViewById(R.id.itemThumb2);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
switch (listViewItemType) {
case 0:
viewHolder.itemTitleViewY.setText(i.itemTitle);
viewHolder.itemTitleViewO.setText("");
viewHolder.itemTitleViewR.setText("");
viewHolder.itemThumbViewY.setVisibility(View.VISIBLE);
viewHolder.itemThumbViewO.setVisibility(View.INVISIBLE);
viewHolder.itemThumbViewR.setVisibility(View.INVISIBLE);
Log.d("DEBUG CHECKME", "getItemViewType Y: " + i.itemTitle + ", " + i.viewtype + ", " + i);
break;
case 1:
viewHolder.itemTitleViewO.setText(i.itemTitle);
viewHolder.itemTitleViewY.setText("");
viewHolder.itemTitleViewR.setText("");
viewHolder.itemThumbViewO.setVisibility(View.VISIBLE);
viewHolder.itemThumbViewY.setVisibility(View.INVISIBLE);
viewHolder.itemThumbViewR.setVisibility(View.INVISIBLE);
Log.d("DEBUG CHECKME", "getItemViewType O: " + i.itemTitle + ", " + i.viewtype + ", " + i);
break;
case 2:
viewHolder.itemTitleViewR.setText(i.itemTitle);
viewHolder.itemTitleViewO.setText("");
viewHolder.itemTitleViewY.setText("");
viewHolder.itemThumbViewR.setVisibility(View.VISIBLE);
viewHolder.itemThumbViewY.setVisibility(View.INVISIBLE);
viewHolder.itemThumbViewO.setVisibility(View.INVISIBLE);
Log.d("DEBUG CHECKME", "getItemViewType R: " + i.itemTitle + ", " + i.viewtype + ", " + i);
break;
default:
viewHolder.itemTitleViewY.setText("");
viewHolder.itemTitleViewO.setText("");
viewHolder.itemTitleViewR.setText("");
viewHolder.itemThumbViewY.setVisibility(View.INVISIBLE);
viewHolder.itemThumbViewO.setVisibility(View.INVISIBLE);
viewHolder.itemThumbViewR.setVisibility(View.INVISIBLE);
Log.d("DEBUG CHECKME", "getItemViewType D: " + i.itemTitle + ", " + i.viewtype + ", " + i);
break;
}
return convertView;
}