是否可以在可拖动列表上有多个布局?

问题描述 投票:0回答:0

我有多个具有不同布局的类,我在我的应用程序中称它们为 Widgets(这与所谓的“Android Widgets”完全不同)。我想将这些类的实例并排放置在一个名为 Dashboard 的活动中。因此,用户可以在仪表板内更改这些小部件的位置和设置。有什么建议吗?

我尝试使用 itemtouchhelper 但问题是拖动布局仅支持一种类型布局 AFAIK。在多个视图类型上拖放很奇怪。

像这样:

 private void swapOneAndTwoSizeWidget(int from, int to) {
        List<WidgetInfo> list = mAdapter.getList();
        WidgetInfo source = list.get(from);
        WidgetInfo target = list.get(to);
        // swap two column item and one column item
        swapLocation(source, target); 
        swapLocation(target, list.get(to + 1));
        updateSort(source, target, list.get(to + 1)); 
        moveItems(from, to); 
    }

    private void swapLocation(WidgetInfo a, WidgetInfo b) {
        int tempX = a.getLocationX();
        int tempY = a.getLocationY();
        a.setLocationX(b.getLocationX());
        a.setLocationY(b.getLocationY());
        b.setLocationX(tempX);
        b.setLocationY(tempY);
    }

    private void updateSort(WidgetInfo... widgets) {
        List<WidgetInfo> listToSwap = new ArrayList<>();
        for (WidgetInfo widget : widgets) {
            if (!widget.isAdd()) {
                listToSwap.add(widget);
            }
        }
        mAdapter.updateSort(listToSwap);
    }

    private void moveItems(int from, int to) {
        List<WidgetInfo> list = mAdapter.getList();
        if (from < to) {
            for (int i = from; i < to + 1; i++) {
                Collections.swap(list, i, i + 1);
            }
            mAdapter.notifyItemMoved(from, to + 1);
        } else {
            for (int i = from; i > to; i--) {
                Collections.swap(list, i, i - 1);
            }
            mAdapter.notifyItemMoved(from, to);
        }
    }

交换之前: enter image description here

交换: enter image description here

我的期望是 enter image description here

android
© www.soinside.com 2019 - 2024. All rights reserved.