为了让listview更流畅,我决定用一个数组来保存getView()每次生成的view,以便重复使用,以避免重复getView(),所以他们继承BaseAdapter来写MyBaseAdapter。最终效果很好,运行很流畅,但是又出现了新的问题:listview第一项和第二项里面的按钮,有时候中间的没有响应点击事件,调试如下日志:
I/InputReader (304): dispatchTouch : touch events action is 1, pending (waiting finished signal) = 0
I/InputDispatcher (304): Delivering touch to current input target: action: 1, channel 42068da0 com.meet.baby / com.meet.baby.test (server)
I/InputDispatcher (304): Delivering touch to current input target: action: 1, channel TouchIntercepter (server)
但是按钮的点击事件却能够补充条目被点击后执行。
package com.meet.baby.adapter;
import java.util.ArrayList;
import com.meet.baby.MyApp;
import com.meet.baby.R;
import com.meet.baby.modle.ui.BBLinearLayout;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ListView;
/**
*
* @author zhangcy
*
* @param <T> datamodel
* @param <V> viewmodel
*/
public abstract class MyBaseAdapter<T, V extends View> extends BaseAdapter{
protected static final String TAG = "MyBaseAdapter";
protected ArrayList<T> lisTs;
protected V[] views;
public MyBaseAdapter (ArrayList<T> dataViews) {
lisTs = dataViews;
if (lisTs == null) {
dataViews = new ArrayList<T>();
}
buildList();
}
@SuppressWarnings("unchecked")
private void buildList() {
// TODO Auto-generated method stub
int i = 0;
if (lisTs != null && lisTs.size() > 0) {
i = lisTs.size();
}
views = (V[]) new View[i];
}
@Override
public void notifyDataSetChanged() {
// TODO Auto-generated method stub
buildList();
super.notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (lisTs == null || lisTs.size() == 0) {
BBLinearLayout layout = new BBLinearLayout(MyApp.getInstance().getContext());
layout.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, 800));
layout.setGravity(Gravity.CENTER_HORIZONTAL);
try {
layout.addView(getEmptyView());
} catch (Exception e) {
// TODO: handle exception
}
layout.setBackgroundResource(R.drawable.bg_drawable);
layout.setEnabled(false);
return layout;
}
V view = views[position];
if ( view!= null) {
view.setSelected(true);
return view;
}else {
Context context = MyApp.getInstance().getContext();
T data = lisTs.get(position);
view = constructView(context, position, data);
views[position] = view;
}
view.setSelected(true);
return view;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
int i = 1;
if (lisTs != null && lisTs.size() > 0) {
i = lisTs.size();
}
return i;
}
public void notifyDataSetChanged(ArrayList<T> lists) {
// TODO Auto-generated method stub
this.lisTs = lists;
notifyDataSetChanged();
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
if (observer != null) {
super.unregisterDataSetObserver(observer);
}
}
/**
* retun View for each item
* @param context
* @param position
* @param data data for each item
* @return V View for each item
*/
public abstract V constructView(Context context, int position, T data);
public abstract View getEmptyView();
}