0

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.

Moh'd Sabri
  • 43
  • 1
  • 10
  • 1
    On touch is not like on click. It is called for all motion events on the view. – Larry McKenzie May 21 '14 at 19:29
  • 1
    http://stackoverflow.com/questions/9586032/android-difference-between-onintercepttouchevent-and-dispatchtouchevent/22490810#22490810 – seb May 21 '14 at 19:43

1 Answers1

4

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.

Nyx
  • 2,233
  • 1
  • 12
  • 25
  • thx @nyx, now I have a new problem "about position" , is when i touch on at first time i got -1 position , but in next time i got the right position. do you have any idea? – Moh'd Sabri May 21 '14 at 19:46
  • 1
    Hmmm it appears that it's returning INVALID_POSITION which only should happen if the point does not intersect an item. You're definitely tapping on a list item right? Also it's consistently returning -1 for the first touch? – Nyx May 21 '14 at 19:55