如果你看下面的代码,当我控制 v 是否为空时,不重新膨胀视图,listview 有问题。
当我滚动列表视图时,行值会发生变化,重复。我可以通过删除 ** " if (null == v) { " **. 但是此时,当我滚动列表视图时,它正在闪烁。
if (null == v) {
v = inflater.inflate(layoutId, parent, false);
// view invoke edilir
invokeView(v);
v.setTag(viewHolder);
} else {
viewHolder = (T) v.getTag();
}
感谢您的帮助
以下所有代码
适应性强
public interface Adaptable {
public View buildView(View v, LayoutInflater inflater, ViewGroup parent,Context context, int position);
public Object returnEntity();
}
基础视图
public abstract class BaseView<T, E> implements Adaptable,IMapView {
private static final String TAG = "BaseView";
protected int layoutId;
protected T viewHolder;
protected E entity;
public BaseView() {
}
public BaseView(E entity, int layoutId) {
this.entity = entity;
this.layoutId = layoutId;
}
protected void invokeView(View v) {
try {
Field fs[] = viewHolder.getClass().getFields();
for (Field f : fs) {
InvokeView a = (InvokeView) f.getAnnotation(InvokeView.class);
int id = a.viewId();
f.set(viewHolder, v.findViewById(id));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
@SuppressWarnings("unchecked")
@Override
public View buildView(View v, LayoutInflater inflater, ViewGroup parent,Context context,int position) {
// view yüklenir
if (null == v) {
v = inflater.inflate(layoutId, parent, false);
// view invoke edilir
invokeView(v);
v.setTag(viewHolder);
} else {
viewHolder = (T) v.getTag();
}
// date view bağlanır
mappingData(viewHolder, entity,context,position);
return v;
}
public Object returnEntity()
{
return entity;
}
protected abstract void mappingData(T viewHolder, E entity,Context context,int background);
}
调用视图
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface InvokeView {
int viewId();
}
通用适配器
public class GenericAdapter extends BaseAdapter implements Filterable {
private String TAG = "GenericAdapter";
private LayoutInflater inflater;
private List<Adaptable> items;
private List<Adaptable> cachedItems;
private Context context;
@SuppressWarnings("unchecked")
public GenericAdapter(List<?> items, Context c) {
this.items = (List<Adaptable>) items;
cachedItems = new ArrayList<Adaptable>();
for (Object object : items) {
this.cachedItems.add(((Adaptable)object));
}
inflater = LayoutInflater.from(c);
context = c;
}
@Override
public int getCount() {
if (items == null)
return 0;
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return items.get(position).buildView(convertView, inflater, parent,context, position);
}
}