Java實(shí)現(xiàn)拖拽列表項(xiàng)的排序功能
在一些允許用戶自定義欄目順序的app(如:鳳凰新聞、網(wǎng)易云音樂(lè)等),我們可以方便地拖拽列表項(xiàng)來(lái)完成列表的重新排序,進(jìn)而完成對(duì)欄目順序的重排。這個(gè)功能很人性化,而實(shí)現(xiàn)起來(lái)其實(shí)很簡(jiǎn)單(甚至都不用寫什么后臺(tái)代碼),只有三步。
①把冰箱門打開
首先,我們需要讓冰箱的大門敞開,也就是允許我們進(jìn)行拖拽的相關(guān)操作。以ListView為例,注意下面幾個(gè)屬性。
<StackPanel>
<ListView x:Name="list"
AllowDrop="True"
CanReorderItems="True"
IsSwipeEnabled="True">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Background" Value="Gray"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Margin" Value="4"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
<Button Click="Button_Click">Show Items</Button>
<TextBlock x:Name="txt"/>
</StackPanel>
AllowDrop屬性允許元素進(jìn)行拖動(dòng),它繼承自UIElement基類,為所有可視元素支持。
CanReorderItems屬性繼承自ListViewBase基類,允許列表控件的項(xiàng)可以重新排序。
IsSwipeEnabled屬性(swipe有“輕掃”之意)也需要設(shè)置為“True”,否則在觸摸屏等輸入設(shè)備下無(wú)法進(jìn)行操作。相關(guān)的詳盡說(shuō)明在MSDN文檔里有介紹(https://docs.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.Controls.ListViewBase),此部分摘錄部分原文:
Remarks
Setting IsSwipeEnabled to false disables some default touch interactions, so it should be set to true when these interactions are needed. For example:
If item selection is enabled and you set IsSwipeEnabled to false, a user can deselect items by right-clicking with the mouse, but can't deselect an item with touch by using a swipe gesture.
If you set CanDragItems to true and IsSwipeEnabled to false, a user can drag items with the mouse, but not with touch.
If you set CanReorderItems to true and IsSwipeEnabled to false, a user can reorder items with the mouse, but not with touch.
You typically set IsSwipeEnabled to false to disable swipe animations when items in the view don't support interactions that use the swipe gesture, like deselecting, dragging, and reordering. Disabling the animation when it's not needed can improve the performance of your app.
(有趣的是最后一段:當(dāng)列表不允許輕掃手勢(shì)(撤銷選定,拖動(dòng),拖拽重排)時(shí),我們可以“顯式”地將IsSwipeEnabled屬性設(shè)置為False來(lái)提升應(yīng)用的性能。)
②把大象裝進(jìn)去
前臺(tái)ok后,我們就可以在后臺(tái)加點(diǎn)東西,把我們的排序邏輯(其實(shí)并沒(méi)有,微軟已經(jīng)寫好了)添加進(jìn)去。這個(gè)demo里,我用了一個(gè)按鈕和一個(gè)文本框來(lái)觀察重排的結(jié)果。如下:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
for (int i = 0; i < 10; i++)
{
list.Items.Add($"-----THIS IS ITEM {i}-----");
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
txt.Text = string.Empty;
foreach (var item in list.Items)
{
txt.Text += item.ToString()[18] + " ";
}
}
}
這樣,重新排序后,點(diǎn)擊按鈕,我們即可觀察到結(jié)果了。
③把冰箱門關(guān)上
把大象(?)裝進(jìn)去之后,最后就是我們的收尾工作了。顯然,剛才的列表只是一個(gè)中間的載體,是我們待排序欄目的簡(jiǎn)單顯示。一般而言,這個(gè)listview會(huì)安置在contentdialog或是popup里,那么怎么在重排后立即讓父頁(yè)面上的欄目得到相應(yīng),進(jìn)行重排呢?我們用個(gè)預(yù)定義的委托即可,加在剛才的后臺(tái)代碼里(冰箱能裝的東西其實(shí)挺多的)。
public Action action;
然后在父頁(yè)面注冊(cè)方法,比如:
btn.Click += async (s, e) =>
{
var dialog = new Dialogs.Sort();
dialog.action += async () => { await sortagain(); };
await dialog.ShowAsync();
};
以上所述是小編給大家介紹的Java實(shí)現(xiàn)拖拽列表項(xiàng)的排序功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java實(shí)現(xiàn)批量下載(打包成zip)的實(shí)現(xiàn)
這篇文章主要介紹了Java實(shí)現(xiàn)批量下載(打包成zip)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Java 數(shù)據(jù)結(jié)構(gòu)與算法系列精講之字符串暴力匹配
字符串暴力匹配算法是指在一個(gè)長(zhǎng)字符串中暴力尋找是否包含某一子串所謂暴力匹配,就是不使用任何其他算法,將兩個(gè)字符串中的字符一一進(jìn)行比對(duì)2022-02-02
maven本地有包但是引不進(jìn)來(lái)的解決方案
Spring MessageSource獲取消息不符合預(yù)期的問(wèn)題解決方案
IDEA中thymeleaf語(yǔ)法沒(méi)有提示的問(wèn)題及解決
struts2標(biāo)簽總結(jié)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
java中hashmap容量的初始化實(shí)現(xiàn)
Jenkins安裝多個(gè)jdk版本并在項(xiàng)目中選擇對(duì)應(yīng)jdk版本
SpringBoot連接MySql數(shù)據(jù)庫(kù)的原理及代碼示例

