Android Learning Series (11)-drag and drop the App list listview (bottom)

Source: Internet
Author: User

Next, let's move to the android Learning Series (10) -- drag the App list listview (top). We will continue to implement the drag-and-drop effect of listview.

7. Override the ontouchevent () method.
In this method, we mainly deal with dragging and dropping.
Drag: the image of the selected item slides with the finger; put down is to exchange data when the drag ends.
The overall structure of the method is as follows:

/*** touch event */@ override public Boolean ontouchevent (motionevent eV) {// If dragmageview is empty, it indicates that only click is determined in the interception event, not drag. Return // If the clicked position is invalid, return. You need to re-judge if (dragimageview! = NULL & dragposition! = Invalid_position) {int action = eV. getaction (); Switch (Action) {Case motionevent. action_up: int upy = (INT) eV. gety (); // release the drag image stopdrag (); // After the image is put down, determine the position to delete and insert ondrop (upy); break; Case motionevent. action_move: int Movey = (INT) eV. gety (); // drag the image ondrag (Movey); break; default: break;} return true;} // The returned value can achieve the selected effect, if true is returned, return Super is not selected. ontouchevent (EV) ;}

8. Drag the image.
When dragging, we call the ondrag (INT y) method. The main thing we do is to let the cursor of the selected item slide with this finger. As follows:

 
If (dragimageview! = NULL) {// set a little transparency windowparams. alpha = 0.8f; // update the Y coordinate position windowparams. y = Y-dragpoint + dragoffset; // update windowmanager. updateviewlayout (dragimageview, windowparams );}

When the data set is large, you also need to scroll the list when dragging to the upper or lower areas, using the built-in setselectionfromtop () method of listview ().
A scrollable drag-and-drop list takes shape and the ondrag () method is used.CodeAs follows:

/*** run the drag command. Run the * @ Param y */Public void ondrag (INT y) command in the move method) {If (dragimageview! = NULL) {windowparams. alpha = 0.8f; windowparams. y = Y-dragpoint + dragoffset; windowmanager. updateviewlayout (dragimageview, windowparams);} // The Int tempposition = pointtoposition (0, Y); If (tempposition! = Invalid_position) {dragposition = tempposition;} // scroll int scrollheight = 0; If (Y 
  
    downscrollbounce) {scrollheight =-8; // defines 8 pixels to scroll down. If you can scroll up,} If (scrollheight! = 0) {// The method of true scrolling setselectionfromtop () setselectionfromtop (dragposition, getchildat (dragposition-getfirstvisibleposition (). gettop () + scrollheight); }}
  

The drag effect is as follows:
 

9. Put down the image and update the data.
The above Implementation of the drag effect, put down the image:
1) We need to obtain which data set is put down;
2) insert and drag data in the put down position item, and delete the original position item of the drag data.
These processes are written in the ondrop () method and executed in the action_up action. The Code is as follows:

/*** @ Param y */Public void ondrop (INT y) when dragging and dropping) {// obtain the position of the put-down position in the dataset. // define the temporary position variable. In order to avoid sliding to the split line, the-1 problem is returned. If it is-1, the value of dragposition is not modified, and execution is urgently needed to skip the invalid position. Int tempposition = pointtoposition (0, Y); If (tempposition! = Invalid_position) {dragposition = tempposition;} // If (Y <getchildat (0) when the boundary is exceeded ). gettop () {// exceeds the upper boundary and is set to the minimum position 0 dragposition = 0;} else if (Y> getchildat (getchildcount ()-1 ). getbottom () {// exceeds the bottom boundary and is set to the maximum position. Note: if it is greater than the bottom of the largest view in the visual interface, it is the lower bound. Therefore, getchildcount () is used in the judgment () method // but the position of the last item in the dataset is getadapter (). getcount ()-1, which must be distinguished by clearing dragposition = getadapter (). getcount ()-1;} // data update if (dragposition> 0 & dragposition <getadapter (). getcount () {@ suppresswarnings ("unchecked") arrayadapter <string> adapter = (arrayadapter <string>) getadapter (); string dragitem = adapter. getitem (dragsrcposition); // Delete the original position data adapter. remove (dragitem); // Insert the drag item adapter in the new position. insert (dragitem, dragposition );}}

When we put it down, we can update the data and save the dragged data status (although it is saved in the dapter ). The effect is as follows:
I processed it (define the getlist () method in the dapter method to get the list <string> In adpter and connect it using the tostring () method). I will output the result of the adapter to see it:

As for the detailed processing or saving of data, it is not the content dragged in this document. It is easy to grasp the adapter set for analysis.

Iii. Expansion

10. Expand by group drag and drop.
We have already added group tags (Group A and Group B) to the data source. We will divide the data into group A and group B. For more information, see
Android Learning Series (9)-App list group listview.
1) define the group label style layout drag_list_item_tag.xml.

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Background = "#555555" Android: padding = "5dip" Android: paddingleft = "10dip"> <! -- The text box id remains unchanged --> <textview Android: Id = "@ + ID/drag_list_item_text" Android: layout_width = "wrap_content" Android: layout_height = "20dip" Android: textcolor = "# ffffff" Android: gravity = "center_vertical"/> <! -- Remove and drag the image on the right. The group labels cannot be dragged randomly. --> </linearlayout>

2) modify the getview () method in draglistadapter.

@ Override public view getview (INT position, view convertview, viewgroup parent) {view = convertview; If (groupkey. contains (getitem (position) {// if it is a group tag, load the group tag layout file. The two layout files have different display effects. view = layoutinflater. from (getcontext ()). inflate (R. layout. drag_list_item_tag, null);} else {// if it is a normal data item tag, add it to the layout file of the normal data item view = layoutinflater. from (getcontext ()). inflate (R. layout. drag_list_item, null);} textview = (textview) view. findviewbyid (R. id. drag_list_item_text); textview. settext (getitem (position); Return view ;}

3) disable the RESPONSE event of the group label item, and rewrite the isenable () method in draglistaapter ().
Because the drag and drop image is removed from the group label, if you click in the group label, dragimageview is empty and will not be dragged, this is a good use of the above.

@ Override public Boolean isenabled (INT position) {If (groupkey. contains (getitem (position) {// if it is a group tag, false is returned. If it is not selected, return false;} return Super. isenabled (position );}

4) label items cannot be dragged, so we need to modify the upper boundary control in ondrop.

 
// Change the upper boundary to 1 If (Y <getchildat (1 ). gettop () {// exceeds the upper boundary dragposition = 1;} else if (Y> getchildat (getchildcount ()-1 ). getbottom () {// exceeds the bottom boundary dragposition = getadapter (). getcount ()-1 ;}

The final result is:

now, the implementation of drag-and-drop listview is complete.
if you find any bugs, contact me.
it is rare to upload the code. I didn't find the place where my blog could upload files. I put it on GitHub, which is:
https://github.com/fjtianxia/qianxudetianxia.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.