将 OnLongClickListener 应用于具有相同名称的多个图像
Posted
技术标签:
【中文标题】将 OnLongClickListener 应用于具有相同名称的多个图像【英文标题】:Apply OnLongClickListener to multiple Images with Same name 【发布时间】:2016-09-28 15:08:58 【问题描述】:所以我有一些代码可以监听 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】:我 100% 肯定理解您的问题,但您应该跟踪最后一个 ImageView 的 CardId。
【讨论】:
我不明白...请解释一下 @YudiMoszkowski 我认为解决您的问题的最佳方法是从上一个视图中删除 OnLongClickListener 对象并将其添加到新的 imageView 中。private OnLongClickListener listener; ... lastImageView.setOnLongClickListener(null); newImageView.setOnLongClickListener(listener) // which will become the lastImageView
以上是关于将 OnLongClickListener 应用于具有相同名称的多个图像的主要内容,如果未能解决你的问题,请参考以下文章
第24章OnLongClickListener长按事件(从零开始学Android)
我可以在运行时在 onClickListener 和 onLongClickListener 之间切换吗?
在 listview Android 1.6 中同时使用 onClickListener 和 onLongClickListener