如果你看下面的代码,当我控制v是否为空时,没有重新填充视图,列表视图有问题。
当我滚动列表视图时,行值会发生变化,重复。我可以通过删除**“if(null == v){”**来解决这个问题。但是在这个时候,当我滚动listview时,它正在闪烁。
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);
}
InvokeView
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface InvokeView {
int viewId();
}
GenericAdapter
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);
}
}
答案 0 :(得分:0)
看起来您正在为每一行重复使用相同的viewHolder对象。此外,如果您对视图注入感兴趣,还应该查看ButterKnife。
这是BaseView
应该是这样的:
public View buildView(View v, LayoutInflater inflater, ViewGroup parent,Context context,int position) {
ViewHolder viewHolder;
if (v == null) {
v = inflater.inflate(layoutId, parent, false);
viewHolder = new ViewHolder();
invokeView(viewHolder, v);
v.setTag(viewHolder);
} else {
viewHolder = v.getTag();
}
//...
}
protected void invokeView(ViewHolder viewHolder, 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();
}
}