如何从Android中的2个不同的LinearLayout交换按钮?

时间:2015-01-23 12:57:36

标签: android

我想在从源布局拖动到目标布局和目标布局的同时执行图像按钮交换。但我没有得到任何解决方案。请帮我解决问题。自从过去10天以来,我一直在努力。

<LinearLayout
        android:id="@+id/source"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/image1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:background="@drawable/bamboo7" />

        <ImageButton
            android:id="@+id/image2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_gravity="center_vertical"
            android:background="@drawable/bamboo4" />

        <ImageButton
            android:id="@+id/image3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_gravity="center_vertical"
            android:background="@drawable/bamboo4"
             />

        <ImageButton
            android:id="@+id/image4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_gravity="center_vertical"
            android:background="@drawable/dragon_chun" />

        <ImageButton
            android:id="@+id/image5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_gravity="center_vertical"
            android:background="@drawable/pin8" />

        <ImageButton
            android:id="@+id/image6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_gravity="center_vertical"
            android:background="@drawable/dragon_haku" />

        <ImageButton
            android:id="@+id/image7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_gravity="center_vertical"
            android:background="@drawable/pin3" />
</LinearLayout>

public class Game_play extends Activity implements OnTouchListener, OnDragListener
{
ImageButton i1, i2, i3, i4, i5, i6, i7;
ImageButton r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14;
LinearLayout source, target;

 @Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_play);

    source = (LinearLayout)findViewById(R.id.source);
    target = (LinearLayout)findViewById(R.id.target);

    i1 = (ImageButton)findViewById(R.id.image1);
    i2 = (ImageButton)findViewById(R.id.image2);
    i3 = (ImageButton)findViewById(R.id.image3);
    i4 = (ImageButton)findViewById(R.id.image4);
    i5 = (ImageButton)findViewById(R.id.image5);
    i6 = (ImageButton)findViewById(R.id.image6);
    i7 = (ImageButton)findViewById(R.id.image7);


    i1.setOnTouchListener(this);        
    i1.setOnDragListener(this);
    i2.setOnTouchListener(this);        
    i2.setOnDragListener(this);
    i3.setOnTouchListener(this);        
    i3.setOnDragListener(this);
    i4.setOnTouchListener(this);        
    i4.setOnDragListener(this);
    i5.setOnTouchListener(this);        
    i5.setOnDragListener(this);
    i6.setOnTouchListener(this);        
    i6.setOnDragListener(this);
    i7.setOnTouchListener(this);        
    i7.setOnDragListener(this);


    final ImageButton r1 = (ImageButton)findViewById(R.id.r1);
    final ImageButton r2 = (ImageButton)findViewById(R.id.r2);
    final ImageButton r3 = (ImageButton)findViewById(R.id.r3);
    final ImageButton r4 = (ImageButton)findViewById(R.id.r4);
    final ImageButton r5 = (ImageButton)findViewById(R.id.r5);
    final ImageButton r6 = (ImageButton)findViewById(R.id.r6);
    final ImageButton r7 = (ImageButton)findViewById(R.id.r7);
    final ImageButton r8 = (ImageButton)findViewById(R.id.r8);
    final ImageButton r9 = (ImageButton)findViewById(R.id.r9);
    final ImageButton r10 = (ImageButton)findViewById(R.id.r10);
    final ImageButton r11 = (ImageButton)findViewById(R.id.r11);
    final ImageButton r12 = (ImageButton)findViewById(R.id.r12);
    final ImageButton r13 = (ImageButton)findViewById(R.id.r13);
    final ImageButton r14 = (ImageButton)findViewById(R.id.r14);

    r1.setOnTouchListener(this);        
    r1.setOnDragListener(this);
    r2.setOnTouchListener(this);        
    r2.setOnDragListener(this);
    r3.setOnTouchListener(this);        
    r3.setOnDragListener(this);
    r4.setOnTouchListener(this);        
    r4.setOnDragListener(this);
    r5.setOnTouchListener(this);        
    r5.setOnDragListener(this);
    r6.setOnTouchListener(this);        
    r6.setOnDragListener(this);
    r7.setOnTouchListener(this);        
    r7.setOnDragListener(this);
    r8.setOnTouchListener(this);        
    r8.setOnDragListener(this);
    r9.setOnTouchListener(this);        
    r9.setOnDragListener(this);
    r10.setOnTouchListener(this);       
    r10.setOnDragListener(this);
    r11.setOnTouchListener(this);       
    r11.setOnDragListener(this);
    r12.setOnTouchListener(this);       
    r12.setOnDragListener(this);
    r13.setOnTouchListener(this);       
    r13.setOnDragListener(this);
    r14.setOnTouchListener(this);       
    r14.setOnDragListener(this);
}
public boolean onTouch(View v, MotionEvent event) 
{
    // TODO Auto-generated method stub  
    ClipData data = ClipData.newPlainText("", "");
    switch (event.getAction()) 
    {
    case MotionEvent.ACTION_DOWN:
        moving=true;
        break;
    case MotionEvent.ACTION_MOVE:
        if (moving) 
         {
            DragShadowBuilder shadow = new View.DragShadowBuilder(v);
            v.startDrag(data, shadow, v, 0);
            v.setVisibility(View.VISIBLE);
         }
    case MotionEvent.ACTION_UP:
        //v.setBackground(v.getBackground());
        moving=false;
        break;
    }
    return false;
}
public boolean onDrag(View v, DragEvent e) 
{
    if (e.getAction()==DragEvent.ACTION_DROP) 
    {
        View view = (View) e.getLocalState();
        switch (e.getAction()) 
        {
            case DragEvent.ACTION_DRAG_STARTED:
             // do nothing
              break;
            case DragEvent.ACTION_DRAG_ENTERED:
              //v.setBackgroundDrawable(enterShape);
              break;
            case DragEvent.ACTION_DRAG_EXITED:        
              //v.setBackgroundDrawable(normalShape);
              break;
            case DragEvent.ACTION_DROP:
              // Dropped, reassign View to ViewGroup
                ViewGroup owner = (ViewGroup) view.getParent();
                owner.removeView(view);
                LinearLayout container = (LinearLayout) v;
                container.addView(view);
                view.setVisibility(View.VISIBLE);
                break;
            case DragEvent.ACTION_DRAG_ENDED:
              //v.setBackgroundDrawable(normalShape);
              default:
              break;
        }
        return true;
   }
    else 
    {
        return false;
    }
 }
}

0 个答案:

没有答案