Android- GridView Ontouch多次调用

时间:2014-05-21 19:25:10

标签: android gridview call ontouchlistener

我有适配器的这个代码(baseAdapter)`public class ImageAdapter extends BaseAdapter实现ListAdapter {

private Context mContext;
private LinkedList<Category> mThumbIds;

public ImageAdapter(Context c) {
    mContext = c;
}

public ImageAdapter(Context c, LinkedList<Category> categories) {
    mContext = c;
    mThumbIds = categories;

}

public int getCount() {
    return mThumbIds.size();
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    TextView category;
    if (convertView == null) { // if it's not recycled, initialize some
                                //TextViewtes
        category = new TextView(mContext);
        category.setLayoutParams(new GridView.LayoutParams(100,100));
        Drawable dr;
        try {
            dr = mContext.getResources().getDrawable(
                    mThumbIds.get(position).getIconName());
        } catch (NotFoundException e) {
            dr = mContext.getResources().getDrawable(R.drawable.home);
        }
        Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
        // Scale it to 50 x 50
        Drawable d = new BitmapDrawable(mContext.getResources(),
                Bitmap.createScaledBitmap(bitmap, 50, 50, true));
        category.setText(mThumbIds.get(position).getName());
        Random r = new Random();
        int c = r.nextInt(6);
        if (c % 2 == 0)
            category.setBackgroundColor(Utils.darkenColor(mContext
                    .getResources().getColor(R.color.expColor)));
        else
            category.setBackgroundColor(mContext.getResources().getColor(
                    R.color.expColor));
        category.setGravity(Gravity.CENTER);
        category.setCompoundDrawablesWithIntrinsicBounds(null, d, null,
                null);
        category.setRotationY(180.f);
        category.setFocusable(false);
        category.setFocusableInTouchMode(false);
        category.setClickable(false);

    } else {
        category = (TextView) convertView;
    }
    return category;
}

`

在活动中我称之为:

gridview = (GridView) findViewById(R.id.iconsgridview);
    gridview.setPadding(10, 10, 10, 10);
    gridview.setAdapter(new ImageAdapter(context, categories));
    gridview.setRotationY(180.0f);
    gridview.setFocusableInTouchMode(true);
    gridview.setFocusable(true);
    gridview.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int position, long id) {
            parent.showContextMenuForChild(view);

            Toast.makeText(context, "long" + position, Toast.LENGTH_SHORT)
                    .show();

            return false;
        }

    });
    gridview.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent me) {

            int action = me.getActionMasked();  // MotionEvent types such as ACTION_UP, ACTION_DOWN
            float currentXPosition = me.getX();
            float currentYPosition = me.getY();
            int position = gridview.pointToPosition((int) currentXPosition, (int) currentYPosition);

            // Access text in the cell, or the object itself
            TextView tv = (TextView) gridview.getChildAt(position);
            Toast.makeText(context, "touch" + position + counter ++, Toast.LENGTH_SHORT)
                    .show();

            return true;
    }
});
    registerForContextMenu(gridview);

但是当我触摸任何textView时,我看到OnTouchListener会多次触发。

1 个答案:

答案 0 :(得分:4)

这是它的预期行为。

它会为ACTION_DOWN触发一次,对ACTION_MOVE触发多次,然后在完成时触发ACTION_UP。