- Notifications
You must be signed in to change notification settings - Fork70
CWAC TouchListView: A Drag-and-Drop Capable ListView
License
timsu/cwac-touchlist
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The standardListView
is a very sophisticated tool, but itlacks any sort of drag-and-drop support. The Android opensource project has an example of implementing drag-and-dropon aListView
, in the form of theTouchInterceptor
classused in the Music application — you use this to re-arrangea playlist. However, that widget is not in the Android SDKas of Android 2.2.
TouchListView
is 99% the Android open source code forTouchInterceptor
.TouchListView
also allows the widgetto be configured from an XML layout file, replacing somehard-wired values thatTouchInterceptor
uses.
This is distributed as an Android library project, followingthe conventions ofthe Android Parcel Project.You can download a ZIP file containing just the library project(sans sample code) from the Downloads section of this GitHubrepository.
You will see a sample project that usesTouchListView
in thedemo/
directory of the repository. This project is designedto be built via Ant from the command line. Compile the top-levelproject first viaant parcel
, then compile and install thedemo project viaant demo
.
To work withTouchListView
in your own project, place thelibrary project somewhere and update your project to referencethe library project (via Eclipse, viaandroid update lib-project
, etc.).Then, add acom.commonsware.cwac.tlv.TouchListView
widget to your XML layout file. You have five customizableattributes:
normal_height
: the height of one of your regular rows (required)expanded_height
: the largest possible height of one ofyour rows (defaults to the value ofnormal_height
)grabber
: theandroid:id
value of an icon in your rowsthat should be used as the "grab handle" for the drag-and-dropoperation (required)dragndrop_background
: a color to use as the background of yourrow when it is being dragged (defaults to being fully transparent)remove_mode
: can benone
(user cannot remove entries),slideRight
(user can remove entries by dragging to the right quarter of the list),slideLeft
(user can remove entries by dragging to the left quarter of the list),or fling (...not quite sure what this does) (defaults tonone
)
NOTE:remove_mode
ofslide
is equivalent toslideRight
, butslideRight
is recommended.
For example, here is the layout from thedemo/
project:
<?xml version="1.0" encoding="utf-8"?><com.commonsware.cwac.tlv.TouchListViewxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tlv="http://schemas.android.com/apk/res/com.commonsware.cwac.tlv.demo"android:id="@android:id/list"android:layout_width="fill_parent"android:layout_height="fill_parent"android:drawSelectorOnTop="false"tlv:normal_height="64dip"tlv:grabber="@+id/icon"tlv:remove_mode="slideRight"/>
You will need to change thecom.commonsware.cwac.tlv.demo
toyour own project's package in thetlv
namespace declaration.
In code, you set up aTouchListView
just like a regularListView
, except that you need to register aTouchListView.DropListener
viasetDropListener()
. In your listener, you will need to dosomething to affect the re-ordering requested via the drag-and-dropoperation. In the demo project, this is a matter of removingthe entry from the old position and putting it in the new position.
If you have remove_mode enabled, you will also need to register aTouchListView.RemoveListener
viasetRemoveListener()
. Thiswill be notified when the user removes an entry from the list.Once again, you need to make this change permanent in yourdata model. In the demo project, this removes the row from theactualArrayAdapter
supporting the list.
There is also aTouchListView.DragListener
that you can registerviasetDragListener()
, if you want to know when the userhas initiated a drag operation.
This depends upon theCWAC-Parcel
JAR for accessingproject-level resources. That can be obtained from itsGitHub repository,though a compatible edition of the JARis included in this GitHub repo for convenience.
This is version v0.2.0 of this module, meaning it is pretty darnnew, though the underlying Android open source code has beenin use for quite some time.
There is ademo/
directory containing a demo project. It usesthe library project itself to access the source code andresources of theTouchListView
library.
The code in this project is licensed under the ApacheSoftware License 2.0, per the terms of the included LICENSEfile.
If you have questions regarding the use of this code, pleasejoin and ask them on thecw-android Google Group. Be sure toindicate which CWAC module you have questions about.
v0.2.0: converted to Android library project
Bear in mind that the person who convertedTouchInterceptor
intoTouchListView
does not fully understand the originalTouchInterceptor
code, and so support may be limited.
About
CWAC TouchListView: A Drag-and-Drop Capable ListView
Resources
License
Uh oh!
There was an error while loading.Please reload this page.