我尝试创建一个扩展SimpleAdapter并覆盖getView()方法的类,以防止我的列表视图中的颜色更改问题(当第一个项目回收到底部时,颜色也会再次出现)。但是,我这样写:
public class Adapter扩展SimpleAdapter {
public Adapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
class ViewHolder {
TextView txt_textview = null;
TextView txt_distance = null;
TextView txt_starimage = null;
ImageView txt_imageview = null;
}
HashMap<Integer, View> lmap = new HashMap<Integer, View>();
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = super.getView(position, convertView, parent);
ViewHolder holder = null;
if (lmap.get(position) == null) {
view =LayoutInflater.from(getApplicationContext()).inflate(
R.layout.activity_bank_listview_item,null);
holder = new ViewHolder();
holder.txt_starimage = (TextView) view
.findViewById(R.id.bank_listview_starimage);
holder.txt_imageview = (ImageView) view
.findViewById(R.id.bank_listview_imageview);
holder.txt_distance = (TextView) view
.findViewById(R.id.bank_listview_distance);
holder.txt_textview = (TextView) view
.findViewById(R.id.bank_listview_textview);
lmap.put(position, view);
view.setTag(holder);
} else {
view = lmap.get(position);
holder = (ViewHolder) view.getTag();
}
return view;
}
}
但listview只显示第一页listview项,以下页面为空(仅包含空布局)。我怎么解决这个问题?谢谢!
答案 0 :(得分:0)
问题在于这一行:
if (lmap.get(position) == null) {
您应该检查convertView是否为null。如果它为null,则需要为新视图充气。如果不是,您应该重复使用它。
e.g。
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder holder;
if (convertView == null){
convertView = inflater.inflate(R.layout.row, parent, false);
holder = new ViewHolder();
init(convertView, holder); // findViewById stuff
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
setView(position, holder, convertView); // fill in the textviews, set background color etc.
return convertView;
}