用户可以将视图从ListView
拖到onItemLongClick
ListView
上的布局。根据一些客户的反馈,我想使用onItemClick
来拖动视图onItemLongClick
。使用onitemClick时startDrag()
无效。请在下面找到我的代码。可能是什么问题 ?如果我们使用onItemLongClick
OnItemClickListener onItemClick = new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
ClipData clipData = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new CustomDragShadowBuilder(getActivity());
view.startDrag(clipData, shadowBuilder,null, 0);
}
}
};
答案 0 :(得分:0)
首先看一下:http://www.vogella.com/tutorials/AndroidDragAndDrop/article.html
你使用startDrag的地方是错误的尝试在touchlistener中使用它。您可以在适配器的getview方法中向行添加触摸侦听器。我写的一个小例子在listview中显示动物图像,你可以将这些图像拖放到容器(布局)中
主要活动中的
公共类MainActivity扩展了Activity {
public static final String TAG="Drag-Drog";
ListView list;
Integer[] imageId = { R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4, R.drawable.image5,
R.drawable.image6,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomList adapter = new CustomList(MainActivity.this,
R.layout.list_row, imageId);
list = (ListView) findViewById(R.id.listview1);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
}
});
findViewById(R.id.container1).setOnDragListener(new MyDragListener());
}
public 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 (action) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
Log.d(TAG,"on ACTION_DRAG_STARTED");
break;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackground(enterShape);
Log.d(TAG,"on ACTION_DRAG_ENTERED");
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackground(normalShape);
Log.d(TAG,"on ACTION_DRAG_EXITED");
break;
case DragEvent.ACTION_DROP:
// the view dropped
// getlocatestate probably gets the reference of this view
View view = (View) event.getLocalState(); // get the view which
// will be dropped
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view); // remove the view from the first
// position
LinearLayout container = (LinearLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
Log.d(TAG,"on ACTION_DRAG_STARTED");
break;
case DragEvent.ACTION_DRAG_ENDED:
v.setBackground(normalShape);
Log.d(TAG,"onACTION_DRAG_ENDED");
break;
}
return true;
}
}
}
在自定义触控侦听器中
公共类MyTouchListener实现了OnTouchListener {
public static final String TAG="Drag-Drog";
@Override
public boolean onTouch(View v, MotionEvent event) {
// when touch event occurs first check the document
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data=ClipData.newPlainText("", "");
//ClippedData is a complex type containing one or Item instances,
//each of which can hold one or more representations of an item of data
DragShadowBuilder shadowBuilder=new View.DragShadowBuilder(v);
v.startDrag(data, shadowBuilder,v, 0);
v.setVisibility(View.VISIBLE);
Log.d(TAG,"on Action Down");
return true;
}
/*
if(event.getAction()==MotionEvent.)
*/
else
return false;
}
}
这是这里的自定义适配器
公共类CustomList扩展了ArrayAdapter {
private final Context context;
private final Integer[] imageId;
public CustomList(Context c, int resource, Integer[] imageId) {
super(c, resource, imageId);
this.context = c;
this.imageId = imageId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_row, null, false);
viewHolder = new ViewHolder();
viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
convertView.setTag(viewHolder);
} else {
// we call the view created before to not create a view in each time
viewHolder = (ViewHolder) convertView.getTag();
}
int imgId = imageId[position];
viewHolder.img.setImageResource(imgId);
viewHolder.img.setOnTouchListener(new MyTouchListener());
return convertView;
}
public class ViewHolder {
ImageView img;
public ImageView getImg() {
return img;
}
public void setImg(ImageView img) {
this.img = img;
}
}
}