0

我是 android 新手,根据要求在 gridview 上实现拖放以实现交换操作,所有工作正常,并且还交换了 gridview 中的项目我到目前为止所做的:- 在适配器类中将 touchlistner 实现到 imageview

这里-

holder.disp_imgview.setOnTouchListener(new MyTouchListener(mcontext));

holder 是适配器类的内部类和触摸事件的 MyTouchListner 类。

这里 -

public final class MyTouchListener  implements OnTouchListener {

public boolean onTouch(View view, MotionEvent motionEvent) {
        // TODO Auto-generated method stub
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            vb=(Vibrator) mcontext.getSystemService(Context.VIBRATOR_SERVICE);
            vb.vibrate(100);
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.VISIBLE);
            view_position=view.getId();
            first_image_view=view;

            Log.v("IMAGE TOUCH", "CLICKED");
            return true;
}

在哪里DragShadowBuilder shadowBuilder为用户触摸的特定图像创建阴影并开始拖动view.startDrag(data, shadowBuilder, view, 0);

接下来仅在适配器类中实现图像视图的拖动事件

这里-

holder.disp_imgview.setOnDragListener(new MyDragListener());

class MyDragListener implements OnDragListener
        {

            @Override
            public boolean onDrag(View v, DragEvent event) {
                // TODO Auto-generated method stub
                sec_view_id=v.getId();
                 first_view_id=MyTouchListener.view_position;


                switch(event.getAction())
                {
                case DragEvent.ACTION_DRAG_STARTED:

                    Log.e("ACTION", "DRAG_STARTED");

                    break;
                case DragEvent.ACTION_DRAG_ENTERED:
                    v.setBackgroundDrawable(enterShape);

                    Log.e("ACTION", "DRAG_ENTERED");
                    break;
                case DragEvent.ACTION_DRAG_LOCATION:
                    Log.e("ACTION", "DRAG_ENTERED");
                    break;
                case DragEvent.ACTION_DROP:

                    if(first_view_id!=sec_view_id)
                    {
                        Integer temp=my_image_list.get(first_view_id);
                        Integer temp_sec=my_image_list.get(sec_view_id);
                        my_image_list.set(first_view_id, temp_sec);
                        my_image_list.set(sec_view_id, temp);
                        image_adapter.notifyDataSetChanged();
                    }
                    else
                    {
                    Toast.makeText(mcontext, "CAN'T PLACE THE SAME IMAGE", 0).show();   
                    }
                    Log.e("ACTION", "ACTION_DROP");
                    break;
                case DragEvent.ACTION_DRAG_ENDED:
                    Log.e("ACTION", "DRAG_ENDED");
                    v.setBackgroundDrawable(normalShape);
                break;
                case DragEvent.ACTION_DRAG_EXITED:
                    Log.e("ACTION", "DRAG_EXITED");
                    break;

                }

                return true;
            }

        }

正如您在操作中看到的那样,在arraylist(my_image_list)中交换图像,然后通知适配器反映更改并且工作正常,但现在问题是

我们如何在第二个项目上做动画,使它看起来像移动到 gridview 中的第一个项目位置,并且动画将在动作下降后发生。

4

0 回答 0