如何使具有可拖动的项目一个ListView?拖动、项目、ListView

2023-09-05 08:11:54 作者:附送成长

我一直在寻找一个列表preference中,用户可以从列表中更改项目的顺序。该项目将可拖动,并且可以被重新排序由用户

I was looking for a ListPreference in which the user can change the order of items from a list. The items would be draggable and can be re-ordered by the user.

我在我的自定义ROM看到这个(我几乎可以肯定我看到了它的CyanogenMod),为的QuickPanel。下面是截图通过对得到的想法:

I saw this in my custom ROM (and I'm almost sure I saw it in Cyanogenmod) for the QuickPanel. Here's a screenshot to get the idea through:

我知道我可以进行自定义的ListView项目和设置的图标,以表明项目是可拖动,但我不知道如何使它们拖动,并相应地改变顺序。至于它们保存在preferences,我发现这它可以很容易地实现。

I know how I can make custom ListView items and set the icon to indicate that the items are draggable, but I don't know how to make them draggable, and change the order accordingly. As for saving them in the preferences, I found this which could be implemented easily.

PS 的:我知道了CyanogenMod是开源的,但我无法找到源出于此特定的事物:(最接近我能得到的是这个,这应该是附近的另一个屏幕的某个地方......

PS: I know Cyanogenmod is open-source, but I couldn't find the source for this particular thing :( The closest I could get was this, which should be somewhere near the other screen...

在此先感谢这个任何提示。

Thanks in advance for any tip about this.

更新:我结束了使用从接受的答案文件,以补充和修改。我列出他们在这里进行进一步的参考。

UPDATE: I ended up using the files from the accepted answer, with additions and modifications. I am listing them here for further reference.

使用自定义适配器(ArrayAdapter在我的情况),实现视觉反馈,这种产品可拖动,这是附近的TextView的一个ImageView的。这是可选的。

Use a custom Adapter (ArrayAdapter in my case), to implement the visual feedback that this item is draggable, which is an ImageView near the TextView. This is optional.

设置DragListener和的removeListener相应地更新列表。 ListView控件不会自动做。它取决于你所使用的适配器。

Set a DragListener and RemoveListener to update the list accordingly. The ListView does not do that automatically. And it depends on the Adapter you are using.

有一条线,铸造视野下的一个ViewGroup中,它取得了一些错误,所以我删除的演员,没有任何问题,这是没有必要的。 (在onInterceptTouchEvent法)。

There was a line that casted a View to a ViewGroup, it made some errors, so I removed the cast without any issue, it was not needed. (in the onInterceptTouchEvent method).

修改 mRemoveMode = 1; 在TouchInterceptor,或之一的构造函数: FLING = 0;滑动= 1; TRASH = 2; 。我觉得已删除邮件,资源应该可用了。

Change mRemoveMode = 1; in the constructor of TouchInterceptor, or one of: FLING = 0; SLIDE = 1; TRASH = 2;. I think for TRASH, a resource should be available too.

其实我接过文件不是从答案的链接,但在CyanogenMod的一个,这我已经有了,但我想这些文件是相同的。

I actually took the file not from the answer's link but from the Cyanogenmod one, which I already had, but I guess these files were the same.

这些都是在项目的实际文件(在R12,在写作的时候):

These are the actual files in the project (at r12, at the time of writing):

使用此ListView中的preference 与监听器和适配器 的ListActivity 实际的ListView The Preference using this ListView The ListActivity with the listeners and the Adapter The actual ListView

我希望它可以帮助别人:)

I hope it helps somebody else :)

推荐答案

有没有内置的小工具要做到这一点,但你可能想看看使用的AOSP音乐播放器,以便重新自定义实现歌曲在播放列表中的排序。

There is no built-in widget to do this, but you may want take a look at the custom implementation used by the AOSP music player to allow for re-ordering of songs in playlists.

TouchInterceptor.java

这是扩大ListView和负责处理所有的MotionEvents和重复订购的物品,以及检测挥笔删除项目的类。你可以看到它落实到一项活动在这里的 TrackBrowserActivity.java 类。

That's the class which is extending ListView and is responsible for handling all of the MotionEvents and re-ordering its items, as well as detecting swipes for deleting items. You can see it implemented into an activity here in the TrackBrowserActivity.java class.

它有三个接口,你也应该知道的:DragListener,DropListener和的removeListener。您可以使用这些接口来提供它回调火的那些事件,这样就可以更新到您节省preferences到列表顺序所做的更改(因为ListView控件将不能处理你)。

It has three interfaces you should also be aware of: DragListener, DropListener, and RemoveListener. You can use these interfaces to provide it callbacks to fire for those events, so that you can update changes made to the list order to your SavedPreferences (since the ListView will not handle that for you).

您可以轻松地扩展或修改TouchInterceptor类添加额外的功能,因为低层次的东西都是在你身边。

You can easily extend or modify the TouchInterceptor class to add additional functionality since the low-level stuff is all there for you.