1

To make the listview smoother, I decided to use an array to save view generated by getView () each time to reuse, in order to avoid duplicate getView(), so they inherit the BaseAdapter to write MyBaseAdapter. Ultimately, the effect is very good, very smooth running, buthas given rise to new problems: the button inside of the first and second item of the listview, sometimes the middle one does not respond to the click event, debug the following log:

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)

But the button click event was able to complement the entry is clicked after implementation.

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();
}
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
ssgxx
  • 79
  • 7
  • those logcat errors are purely informational and don't tell us much about the problem – Alex Lockwood Jun 28 '12 at 03:11
  • I have initially set the log indicate that the click event response, but the response of the state is waiting for, indicating that the click is not a complete process, so do not continue to perform. – ssgxx Jun 28 '12 at 03:20

0 Answers0