gpt4 book ai didi

android - 什么是实现 Viewholder 设计模式的最佳方式

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

这些天我更多地处理自定义 ListView 和自定义 GridView。我使用 ViewHolder 类来实现平滑滚动。但是我注意到了一些奇怪的事情。当我向下滚动列表或 gridView 时,当我实现 ViewHolder 时,项目会不断重复,这在 Android 指南中是推荐的。但是当我从中初始化 View 时,项目不会重复。

请告诉我如何改进它。这让我可以流畅地滚动,而不会重复相同的项目。

public class GridAdapter extends BaseAdapter {
Context context;
Main values;
LayoutInflater inflater;
public GridAdapter(Context context, Main main){
this.context=context;
this.values=main;
inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return values.getPosts().size();
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

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

ImageLoader imageLoader= ImageLoader.getInstance();
ViewHolder viewHolder = null;
if(convertView==null){
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.grid_item,parent,false);

convertView.setTag(viewHolder);
}
else {
viewHolder=(ViewHolder)convertView.getTag();
}
viewHolder.image=(ImageView)convertView.findViewById(R.id.thumbnail);
viewHolder.title=(TextView)convertView.findViewById(R.id.title);
viewHolder.title.setText(Html.fromHtml(values.getPosts().get(position).getTitle()));
imageLoader.displayImage(values.getPosts().get(position).getThumbnail_images().getFull().getUrl(), viewHolder.image);
return convertView;
}

private static class ViewHolder{
public static ImageView image;
public static TextView title;
}
}

这段代码给了我重复的项目,是 Android 指南中推荐的方式:

public class GridAdapter extends BaseAdapter {
Context context;
Main values;
LayoutInflater inflater;
public GridAdapter(Context context, Main main){
this.context=context;
this.values=main;
inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return values.getPosts().size();
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

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

ImageLoader imageLoader= ImageLoader.getInstance();
ViewHolder viewHolder = null;
if(convertView==null){
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.grid_item,parent,false);
viewHolder.image=(ImageView)convertView.findViewById(R.id.thumbnail);
viewHolder.title=(TextView)convertView.findViewById(R.id.title);
convertView.setTag(viewHolder);
}
else {
viewHolder=(ViewHolder)convertView.getTag();
}

viewHolder.title.setText(Html.fromHtml(values.getPosts().get(position).getTitle()));
imageLoader.displayImage(values.getPosts().get(position).getThumbnail_images().getFull().getUrl(), viewHolder.image);
return convertView;
}

private static class ViewHolder{
public static ImageView image;
public static TextView title;
}
}

最佳答案

 public static ImageView image;
public static TextView title;

错了。他们应该是

public ImageView image;
public TextView title;

没有 static 。这应该可以解决您的问题。您可以阅读有关静态关键字 here 的更多信息

关于android - 什么是实现 Viewholder 设计模式的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34941919/

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