java 多个按钮_java - 在Java中拖动多个按钮 View

我有一个应用程序,需要移动成堆的按钮(就像将一堆纸牌的一部分从一堆移动到另一堆一样)。我已经在xml布局中定义了所有按钮,并设置了所有触摸和拖动监听器。

我可以在屏幕上分别拖放任何按钮。但是在某些情况下,我需要做的就是将其他按钮堆叠在我同时单击的原始按钮上。

有没有办法“欺骗”或模拟按下另一个按钮(以便侦听器注册它)?

谢谢

***编辑9/8/15

@Override

public boolean onTouch(View v,MotionEvent e){

//tosty(“mclicking:” + mClicking);

int startpos = 0;

开关(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;

最佳答案

与其尝试为每个按钮复制侦听器,不如将按钮视为组。当您单击组中的按钮时,它分为两个组:上面有按钮的按钮以及您留下的内容。然后将该组拖放到其他组上。

这些组可以是Templerschaf建议的LinearLayouts:

您单击LinearLayout

它注册哪个项目

制作一个新的LinearLayout,在正确的位置包含该项目下方的项目

删除单击的项目下方的项目,并相应地调整大小

然后可以继续拖动布局

然后可以将其放置在另一个LinearLayout上,这会将项目添加到其自己的列表

这与ListView动画非常相似。看到:

https://www.youtube.com/watch?v=_BZIvjMgH-Q

https://www.youtube.com/watch?v=mwE61B56pVQ

https://github.com/bauerca/drag-sort-listview/blob/master/library/src/com/mobeta/android/dslv/DragSortListView.java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值