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

Android 如何限制用户可以拖放到特定布局的对象数量?,android,android-studio,Android,Android Studio,我开发了一个卡片分类应用程序,用户可以根据这里的代码在屏幕上拖放卡片- 如何限制可以拖动到特定布局的对象数量? 我想将每个布局的对象数量限制为仅一个它看起来就像是在DrageEvent.ACTION\u下拉列表中,视图被添加到新的父视图中。因此,当发生这种情况时,您只需检查将其添加到的视图是否已经有一定数量的子视图: 更改此项: case DragEvent.ACTION_DROP: ... View v = (View) event.getLocalState();

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

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

它看起来就像是在DrageEvent.ACTION\u下拉列表中,视图被添加到新的父视图中。因此,当发生这种情况时,您只需检查将其添加到的视图是否已经有一定数量的子视图:

更改此项:

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);

@tamariwolfman很高兴我能帮上忙,请记住选择并投票选出对您有帮助的答案: