Search code examples
androidgridviewcallontouchlistener

Android- GridView Ontouch called multiple times


i have this code for the adapter (baseAdapter)`public class ImageAdapter extends BaseAdapter implements 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;
}

`

and in activity i have call this:

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);

but i see the OnTouchListener fires multiple times when i touch any textView.


Solution

  • That is its expected behaviour.

    It'll fire once for ACTION_DOWN, and multiple times for ACTION_MOVE, and then for ACTION_UP when you are finished.