【问题标题】:Android- GridView Ontouch called multiple timesAndroid-多次调用GridView Ontouch
【发布时间】:2014-05-21 19:25:10
【问题描述】:

我有这个适配器(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;
}

`

在活动中我称之为:

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 多次触发。

【问题讨论】:

标签: android gridview call ontouchlistener


【解决方案1】:

这是它的预期行为。

ACTION_DOWN 会触发一次,ACTION_MOVE 会触发多次,完成后会触发 ACTION_UP。

【讨论】:

  • thx @nyx,现在我有一个“关于位置”的新问题,当我第一次触摸时我得到了 -1 位置,但下次我得到了正确的位置。你有什么想法吗?
  • 嗯,它似乎返回了 INVALID_POSITION,只有当点不与项目相交时才会发生这种情况。您肯定是在点击列表项,对吧?第一次触摸它也一直返回-1?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
相关资源
最近更新 更多