Android imitation Netease News client classification and sorting function
Let's take a look at the Netease News client and its own effect picture. Of course, the effect is still good
gridviewsort.gif
How to drag an item
Add an ImageView with WindowManager, and set the display image of the ImageView to the screenshot of the dragged item. The screenshot can be obtained through the getdrawingcache of the view. When dragging, hide the original item. Handle the actionmove of the touch event, adjust the position of the ImageView and follow the finger. Removeview during actionup
GridView
How to hide dragged items
When you start dragging, tell the adapter the position of the hidden item, call the notifydatasetchanged of the adapter to refresh the data, and judge whether the position of the currently built item needs to be hidden in the getview method. If so, set the view to invisible
GridView
How do I know which item to drag onto
If you want to change positions when dragging onto other items, you must know which item you are currently dragging onto. Grideview provides a method called pointtoposition, which can handle the action of touch events_ During move, get the X and Y touched by your finger to get the position currently dragged onto the item. One thing to note here is that the position of the same item will not change during dragging. The position will not be recalculated unless notifydatasetchanged of the adapter is called. For example, for an item with position 2, no matter how the animation moves the position during the drag process, its position is 2. When the action up is completed after a drag, notifydatasetchanged will be called
GridView
How to realize animation
The distance that an item needs to move horizontally and vertically can be calculated in advance. In fact, the horizontal distance must be the width of a GridView cell plus the horizontal spacing anyway. The vertical distance is the height of a cell plus the vertical distance anyway. The width is very easy to take. If the height is high, Here, the height of the default item is the same as that of the cell.
GridViewSortAdapter
Start animation when you drag onto other items
SortGridView
Here, I mainly explain the function of the mpositionlist list in the code. As mentioned earlier, the position of the item will not change during a drag and drop.
Suppose there is a set of data
a b c d e f g h i j k l
At this time, the content of mpositionlist is 0 1 2 3 4 5 6 7 8 9 10 11 12
Now drag C onto g, and the data after dragging should be that the finger is not released
a b d e f g c h i j k l
At this time, the content of mpositionlist is 0 1 2 4 5 6 7 3 8 9 10 11 12
Then, continue dragging C to e, and you will find that the position obtained by calling the pointtoposition method is 5, but the current index of E is 4
So you just need to call
You can get the position of the real item
other
If the number of columns in the GridView is changed to 1, it's deja vu
gridviewex.gif
Source address
https://github.com/jiahuanyu/android-example-code/tree/master/app/src/main/java/com/github/jiahuanyu/example/ui/dragsortgird
The above is the sorting function of Android imitation Netease News client introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!