Dragging multiple button views in java

I have an app where I need to move stacks of buttons (just like moving part of a stack of cards from one pile to another). I have defined all the buttons in an xml layout and setup touch and drag listeners for all. I can drag and drop any button around the screen individually. but what I need to do in some cases is drag the other buttons stacked on top of the original button I clicked at the same time. Is there a way to "trick" or simulate that another button is pressed (so the listener registers it)? Thanks ***Edited 9/8/15 @Override 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;

Rather than trying to replicate the listeners for each button, I would consider the buttons as groups. When you click on a button within a group, it splits into two groups: the button with the buttons above it and what you are leaving behind. The group is then dragged and dropped onto other groups.

The groups could be LinearLayouts as Templerschaf suggests:

  • You click the LinearLayout
  • It registers which item
  • Makes a new LinearLayout containing the items below that item in the correct position
  • Deletes the items below the clicked item and resizes accordingly
  • Then the layout can continue to be dragged
  • It can then be dropped on another LinearLayout, which will add the items to its own list
  • This is quite like ListView animations. See:

    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

    链接地址: http://www.djcxy.com/p/87660.html

    上一篇: 设置bgcolor属性值

    下一篇: 在java中拖动多个按钮视图