我是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);
接下来仅在适配器类中实现imageview的拖动事件
此处 -
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中的第一个项目位置,动画将在动作放弃后发生。