Android implementation picture selection drag and drop control

Source: Internet
Author: User

1. Using Recyclerview

The Recyclerview can be used to easily animate the image when switching, which is better than the GridView.

2. Drag and drop implementation

    • Dragmanager

Inherit the View.ondraglistener, operate on the drag process,

Action_drag_started gets the item to the action

The action_drag_location determines whether the position of the item is exchanged based on the position of each stop.

Action_drag_ended and new location

    • Dragsortadapter

Abstract class, add Onitemtouchlistener and Onscrolllistener for Recyclerview, record Ontouch item, and determine if Recyclerview can scroll during drag and drop. This lets you scroll through the Recyclerview when the dragged item is fast to the boundary, allowing you to swap positions with item that would otherwise be invisible on the screen.

    • Viewholder

Viewholder Implementing the StartDrag method


2.itemDecoration


To add divider to Recyclerview item, there are two ways to override the OnDraw method to draw Itemdivider, or to override the Getitemoffsets method so that item can be separated.

3.GridlayoutManager

When the Recyclerview is embedded in the ScrollView, a carbon layoutmanager is required, where the Onmeasure method is replicated, and the height or width of each item needs to be calculated to overlay, when the Recyclerview item is many , do not use this way, back to cause view can not be reused, which in the calculation item height, need to add each item's itemoffsets, view Recyclerview source discovery, unable to get directly to the item offsets, Finally, it takes reflection to get its value:

try {method = Recyclerview.getclass (). Getdeclaredmethod ("Getitemdecorinsetsforchild", View.class);    Method.setaccessible (TRUE);    Final Rect insets = (rect) method.invoke (Recyclerview, child);    Itemdecorationheight = heightused + insets.height (); Itemdecorationwidth = widthused + Insets.width ();} catch (Nosuchmethodexception e) {log.d ("Fullgridlayoutmanager", "no Method found"); catch (Illegalaccessexception e) {log.d ("Fullgridlayoutmanager", "illegalaccessexception");} catch (InvocationTargetException e) {log.d ("Fullgridlayoutmanager", "invocationtargetexception");}



Android implementation picture selection drag and drop control

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.