Android custom GridView Fake Alipay home drag, removable nine Sudoku

Source: Internet
Author: User

As a result of the project needs, to achieve a similar Alipay home nine Sudoku function. A reference to the network of multiple projects is not enough to meet demand. Finally decided to change their own one, first look at the effect of the picture



The third picture is the one where the drag has been modified.

The following is the layout of the main function

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://
    Schemas.android.com/apk/res/android "android:layout_width=" match_parent "android:layout_height=" Match_parent " android:orientation= "vertical" > <linearlayout android:layout_width= "fill_parent" Android:layo
            ut_height= "wrap_content" android:orientation= "vertical" > <com.tisson.test.draggrid Android:id= "@+id/usergridview" android:layout_width= "fill_parent" android:layout_height= "MATC" H_parent "android:layout_gravity=" center_horizontal "android:gravity=" Center "android:
            Listselector= "@android: Color/transparent" android:numcolumns= "4" android:scrollbars= "vertical" Android:stretchmode= "ColumnWidth"/> </LinearLayout> </linearlayout> 
For the item's split line, it is easy to set the ListView, just set the ListView color and width of the line, define the Android:d Ivider and Android:dividerheight properties in the layout. But the GridView does not have such a property and method, here is to set the background with each item, the background defines the selector, in which a shape is rectangular rectangle, set the rectangle's stroke stroke properties of the color of the split line color, This item will have a split line in four weeks. Defines one, and the following is the layout of the selector bg_gv.xml

<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android= "http://schemas.android.com/apk/res/" Android "> <item android:state_pressed=" true "><shape android:shape=" Rectangle "> <stroke Android:width= "1.0px" android:color= "#ffd3dde6"/> <gradient android:angle= "270.0" android:endcolor= "#f Fe8ecef "android:startcolor=" #ffe8ecef "/> </shape></item> <item ANDROID:STATE_FOCU Sed= "true" ><shape android:shape= "Rectangle" > <gradient android:angle= "270.0" android:endcolor= "#ff 
        E8ecef "android:startcolor=" #ffe8ecef "/> <stroke android:width=" 1.0px "android:color=" #ffd3dde6 "/> </shape></item> <item><shape android:shape= "Rectangle" > <gradient andr Oid:angle= "270.0" android:endcolor= "#ffffffff" android:startcolor= "#ffffffff"/> <stroke "1.0px" android:color= "#ffd3dde6"/>
 </shape></item> </selector>

The drag of the inherited GridView custom Draggrid,item is saved as bitmap by the cache mechanism. Hide click on item. Modify the bitmap position through the ontouchevent (motionevent ev). After the drag completes, the item's location is updated with the adapter dragadapter, showing the item that was previously hidden.

Here are some of the code

Dragitemview = (viewgroup) getchildat (Downpos-getfirstvisibleposition ());
win_view_x = Windowx-dragitemview.getleft ()//view relative to its own x,
win_view_y = Windowy-dragitemview.gettop ();// View relative to its own y, the
//cache mechanism is saved as bitmap
Dragitemview.destroydrawingcache ();
Dragitemview.setdrawingcacheenabled (true);
	Bitmap Dragbitmap = Bitmap.createbitmap (Dragitemview.getdrawingcache ());
	Mvibrator.vibrate (50),//Set vibration time
	StartDrag (dragbitmap, (int) ev.getrawx (), (int) Ev.getrawy ());
	Hidedropitem ();
	Dragitemview.setvisibility (view.invisible);


Deletion is judged by the range of clicks and deletions of the imageview. Click on the click of three kinds of situation to deal with (1. Delete event, 2. Cancel drag state time, 3.item Click Time, priority is from 1-3). This avoids the conflict with the item click event. The following is part of the code that deletes the processing of the event

ViewGroup Dragviewgroup = (viewgroup) getchildat (Clickpos-getfirstvisibleposition ());
ImageView Deleteicon = (imageview) Dragviewgroup.findviewbyid (R.id.delete_icon);
int dragiconviewx = Dragviewgroup.getright ()-deleteicon.getwidth ();
int dragiconviewy = Dragviewgroup.gettop ();
if (Dragiconviewx<clickx && clickx<dragviewgroup.getright () &&
   Dragiconviewy < clicky && Clicky < (dragiconviewy + deleteicon.getheight ())) {
Dragadapter mdragadapter = (dragadapter) Getadapter ();
Mdragadapter.remove (Clickpos);
Mdragadapter.setreset (true);
return;
}
Refer to the project code below. There is no understanding of the update of the description content.

Project Link: Project source code


Add: On the feedback that ITEM8 can not drag the problem, the previous idea is ITEM8 as a function of adding modules, rather than as a drag function module. Workaround: Find this segment code in Draggrid.java if (startposition = = GetCount ()-1) {return false;} You can just drop the comment.



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.