Xamarin.Forms可重新排序的WrapLayout

时间:2017-02-07 11:55:33

标签: c# layout xamarin xamarin.forms reorderlist

我有一组单元格(StackLayout类型),它们都在WrapLayout中。我似乎无法弄清楚如何使其重新排序,类似于Drag & Drop ListView

这是WrapLayout:

WrapLayout layout = new WrapLayout
{
    Spacing = 5,
    Orientation = StackOrientation.Horizontal,
    Padding = new Thickness(5, Device.OnPlatform(20, 5, 5), 5, 5),
    HorizontalOptions = LayoutOptions.FillAndExpand,
    VerticalOptions = LayoutOptions.FillAndExpand,
};

这是一个单元格:

// Configure cell
var cell = new StackLayout
{
    WidthRequest = Device.Idiom == TargetIdiom.Phone ? 60 : 140,
    HeightRequest = Device.Idiom == TargetIdiom.Phone ? 60 : 50,
    BackgroundColor = Color.FromRgb(60, 56, 72),
    Children = {
        new Label {
            Text = Device.Idiom == TargetIdiom.Phone ? book.Contains("1") || book.Contains("2") || book.Contains("3") ? book.Substring(0,4).Replace(" ", "") : book.Substring(0,2) : book,
            FontSize = 18,
            TextColor = Color.White,
            LineBreakMode = LineBreakMode.TailTruncation,
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalTextAlignment = TextAlignment.Center,
            HorizontalTextAlignment = TextAlignment.Center
        }
    }
};
layout.Children.Add(cell);

以下是该应用程序截至目前的实际情况: The App

任何有助于实现这一目标的帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

你所指的WrapLayout是一个自定义布局,它是由我相信的开发人员创建的其中一个演示。要具有拖放功能,您必须自己手动实现它。

我个人会将事件处理程序附加到单元格的Tapped事件:

var cell = new StackLayout
{
    Tapped += Cell_Tapped;
    WidthRequest = Device.Idiom == TargetIdiom.Phone ? 60 : 140,
    HeightRequest = Device.Idiom == TargetIdiom.Phone ? 60 : 50,
    BackgroundColor = Color.FromRgb(60, 56, 72),
    Children = {
        new Label {
            Text = Device.Idiom == TargetIdiom.Phone ? book.Contains("1") || book.Contains("2") || book.Contains("3") ? book.Substring(0,4).Replace(" ", "") : book.Substring(0,2) : book,
            FontSize = 18,
            TextColor = Color.White,
            LineBreakMode = LineBreakMode.TailTruncation,
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalTextAlignment = TextAlignment.Center,
            HorizontalTextAlignment = TextAlignment.Center
        }
    }
};
layout.Children.Add(cell);

然后创建一个事件处理程序:

void Cell_Tapped(object sender, EventArgs e)
{
    // Stuff you want to do with that cell
    // Make the current selected cell invisible, attach it to a boxview that is being moved by the tap event
}

但这只适用于点按而不是真正的拖放实现,而Xamarin.Forms目前不允许您访问触摸事件。您可以为iOS和Android上的单元格编写自定义渲染器,以允许您在两个平台上访问TouchesBegan和TouchesEnded类型的事件。

Xamarin link提供了有关在iOS和Android上集成触控的演练。

我希望这会有所帮助,我相信这有不同的实现方式。