【发布时间】:2015-11-23 12:20:34
【问题描述】:
我有一个应用程序,我需要在其中移动一堆按钮(就像将一堆卡片的一部分从一堆移动到另一堆一样)。我已经在 xml 布局中定义了所有按钮,并为所有人设置了触摸和拖动侦听器。 我可以单独在屏幕上拖放任何按钮。但在某些情况下,我需要做的是拖动堆叠在我同时单击的原始按钮顶部的其他按钮。 有没有办法“欺骗”或模拟按下另一个按钮(以便侦听器注册它)? 谢谢 ***编辑于 2015 年 9 月 8 日 @覆盖 public boolean onTouch(View v, MotionEvent e) { // tosty("mclicking: "+mClicking); int startpos = 0; switch (e.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
isWastePile=false;
get_selected_deck(v); // determines which of 7 decks or layouts in the tablau you have
// clicked
FromDeck = selecteddeck;
FromDeckCard = deckcard;
FromDeckButton = deckbutton;
// if (!mClicking) {
mClicking = true;
//String piecetag = (String) v.getTag();
// // IDEA!!!/ ///
/*
* I wrote a function that finds all the ImageButtons below where
* the user clicked, and set them all to invisible. I then created a
* new Linear Layout within the Linear Layout that the user clicked (during the ACTION_DOWN event),
* and passed that into the Drag Shadow builder during the ACTION_MOVE event.
*
* Once into the ACTION_DROP portion, I simply referenced global
* variables to figure out if the user dropped in one or multiple
* ImageButtons, and dealt with them accordingly.
*/
//if (!isWastePile) {
//draglayout.setClipChildren(false);
lltemp = new LinearLayout(this);
lltemp.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams llparams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llparams.setMargins(0, -52, 0, 0);
lltemp.setLayoutParams(llparams);
draglayout.addView(lltemp);
for (int i = 0; i < deckstack_list[selecteddeck].size(); i++) {
if (v == (draglayout.getChildAt(i))) {
startpos = i;
for (int o = i; o < deckstack_list[selecteddeck].size(); o++) {
// layout5.removeViewAt(o);
draglayout.getChildAt(o).setVisibility(View.GONE); // all
// buttons
// being
dragtempstack.push((Integer) deckstack_list[selecteddeck].get(o)) ; // dragged
// to
// invisible
// then recreate another linear layout within layout5
// and pass to dragshadow builder
// to do
// also set a GLOBAL variable with stack count (number
// of cards dragged)
lltemp.setClipChildren(false);
lltemp.addView(createtempButtons(o, startpos));
}
}
}
//} // end if wastepile check statement
//tosty("dragtempstack size: "+dragtempstack.size());
break;
case MotionEvent.ACTION_MOVE:
//tosty("Action MOVE");
Log.i("ACTION Event: ", "ACTION MOVE");
// v = layout5;
v = lltemp;
v.setVisibility(View.INVISIBLE);
v.bringToFront();
v.invalidate();
v.setVisibility(View.VISIBLE);
//DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
DeckDragShadow shadowBuilder = new DeckDragShadow(v);
v.startDrag(null, shadowBuilder, v, 0);
correctDrag = false;
break;
private Button createtempButtons(final int i, final int startpos) {
final Button b = new Button(this);
b.setOnTouchListener(this);
b.setOnDragListener(new DeckDragListener());
b.setBackgroundResource(cardimagearray[dragdeckstack.get(i)]);
float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
45, getResources().getDisplayMetrics());
float height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
61, getResources().getDisplayMetrics());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
(int) width, (int) height);
float margTop = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
-36, getResources().getDisplayMetrics());
if (i > startpos) {
params.setMargins(0, -57, 0, 0);
}
b.setLayoutParams(params);
// b.bringToFront();
// b.invalidate();
b.setVisibility(View.VISIBLE);
return b;
【问题讨论】:
-
进一步澄清,纸牌是我需要做的一个很好的例子。单击卡片堆中任意位置的卡片,然后将所有卡片拖动到单击的原始按钮的顶部。我可以通过循环更改所有其他卡片的背景、可见性等,但似乎无法拖动所有卡片。
-
我假设您已经有了要移动视图的东西,那么为什么不移动按钮,而是创建并移动包含按钮的布局呢?
-
是的,正确我有线性布局的按钮。如果我将线性布局视图传递到拖动阴影生成器中,所有按钮都会移动。但是当我尝试使用我想要移动的按钮重新创建一个临时线性布局时,例如一半的按钮,我遇到了拖动问题。这是我一直在尝试的一些代码
标签: java android drag-and-drop