Objective
Product: NetEase News that tab sort good handsome.
Development: Oh ~
And then this stuff comes up in a few days ...
(PS: Almost a year did not come back to write blog ~ ~ ~ ~ ~ ~ (>_<) ~ ~ ~ ~ ~ ~ ~, by the name from the enjoy wind chimes modified to yards Uncle Farm
How to use
1. Introduction of XML layout
< com.net168.lib.SortTabLayout Android:id = "@+id/layout" android:layout_width= "Match_parent" android:layout_height= "Wrap_ Content " />
2, set the data source data, that is, the corresponding text data for each item
1 // constructs a data source and temporarily supports only string 2 New Arraylist<string>(); 3 for (int i = 0; i <; i + +) {4 data.add ("item" + i); 5 }6// Set data source 7 vsortlayout.setshowdata (1);
3. Set the listener for interactive tap and long press events
1Vsortlayout.setonselectlistener (NewOnselectlistener () {2 //Click on the event and click the item trigger in the tab layout3 @Override4 Public voidOnSelect (View V,intindex) {5Toast.maketext (mainactivity. This, "You clicked on item and the content is:" +( (TextView) v). GetText (), Toast.length_short). Show ();6 }7 //Long press event, long press the item trigger in the tab layout8 @Override9 Public voidOnlongselect (View v) {TenToast.maketext (mainactivity. This, "Long press TAB, start arranging", Toast.length_short). Show (); One } A});
4. Start sorting and end sorting interfaces
1 // If the argument is true, start sorting, that is, you can drag 2 vsortlayout.setismovelist (true); 3 // ends the sort and returns the current new position of the Selection tab 4 vsortlayout.getandfinishsortdata ();
5, basically no other ~
Incomplete Custom Features
1, now only support string, and layout can not be customized, subsequent may be perfect tab item's view of the custom input
2, now the layout of the number of lines and spacing by hard code control, and did not form a simple interface burst leakage
PS: The method of debugging spacing, mainly debugging the following several parameters
/*** Configuration Parameter area * Mmaxrow: Number of rows per line * Magin width tab spacing and the ratio of the widths of itself * For example Mmaxrow = 4, the width is assigned by this policy * | magin| view| magin| view| magin| view| magin| view| magin| * Remember the width of the control is so assigned: total width = 5 * Magin + 4 * View, while Magin:view = Mrowmagin:mrowwidth * Can deduce the width of each control, height is also calculated*/Private Final intMmaxrow = 4;Private Final intMrowmagin = 5;Private Final intMrowwidth = 26;Private Final intMcolumnmagin = 4;Private Final intMcolumnheight = 10;
3, rollback is not smooth, late can introduce scroller to control slow rollback
Principle Realization
1. The layout item sort uses the custom layout based on ViewGroup, and the method logic in OnLayout is calculated according to the parameters of the configuration parameter area.
1 @Override2 protected voidOnLayout (BooleanARG0,intArg1,intArg2,intArg3,intArg4) {3 4 Final intChildCount =Getchildcount ();5 6 intRow = 0;7 intColumn = 0;8 intStartwidth = 0;9 intStartheight = 0;Ten One for(inti = 0; i < ChildCount; i++) { AView Childeview =Childlist.get (i); -row = I/4; -Column = i% 4; theStartwidth = (int) ((column * (Mrowwidth + 2 * mrowmagin) + mrowmagin) *mchildeitemsize); -Startheight = (int(Row * (Mcolumnheight + 2 * mcolumnmagin) + mcolumnmagin) *mchildeitemsize); -Childeview.layout (Startwidth, Startheight, (int) (Startwidth + mrowwidth *mchildeitemsize), -(int) (Startheight + mcolumnheight *mchildeitemsize)); + } - +}
2, sliding module part, in Ontouchevent in accordance with the capture of coordinates, coordinate analysis of the corresponding sub-item, using the View.layout () method to let the dragged view follow the finger move, participate in the code
1 Private voidMovechildview (floatXfloaty) {2 if(Mmovechildview! =NULL) {3 intleft = (int) ((((mchildindex% 4) * (Mrowwidth + 2 * mrowmagin) + mrowmagin) *mchildeitemsize);4 inttop = (int) (((MCHILDINDEX/4) * (Mcolumnheight + 2 * mcolumnmagin) + mcolumnmagin) *mchildeitemsize);5 intwidth = (int) (Left + mrowwidth *mchildeitemsize);6 intHeigth = (int) (Top + mcolumnheight *mchildeitemsize);7 intMoveX = (int) (X-BeginX);8 intMovey = (int) (Y-beginy);9Mmovechildview.layout (left + MoveX, top + movey, Width + moveX, Heigth +Movey);Ten mmovechildview.invalidate (); One } A}
3, animation module, due to consider the low version and do not want to introduce too many open source libraries, so the use of ordinary animation implementation
See BeginAnimation (final int start,final int end, Boolean forward) method.
4. Overall process
A, touch down event, captures the current x, y data, calculates the corresponding index of the moved view, and the rest of the view starts dithering animation
B, Move event, the selected view according to x, y using layout method to follow the finger movement
C, up event, perform position adjustment animation, and after the adjustment is complete, make a new position setting
Conclusion
Because lazy, so lazy ~
Controls are written for demand and are not as easy to scale as open source, and are time-optimized
GitHub Address: Https://github.com/CodeFarmerUncle168/SortTabLayout
Uncle Nong (enjoy wind chimes)
Source:http://www.cnblogs.com/net168/
This article is copyright to the author and the blog Park is shared, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to the original connection, or next time not to you reproduced
Android Open Source series: Imitation netease tab sorting sorting control implementation