2017-01-03 33 views
1

我剛開始使用android,並且我對這項技術相對較新。我製作了3個具有拖放功能的水平卷軸,我想將它們拖放到某個區域,同時保持失敗和成功的下降數量。到目前爲止,我完成了一切,但代碼並不是那麼有效,我認爲這種方法並不是最好的方法。我想知道更有效的方法來解決這個問題。我activity_main.xml中如何通過拖放功能從多個Imageviews中選擇ImageView ID

<HorizontalScrollView 
    android:id="@+id/horizontalScrollView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:scrollbars="horizontal"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:onClick="onTouch"> 

     <ImageView 
      android:id="@+id/b1" 
      android:layout_width="200px" 
      android:layout_height="200px" 
      android:layout_gravity="center" 
      android:layout_margin="20dp" 
      android:src= "@drawable/a1"/> 

     <ImageView 
      android:id="@+id/b2" 
      android:layout_width="200px" 
      android:layout_height="200px" 
      android:layout_gravity="center" 
      android:layout_margin="20dp" 
      android:src= "@drawable/a2"/> 

     <ImageView 
      android:id="@+id/b3" 
      android:layout_width="200px" 
      android:layout_height="200px" 
      android:layout_gravity="center" 
      android:layout_margin="20dp" 
      android:src= "@drawable/a3"/> 

     <ImageView 
      android:id="@+id/b4" 
      android:layout_width="200px" 
      android:layout_height="200px" 
      android:layout_gravity="center" 
      android:layout_margin="20dp" 
      android:src= "@drawable/a4"/> 

     <ImageView 
      android:id="@+id/b5" 
      android:layout_width="200px" 
      android:layout_height="200px" 
      android:layout_gravity="center" 
      android:layout_margin="20dp" 
      android:src= "@drawable/a5"/> 

     <ImageView 
      android:id="@+id/b6" 
      android:layout_width="200px" 
      android:layout_height="200px" 
      android:layout_gravity="center" 
      android:layout_margin="20dp" 
      android:src= "@drawable/a6"/> 

     <ImageView 
      android:id="@+id/b7" 
      android:layout_width="200px" 
      android:layout_height="200px" 
      android:layout_gravity="center" 
      android:layout_margin="20dp" 
      android:src= "@drawable/a7"/> 

     <ImageView 
      android:id="@+id/b8" 
      android:layout_width="200px" 
      android:layout_height="200px" 
      android:layout_gravity="center" 
      android:layout_margin="20dp" 
      android:src= "@drawable/a9"/> 

     <ImageView 
      android:id="@+id/b9" 
      android:layout_width="200px" 
      android:layout_height="200px" 
      android:layout_gravity="center" 
      android:layout_margin="20dp" 
      android:src= "@drawable/a10"/> 

     <ImageView 
      android:id="@+id/b10" 
      android:layout_width="200px" 
      android:layout_height="200px" 
      android:layout_gravity="center" 
      android:layout_margin="20dp" 
      android:src= "@drawable/a8"/> 
    </LinearLayout> 

</HorizontalScrollView> 

部分和MainActivity.java

public class MainActivity extends Activity { 

    int id; 
    ImageView drag; 
    LinearLayout drop; 
    TextView text, success, fail; 
    int total, failure, succ = 0, i; 


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




     drag = (ImageView) findViewById(id); 

     drag.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent arg1) { 
       // TODO Auto-generated method stub 
       ClipData data = ClipData.newPlainText("", ""); 
       View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag); 
       v.startDrag(data, shadow, null, 0); 
       return false; 
      } 
     }); 


     drop = (LinearLayout) findViewById(R.id.bottomlinear); 
     text = (TextView) findViewById(R.id.Total); 
     success = (TextView) findViewById(R.id.Success); 
     fail = (TextView) findViewById(R.id.Fail); 

     drop.setOnDragListener(new View.OnDragListener() { 

      @Override 
      public boolean onDrag(View v, DragEvent event) { 
       // TODO Auto-generated method stub 
       final int action = event.getAction(); 
       switch (action) { 

        case DragEvent.ACTION_DRAG_STARTED: 
         break; 

        case DragEvent.ACTION_DRAG_EXITED: 
         break; 

        case DragEvent.ACTION_DRAG_ENTERED: 
         break; 

        case DragEvent.ACTION_DROP: 
         { 
          succ = succ + 1; 
          return (true); 
         } 

        case DragEvent.ACTION_DRAG_ENDED: 
         { 
          total = total + 1; 
          int failure = total - succ; 
          success.setText("Sucessful Drops :" + succ); 
          text.setText("Total Drops: " + total); 
          fail.setText("Failed Drops: " + failure); 
          return (true); 

         } 

        default: 
         break; 
       } 
       return true; 
      } 
     }); 
    } 
} 

上面的代碼適用於僅第一圖像視圖,我需要一個解決方案,使所有的人。我嘗試使用ImageView數組for循環,但只返回最後ImageView。我真的很感謝一些幫助。

回答

1

我用了一個數組,這一點,但主要的問題是,我總是得到最後的圖像視圖不管我選擇的一些子類。此問題已通過使用'final'關鍵字解決。

​​

這使我得到了不同的拖動圖像視圖。所有的答覆都表示讚賞。

+0

對不起,但您還沒有輸入正確的描述與您錯誤的數組實施,我給了你適當的解決方案你的問題,這是不可能知道如何錯誤數組實施 –

+0

接下來的事情,如果你有最終變量,那麼它會拋出每次嘗試重新分配一個例外。 –

+0

我剛纔使用了'final'關鍵字,它在任何情況下總是得到最後一個圖像視圖之前就工作得很好。不發佈錯誤的解決方案是我的不好,我很抱歉,是的,我剛剛嘗試了你的確切解決方案。 它工作正常,除了它總是最終選擇最後一個圖像視圖類似於我的數組用來做'最終'關鍵字解決了這個問題的事實。 – JBJ

1

我認爲你應該使用HorizontalRecyclerview

此鏈接將有助於垂直recyclerview Verticle Reclyclerview drag and drop

然後做一些修改像

組佈局管理水平在recylerview

mRecyclerView.setHasFixedSize(true); 
    mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.HORIZONTAL, false)); 

LinearLayoutManager.HORIZ ONTAL與recylerview

水平列表,你會得到SimpleItemTouchHelperCallback類形式演示 和改良方法getMovementFlags方法類似

@Override 
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { 

      final int dragFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; 
      final int swipeFlags = 0; 
      return makeMovementFlags(dragFlags, swipeFlags); 

    } 

希望這有助於你

2

不改變完全你的實現,我會去用imageView的參數id得到的方法求解。

void dragAndDropImage(String imageId){ 

    // get int id from String 
    int id = getResources().getIdentifier(imageId, "id", getPackageName()); 

    //here Your drag and drop functionality code 
    ImageView drag = (ImageView) findViewById(id); 

    //... rest of code 

} 

使用它:

for (int i=1; i<=10; i++){ 

    //loop over every imageView (from 1 to 10) 
    dragAndDropImage("b"+i); 

} 

很少有更多的重構注意事項:

  • drop, text, success, fail意見應存放在一些類屬性,以避免做findViewById每次
  • touchListener應一些小類的活動
  • onDragListener還應活動
+0

其實,我太累了,使用數組總是返回最後的形象意義不管我選擇它會拖累最後的圖像顯示。我知道這是因爲我在那裏有陰影效果,並且每次拖動其始終最後一個圖像視圖時,無論我選擇的是什麼。 – JBJ

+0

但是,您是否嘗試過我建議的那種解決方案? –

+0

「我嘗試使用ImageView數組與for循環但只返回最後ImageView「它認爲你做錯了什麼,檢查我的解決方案 –

1

在你拖動監聽器的時候,你可以在你傳遞null的地方傳遞對象類型。

drag.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent arg1) { 

       PassObject object=new PassObject(); 
       object.setId("id"); 
       // TODO Auto-generated method stub 
       ClipData data = ClipData.newPlainText("", ""); 
       View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag); 
       v.startDrag(data, shadow, object, 0); 
       return false; 
      } 
     }); 

而在Listener你正在實施他們你可以得到它。

drop.setOnDragListener(新View.OnDragListener(){

 @Override 
     public boolean onDrag(View v, DragEvent event) { 
      // TODO Auto-generated method stub 
      final int action = event.getAction(); 
      switch (action) { 

       case DragEvent.ACTION_DRAG_STARTED: 
        break; 

       case DragEvent.ACTION_DRAG_EXITED: 
        break; 

       case DragEvent.ACTION_DRAG_ENTERED: 
        break; 

       case DragEvent.ACTION_DROP: 
        { 
         //succ = succ + 1; 
         return (true); 
        } 

       case DragEvent.ACTION_DRAG_ENDED: 
        { 
      PassObject passObject = (PassObject) event.getLocalState(); 
       String id=passobject.getID(); 
         return true; 

        } 

       default: 
        break; 
      } 
      return true; 
     } 
    }); 
}