gpt4 book ai didi

java - Gridview重复图像但不重复android中的文本

转载 作者:太空宇宙 更新时间:2023-11-04 13:33:46 24 4
gpt4 key购买 nike

ImageView 中,我的图像被打乱,但文本没有打乱。这是我的 getView() 方法:

public View getView(int position, View convertView, ViewGroup parent) {
View v;

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mainActivity.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
v = inflater.inflate(R.layout.item_people, parent, false);
} else {
v = (View) convertView;
}

TextView textView = (TextView) v.findViewById(R.id.name);
ImageView imageView = (ImageView) v.findViewById(R.id.image);

if (list.get(position).getClass() == SearchData.Video.class) {
SearchData.Video video = (SearchData.Video) list.get(position);
textView.setText(video.getVideoName());

if (video.getCoverPicture().length > 0)
imageView.setBackground(mainActivity.Base64toImage(video.getCoverPicture()[0].getImg()));
} else if (list.get(position).getClass() == SearchData.Actor.class) {
SearchData.Actor actor = (SearchData.Actor) list.get(position);
textView.setText(actor.getFirstName());

if (actor.getPicture().length > 0)
imageView.setBackground(mainActivity.Base64toImage(actor.getPicture()[0].getImg()));
}
return v;
}

我正在设置 Actor 的图像和文本。当我向下滚动然后向上滚动时,图像已打乱,但文本未打乱。为什么?

最佳答案

您是否尝试过与 ViewHolder 相同的操作?

通常,当调用 getView() 方法时,gridview/listview 会自动通知更改,因此这就是 gridview 中图像洗牌/更改的原因。

因此,尝试使用 ViewHolder 模式实现 Gridview 并避免那些改组/重新排序

这里是 View 持有者模式实现的示例代码。

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolderItem viewHolder;


// The convertView argument is essentially a "ScrapView" as described is Lucas post
// http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
// It will have a non-null value when ListView is asking you recycle the row layout.
// So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.

if(convertView==null){

// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);

// well set up the ViewHolder
viewHolder = new ViewHolderItem();
viewHolder.textViewItem = (TextView) convertView.findViewById(R.id.textViewItem);

// store the holder with the view.
convertView.setTag(viewHolder);

}else{
// we've just avoided calling findViewById() on resource everytime
// just use the viewHolder
viewHolder = (ViewHolderItem) convertView.getTag();
}

// object item based on the position
ObjectItem objectItem = data[position];

// assign values if the object is not null
if(objectItem != null) {
// get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values
viewHolder.textViewItem.setText(objectItem.itemName);
viewHolder.textViewItem.setTag(objectItem.itemId);
}

return convertView;

}

你的ViewHodler应该是这样的

// ViewHolder.
// caches our TextView
static class ViewHolderItem {
TextView textViewItem;
}

这与您的问题不完全相同,但您可以根据自己的方式编辑上述逻辑。

希望有帮助:)

关于java - Gridview重复图像但不重复android中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31912096/

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