簡體   English   中英

Android- GridView Ontouch多次調用

[英]Android- GridView Ontouch called multiple times

我有以下代碼用於適配器(baseAdapter)的公共類ImageAdapter擴展了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會觸發多次。

這是其預期的行為。

它將針對ACTION_DOWN觸發一次,並針對ACTION_MOVE觸發多次,然后在完成后觸發ACTION_UP。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM