こちらの続きです。
D&Dで入れ替わるリストビュー1 - C#ATIA
ListViewでは無く、ListTileの方が綺麗だからそちらを使おうと
試したのですが、ちょっと思った様な書き方が出来ずに断念。
(恐らく力量不足)
結局ListViewにしてみた所、前回のものがほぼ流用出来ました。
# python using-flet import flet as ft class DragAndDropItem(ft.UserControl): def __init__(self, group: str, value: str): super().__init__() self.group = group self.vaule = value def build(self): self.view = ft.Draggable( group = self.group, content = ft.DragTarget( group = self.group, content = ft.Text( self.vaule, size = 50 ), on_accept = self._drag_accept, ) ) return self.view def _drag_accept(self, e: ft.DragTargetAcceptEvent): src = e.page.get_control(e.src_id) target = e.page.get_control(e.target) target.content.value, src.content.content.value = src.content.content.value, target.content.value src.update() target.update() def main(page: ft.Page): page.title = "Drag and drop test" groupName = "test" textLst = [ "hoge", "piyo", "fuga", "poyo", ] listView = ft.ListView( expand=1, spacing=10, padding=20, auto_scroll = True ) [listView.controls.append(DragAndDropItem(groupName, t)) for t in textLst] page.add(listView) page.update() ft.app(target = main)
今は、ドロップイベント時に投げ込んだコントロールの文字と、投げ込まれた
コントロールの文字をスワップさせているのですが、本当はお互いの
コントロールをスワップさせたいんです。
こんな感じです。
・・・ def _drag_accept(self, e: ft.DragTargetAcceptEvent): src = e.page.get_control(e.src_id) target = e.page.get_control(e.target) # target.content.value, src.content.content.value = src.content.content.value, target.content.value target.content, src.content.content = src.content.content, target.content src.update() target.update() ・・・
これ、駄目なんですよ。許してくれません。
しょうがないか。