2017-01-03 59 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; 
     } 
    }); 
}