在你亲切地阅读之前,我已经看过已经问过的问题了。很多问题都与它们不匹配。
这些天我更多地处理自定义ListView
和自定义GridView
。我使用ViewHolder
类进行平滑滚动。但是我注意到了一些奇怪的东西。当我向下滚动列表时,项目会不断重复,或者当我实施gridView
时,ViewHolder
会继续重复,因为在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);
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;
}
}
答案 0 :(得分:2)
public static ImageView image;
public static TextView title;
错了。他们应该是
public ImageView image;
public TextView title;
没有static
。这应该可以解决您的问题。您可以阅读有关静态关键字here