2014-11-21 44 views
0

我需要實現這樣的場景之間,但我不知道如何處理它:安卓移動圖像GridView的

gridview

黑色矩形是兩個GridView的,藍光廣場是圖像,紅色的只是爲了向你展示一個佔位符)。

我需要在網格視圖之間拖動圖像,讓用戶選擇4-5圖像(他們走在紅色正方形)......所以拖動從上到下,從下到上(選擇和取消選擇)。 任何人都可以幫助我?

回答

0

通過在您的活動中實現OnTouchListener和OnDragListener,您可以輕鬆完成此任務。 在可繪製文件夾中創建shape_droptarget.xml和shape.xml。我已經爲自己的工作實現了它。我希望它能給你一些幫助。

shape_droptarget.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

    <stroke 
     android:width="2dp" 
     android:color="#FFFF0000" /> 

    <gradient 
     android:angle="225" 
     android:endColor="#DD2ECCFA" 
     android:startColor="#DD000000" /> 

    <corners 
     android:bottomLeftRadius="7dp" 
     android:bottomRightRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp" /> 

</shape> 

shape.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

    <stroke 
     android:width="2dp" 
     android:color="#FFFFFFFF" /> 

    <gradient 
     android:angle="225" 
     android:endColor="#DD2ECCFA" 
     android:startColor="#DD000000" /> 

    <corners 
     android:bottomLeftRadius="7dp" 
     android:bottomRightRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp" /> 

</shape> 

創建在佈局文件夾

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <GridLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/darkred" 
     android:columnCount="2" 
     android:columnWidth="300dp" 
     android:orientation="vertical" 
     android:rowCount="2" 
     android:stretchMode="columnWidth" > 

    <LinearLayout 
     android:id="@+id/topleft" 
     android:layout_width="160dp" 
     android:layout_height="200dp" 
     android:layout_column="0" 
     android:layout_row="0" 
     android:background="@drawable/shape" > 

     <ImageView 
      android:id="@+id/myimage1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/topright" 
     android:layout_width="160dp" 
     android:layout_height="200dp" 
     android:layout_column="1" 
     android:layout_row="0" 
     android:background="@drawable/shape" > 

     <ImageView 
      android:id="@+id/myimage2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/bottomleft" 
     android:layout_width="160dp" 
     android:layout_height="200dp" 
     android:layout_column="0" 
     android:layout_row="1" 
     android:background="@drawable/shape" > 

     <ImageView 
      android:id="@+id/myimage3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/bottomright" 
     android:layout_width="160dp" 
     android:layout_height="200dp" 
     android:layout_column="1" 
     android:layout_row="1" 
     android:background="@drawable/shape" > 

     <ImageView 
      android:id="@+id/myimage4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

</GridLayout> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
    > 
    </View> 
    <GridLayout 
     android:background="@color/darkgreen" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:columnCount="4" 
    android:columnWidth="300dp" 
    android:orientation="horizontal" 
    android:rowCount="1" 
    android:stretchMode="columnWidth" > 

    <LinearLayout 
     android:id="@+id/topleft1" 
     android:layout_width="160dp" 
     android:layout_height="200dp" 
     android:layout_column="0" 
     android:layout_row="0" 
     android:background="@drawable/shape" > 

     <ImageView 
      android:id="@+id/myimage11" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/topright1" 
     android:layout_width="160dp" 
     android:layout_height="200dp" 
     android:layout_column="1" 
     android:layout_row="0" 
     android:background="@drawable/shape" > 

     <ImageView 
      android:id="@+id/myimage21" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/bottomleft1" 
     android:layout_width="160dp" 
     android:layout_height="200dp" 
     android:layout_column="2" 
     android:layout_row="0" 
     android:background="@drawable/shape" > 

     <ImageView 
      android:id="@+id/myimage31" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/bottomright1" 
     android:layout_width="160dp" 
     android:layout_height="200dp" 
     android:layout_column="3" 
     android:layout_row="0" 
     android:background="@drawable/shape" > 

     <ImageView 
      android:id="@+id/myimage41" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

</GridLayout> 
</LinearLayout> 

draglayout.xml文件創建活動DragActivity

public class DragActivity extends Activity { 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.draglayout); 
    findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener()); 
    findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener()); 
    findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener()); 
    findViewById(R.id.myimage4).setOnTouchListener(new MyTouchListener()); 
    findViewById(R.id.topleft).setOnDragListener(new MyDragListener()); 
    findViewById(R.id.topright).setOnDragListener(new MyDragListener()); 
    findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener()); 
    findViewById(R.id.bottomright).setOnDragListener(new MyDragListener()); 


    findViewById(R.id.myimage11).setOnTouchListener(new MyTouchListener()); 
    findViewById(R.id.myimage21).setOnTouchListener(new MyTouchListener()); 
    findViewById(R.id.myimage31).setOnTouchListener(new MyTouchListener()); 
    findViewById(R.id.myimage41).setOnTouchListener(new MyTouchListener()); 
    findViewById(R.id.topleft1).setOnDragListener(new MyDragListener()); 
    findViewById(R.id.topright1).setOnDragListener(new MyDragListener()); 
    findViewById(R.id.bottomleft1).setOnDragListener(new MyDragListener()); 
    findViewById(R.id.bottomright1).setOnDragListener(new MyDragListener()); 
    } 

    private final class MyTouchListener implements OnTouchListener { 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
     ClipData data = ClipData.newPlainText("", ""); 
     DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
     view.startDrag(data, shadowBuilder, view, 0); 
     view.setVisibility(View.INVISIBLE); 
     return true; 
     } else { 
     return false; 
     } 
    } 
    } 

    class MyDragListener implements OnDragListener { 
    Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget); 
    Drawable normalShape = getResources().getDrawable(R.drawable.shape); 

    @Override 
    public boolean onDrag(View v, DragEvent event) { 
     int action = event.getAction(); 
     switch (event.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 
     View view = (View) event.getLocalState(); 
     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; 
    } 
    } 
} 

現在,您可以將圖像從一個網格移動到另一個網格,也可以在網格內移動。

+0

是的,但你看到如果我拖動圖像,當拖動時圖像仍舊在舊的地方(如重複) – 2014-11-26 08:47:34

+0

圖像正在移動,因爲你的指針,它正在與指針移動。可以更多地解釋什麼你完全想要做的。也可以設置圖像視圖的高度和寬度等於父母,因爲根據你的要求,它不應該在它自己的容器中移動。你可以從xml文件中的第二個網格的圖像視圖中移除android:src。 – 2014-11-26 12:55:51