在java中拖动多个按钮视图

在java中拖动多个按钮视图,java,android,drag-and-drop,Java,Android,Drag And Drop,我有一个应用程序,我需要移动一堆按钮(就像将一堆卡片的一部分从一堆移动到另一堆)。我已经定义了xml布局中的所有按钮,并为所有按钮设置了触摸和拖动侦听器。 我可以在屏幕周围单独拖放任何按钮。但在某些情况下,我需要做的是将其他按钮拖动到我同时单击的原始按钮的顶部。 有没有一种方法可以“欺骗”或模拟按下另一个按钮(以便听者注册)? 谢谢 ***2015年9月8日编辑 @凌驾 公共布尔onTouch(视图v,运动事件e){ //托斯蒂(“麦克舔:+麦克舔”); int startpos=0; 开关(如

我有一个应用程序,我需要移动一堆按钮(就像将一堆卡片的一部分从一堆移动到另一堆)。我已经定义了xml布局中的所有按钮,并为所有按钮设置了触摸和拖动侦听器。 我可以在屏幕周围单独拖放任何按钮。但在某些情况下,我需要做的是将其他按钮拖动到我同时单击的原始按钮的顶部。 有没有一种方法可以“欺骗”或模拟按下另一个按钮(以便听者注册)? 谢谢 ***2015年9月8日编辑 @凌驾 公共布尔onTouch(视图v,运动事件e){ //托斯蒂(“麦克舔:+麦克舔”); int startpos=0; 开关(如getAction()&MotionEvent.ACTION\u掩码){

case MotionEvent.ACTION\u向下:
isWastePile=false;
get_selected_deck(v);//确定您在tablau中拥有7个组或布局中的哪一个
//点击
FromDeck=选择Deck;
FromDeckCard=deckcard;
FromDeckButton=deckbutton;
//如果(!mClicking){
mClicking=true;
//String piecetag=(String)v.getTag();
////想法!!!////
/*
*我写了一个函数,可以找到下面所有的ImageButton
*用户单击,并将它们全部设置为不可见
*用户单击的线性布局中的新线性布局(在ACTION_DOWN事件期间),
*并在ACTION_MOVE事件期间将其传递到Drag Shadow builder。
* 
*进入ACTION_DROP部分后,我简单地引用了global
*变量,以确定用户是插入一个还是多个
*图像按钮,并相应地处理它们。
*/
//如果(!iswastepill){
//draglayout.setClipChildren(false);
lltemp=新的线性布局(本);
低温设置方向(线性布局、垂直);
LinearLayout.LayoutParams llparams=新的LinearLayout.LayoutParams(
LayoutParams.WRAP_内容,LayoutParams.WRAP_内容);
llparams.setMargins(0,-52,0,0);
lltemp.setLayoutParams(llparams);
draglayout.addView(lltemp);
对于(int i=0;istartpos){
参数设置边距(0,-57,0,0);
}
b、 setLayoutParams(参数);
//b.布林托夫隆();
//b.使无效();
b、 设置可见性(View.VISIBLE);
返回b;

> p>而不是尝试为每个按钮复制侦听器,我将按钮视为组。当您单击组内的按钮时,它分成两组:按钮上方的按钮和您留下的按钮。然后将该组拖拽到其他组。

正如Templerschaf建议的那样,这些组可以是线性布局:

  • 单击线性布局
  • 它记录了哪个项目
  • 在正确位置创建一个新的线性布局,其中包含该项目下方的项目
  • 删除单击项下的项并相应调整大小
  • 然后可以继续拖动布局
  • 然后可以将其放置在另一个LinearLayout上,这将把项目添加到它自己的列表中
这与ListView动画非常相似。请参见:

澄清
    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;