如何限制用户可以拖放到特定布局的对象数量



我开发了一个卡片分类应用程序,用户可以根据这里的代码在屏幕上拖放卡片-拖放代码

如何限制可以拖动到特定布局的对象数量?(我想将对象的数量限制为每个布局只有一个(

它看起来就像是在DragEvent.ACTION_DROP中,视图被添加到新的父级中。因此,当这种情况发生时,您可以简单地检查添加到的视图是否已经有一定数量的子级:

更改此项:

case DragEvent.ACTION_DROP:
...
View v = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) v.getParent();
owner.removeView(v);//remove the dragged view
LinearLayout container = (LinearLayout) view;
container.addView(v);
v.setVisibility(View.VISIBLE);

到此:

case DragEvent.ACTION_DROP:
...
LinearLayout container = (LinearLayout) view;
if (container.getChildCount() < 1) { // only move the view if the container has no kids
View v = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) v.getParent();
owner.removeView(v);
container.addView(v);
}
v.setVisibility(View.VISIBLE);

最新更新