HandyGridView
HandyGridView 本质上是一个 GridView,所以你也可以当成普通的 GridView 来使用,HandyGridView 继承了 GridView 并在此之上添加了 item 拖动和交换,绘制图文等功能。 由于只是一个 GridView,所以性能比目前其他大部分解决方案都要好。
HandyGridView is a high-performance drag and drop GridView, it extends GridView, you can drag drop GridView item to sort the labels,and draw something on the GridView. Just use the HandyGridView like a GridView.
Screenshots

Usage
Gradle
dependencies {
compile 'com.huxq17.handygridview:handygridview:1.2.0'
}
minSdkVersion 11
HandyGridView's three modes:
| Mode | introduction |
|---|---|
| TOUCH | Edit mode,the item can be dragged |
| LONG_PRESS | Long press mode,item can be dragged after long press. |
| NONE | Item can not be dragged, jsut like normal GridView. |
Usage:
HandyGridView#setMode(TOUCH|LONG_PRESS|NONE);
Adapter
HandyGridView 会在 item 被拖动交换时发出通知,如果想要做出对应数据上的变化,则可以在 Apdater 中实现 OnItemMovedListener,示例如下:
HandyGridView will send a notification to notify you swip the data source when its item's order is changed. the usage is as follows:
public class GridViewAdapter extends BaseAdapter implements OnItemMovedListener{
@Override
public void onItemMoved(int from, int to) {
String s = mDatas.remove(from);
mDatas.add(to, s);
}
@Override
public boolean isFixed(int position) {
//When postion==0,the item can not be dragged.
if (position == 0) {
return true;
}
return false;
}
}
绘制图文
HandyGridView 提供了在 gridview 上绘制图文的接口,示例如下:
You can draw something on HandyGridView, the usage is as follows:
mGridView.setDrawer(new IDrawer() {
@Override
public void onDraw(Canvas canvas, int width, int height) {
if (!mGridView.isNoneMode()) {
int offsetX = -DensityUtil.dip2px(MainActivity.this, 10);
int offsetY = -DensityUtil.dip2px(MainActivity.this, 10);
//文字绘制于 gridview 的右下角,并向左,向上偏移 10dp。
//Draw text on the right-bottom of GridView.
drawTips(canvas, width + offsetX, height + offsetY);
}
}
},false);
private void drawTips(Canvas canvas, int width, int height) {
if (mTextPaint == null) {
mTextPaint = new TextPaint();
mTextPaint.setColor(Color.parseColor("#CFCFCF"));
mTextPaint.setTextSize(DensityUtil.dip2px(MainActivity.this, 12));
Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
textHeight = (int) (fontMetrics.bottom - fontMetrics.top) + 1;
textWidth = (int) mTextPaint.measureText(paintText) + 1;
}
width = width - textWidth;
height = height - textHeight;
if (tipsLayout == null) {
tipsLayout = new StaticLayout(paintText, mTextPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.5f, 0f, false);
}
canvas.translate(width, height);
tipsLayout.draw(canvas);
}
以上就是主要的用法了,更多的用法可以参考 example.
The above is the main usage,click to get more.
更新日志
2018-8-29:
1.解决 HandyGridView 在没有 item 并且 horizontalSpacing 为 0dp 或者没有设置时触摸会挂掉的问题
2017-12-29:
1.解决某些小米手机上 item 拖动交换时会闪烁的问题,更新到 1.1.0
