将OnLongClickListener应用于具有相同名称的多个图像

时间:2016-05-30 18:13:10

标签: java android imageview draggable onclicklistener

所以我有一些代码可以监听ImageView的拖动但是它只监听最后一个,我希望它能听到所有这些代码。问题是ImageViews是动态制作的,它们都具有相同的名称,因此它只适用于最后一个。

以下是ImageView创建代码:

public ImageView getCardView(String Card) {

        imageView = new ImageView(this);

        switch (Card) {
            case "ace-clubs":
                imageView.setImageResource(R.drawable.black_joker);
                imageView.setId(Integer.valueOf(CardId));
                break;
            case "ace-diamonds":
                imageView.setImageResource(R.drawable.ace_of_diamonds);
                imageView.setId(Integer.valueOf(CardId));
                break;

            case "ace-hearts":
                imageView.setImageResource(R.drawable.ace_of_hearts);
                imageView.setId(Integer.valueOf(CardId));
                break;
        }

        CardId++;

        final float scale = getResources().getDisplayMetrics().density;
        int padding_5dp = (int) (5 * scale + 0.5f);
        int Height = (int) (100 * scale + 0.5f);
        int Width = (int) (60 * scale + 0.5f);


        imageView.setBackgroundColor(Color.WHITE);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Width, Height);
        imageView.setLayoutParams(params);
        imageView.setPadding(2, 0, 2, 0);
        params.rightMargin = 8;
        params.leftMargin = 8;

        return imageView;

    }

以下是仅适用于名称为

的最后一个ImageView的图像侦听
imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
                String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};

                ClipData dragData = new ClipData(v.getTag().toString(),mimeTypes, item);
                View.DragShadowBuilder myShadow = new View.DragShadowBuilder(imageView);

                v.startDrag(dragData,myShadow,null,0);
                return true;
            }
        });

        imageView.setOnDragListener(new View.OnDragListener() {
            @Override
            public boolean onDrag(View v, DragEvent event) {
                switch(event.getAction())
                {
                    case DragEvent.ACTION_DRAG_STARTED:
                        layoutParams = (RelativeLayout.LayoutParams)v.getLayoutParams();
                        Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");

                        // Do nothing
                        break;

                    case DragEvent.ACTION_DRAG_ENTERED:
                        Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED");
                        int x_cord = (int) event.getX();
                        int y_cord = (int) event.getY();
                        break;

                    case DragEvent.ACTION_DRAG_EXITED :
                        Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
                        x_cord = (int) event.getX();
                        y_cord = (int) event.getY();
                        layoutParams.leftMargin = x_cord;
                        layoutParams.topMargin = y_cord;
                        v.setLayoutParams(layoutParams);
                        break;

                    case DragEvent.ACTION_DRAG_LOCATION  :
                        Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION");
                        x_cord = (int) event.getX();
                        y_cord = (int) event.getY();
                        break;

                    case DragEvent.ACTION_DRAG_ENDED   :
                        Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED");

                        // Do nothing
                        break;

                    case DragEvent.ACTION_DROP:
                        Log.d(msg, "ACTION_DROP event");

                        // Do nothing
                        break;
                    default: break;
                }
                return true;
            }
        });

        imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    ClipData data = ClipData.newPlainText("", "");
                    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(chosenCards);

                    imageView.startDrag(data, shadowBuilder, imageView, 0);
                    imageView.setVisibility(View.INVISIBLE);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        });

谢谢:)

1 个答案:

答案 0 :(得分:1)

我百分百肯定了解您的问题,但您应该跟踪最后一个ImageView的CardId。