Drag multiple button views in Java
I have an application where I need a button to move the stack (like moving part of a pile of cards from one pile to another) I have defined all the buttons in the XML layout and set up touch and drag listeners for everyone
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);
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);
}
b.setLayoutParams(params);
// b.bringToFront();
// b.invalidate();
b.setVisibility(View.VISIBLE);
return b;
Solution
Instead of trying to copy listeners for each button, I treat the buttons as groups When you click a button in a group, it is divided into two groups: the button above the button and what you want to leave Then drag and drop the group into another group
As templerschaf suggests, these groups can be linear layouts:
>Click LinearLayout > which item it registers > place the new LinearLayout containing the item below the item in the correct position > delete the item below the clicked item and resize it accordingly > then you can continue to drag the layout > and then you can put it on another LinearLayout, which will add the item to your list
This is much like listview animation See:
https://www.youtube.com/watch?v=_BZIvjMgH -Q
https://github.com/bauerca/drag-sort-listview/blob/master/library/src/com/mobeta/android/dslv/DragSortListView.java
