SoFunction
Updated on 2025-03-11

Android implements the free drag effect of item in GridView

In my previous work, I needed to implement a function that the item in the GridView can be dragged freely. After thinking about it, it is not very difficult to implement. The main job is to exchange nodes and the movement effect during dragging. Let’s talk about the specific implementation:

First declare a BaseAdapter:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* Created by zhangyulong on 16/9/22.
*/
public abstract class BaseDragableAdapter<T> extends BaseAdapter {
protected List<T> mList;
protected Context mContext;
protected LayoutInflater mInflater;
//Initialization location information to be hiddenprotected int mHidePosition = AdapterView.INVALID_POSITION;
public BaseDragableAdapter(Context context) {
 = context;
 = (context);
mList = new ArrayList<T>();
}
@Override
public int getCount() {
return mList == null ? 0 : ();
}
@Override
public Object getItem(int i) {
return mList == null ? 0 : (i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public abstract View getView(int i, View view, ViewGroup viewGroup);
public void setList(List<T> list) {
if ( != null) (); // Avoid dirty dataif (list == null) {
return;
}
 = list;
}
public void setList(T[] list){
ArrayList<T> arrayList = new ArrayList<T>();
for (T t : list) {
(t);
}
setList(arrayList);
}
public List<T> getList() {
return ;
}
public void addAll(List<T> list) {
if (list == null || () == 0) return;
(list);
}
public void addAll(int location, List<T> list) {
if (list == null || () == 0) return;
(location, list);
}
public void clean() {
if (getCount() == 0) return;
();
}
public void hideView(int position){
mHidePosition = position;
notifyDataSetChanged();
}
public void showHideView(){
//Reset hidepositionmHidePosition = AdapterView.INVALID_POSITION;
notifyDataSetChanged();
}
/**
 * Switch node
 * @param draggedPos The start node of dragging
 * @param currentPos dragged current node
 */
public void swapView(int draggedPos , int currentPos) {
//Drag from front to backif(draggedPos < currentPos){
//Move the dragged node to the current node(currentPos + 1 , (draggedPos));
//Delete the node before dragging(draggedPos);
}
//Drag from behind to frontelse if (draggedPos > currentPos) {
//Move the dragged node to the current node(currentPos , (draggedPos));
//Delete the node before dragging(draggedPos + 1);
}
mHidePosition = currentPos;
notifyDataSetChanged();
}
}

Secondly, implement DragableGridView:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* Created by zhangyulong on 16/9/22.
*/
public class DragableGridView extends GridView {
private static final int DRAG_IMG_SHOW = 1;
private static final int DRAG_IMG_NOT_SHOW = 0;
private static final String LOG_TAG = "DragGridView";
private static final float AMP_FACTOR = 1.2f;
private ImageView dragImageView;
private  dragImageViewParams;
private WindowManager windowManager;
private boolean isViewOnDrag = false;
/**previous dragged over position*/
private int preDraggedOverPositon = AdapterView.INVALID_POSITION;
private int downRawX;
private int downRawY;
public DragableGridView(Context context) {
super(context);
initView();
}
public DragableGridView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public DragableGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
public void initView() {
setOnItemLongClickListener(mOnItemLongClickListener);
//setOnItemClickListener(onItemClickListener);
//setOnItemSelectedListener(onItemSelectedListener);
//Initialize the image view of the dragged itemdragImageView = new ImageView(getContext());
(DRAG_IMG_NOT_SHOW);
//Initialize the parameter object used to set dragImageViewdragImageViewParams = new ();
//Get window management object, used to add dragImageView to the window laterwindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
}
private OnItemLongClickListener mOnItemLongClickListener = new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//Record long press item positionpreDraggedOverPositon = position;
//Get the drawing cache that is long pressed on the item();
(true);
//By long pressing the item, get the bitmap of the drag itemBitmap dragBitmap = (());
//Set the parameters of dragging the item =  | ;
//Set drag the item to 1.2 times the original item = (int)(AMP_FACTOR*());
 = (int)(AMP_FACTOR*());
//Set the touch point as the center of the drawing drag item = (downRawX - /2);
 = (downRawY - /2);
 = .FLAG_NOT_FOCUSABLE
| .FLAG_NOT_TOUCHABLE
| .FLAG_KEEP_SCREEN_ON
| .FLAG_LAYOUT_IN_SCREEN;
//Setting window supports transparency = ;
 = 0;
//dragImageView is the container of the dragged item, clearing the last displayif((int)() == DRAG_IMG_SHOW) {
(dragImageView);
(DRAG_IMG_NOT_SHOW);
}
//Set the item you have been pressed for this time(dragBitmap);
//Add drag item to screen(dragImageView, dragImageViewParams);
(DRAG_IMG_SHOW);
isViewOnDrag = true;
//The setting is long pressed to the item will not be displayed((BaseDragableAdapter)getAdapter()).hideView(position);
return true;
}
};
@Override
public boolean onTouchEvent(MotionEvent ev) {
//Record the coordinates pressed when pressedif(() == MotionEvent.ACTION_DOWN) {
//Get the coordinates of the touch point relative to the screendownRawX = (int)();
downRawY = (int)();
}
// When the dragImageView is being dragged, update the dragImageView positionelse if((() == MotionEvent.ACTION_MOVE) && (isViewOnDrag == true)) {
(LOG_TAG, "" + () + " " + ());
//Set the touch point to dragImageView center = (int)(() - ()/2);
 = (int)(() - ()/2);
//Update window display, move the location of dragImageView(dragImageView, dragImageViewParams);
//Get the item position of the current touch pointint currDraggedPosition = pointToPosition((int)(), (int)());
//If the current item is not equal to the item of the last item of the stay, exchange the item of this and last time stayedif((currDraggedPosition != AdapterView.INVALID_POSITION) && (currDraggedPosition != preDraggedOverPositon)) {
((BaseDragableAdapter)getAdapter()).swapView(preDraggedOverPositon, currDraggedPosition);
preDraggedOverPositon = currDraggedPosition;
}
}
//Release dragImageViewelse if((() == MotionEvent.ACTION_UP) && (isViewOnDrag == true)) {
((BaseDragableAdapter)getAdapter()).showHideView();
if((int)() == DRAG_IMG_SHOW) {
(dragImageView);
(DRAG_IMG_NOT_SHOW);
}
isViewOnDrag = false;
}
return (ev);
}
}

Implement an adapter inherits from BaseDragableAdapter:

package ;
import ;
import ;
import ;
import ;
import ;
/**
* Created by zhangyulong on 16/9/22.
*/
public class MyAdapter extends BaseDragableAdapter<String> {
public MyAdapter(Context context) {
super(context);
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null) {
viewHolder = new ViewHolder();
view = (.adapter_my , null);
 = (TextView) (.tv_item);
(viewHolder);
} else {
viewHolder = (ViewHolder)();
}
if (mHidePosition == i) {
();
} else {
();
}
((i));
return view;
}
class ViewHolder{
TextView mItemTv;
}
}

Use in activity:

package ;
import ;
import .;
import ;
import ;
import ;
public class MainActivity extends Activity {
private DragableGridView mDragableGv;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_main);
mDragableGv = (DragableGridView) findViewById(.drag_grid_view);
MyAdapter adapter = new MyAdapter(this);
List<String> list = new ArrayList<String>();
for (int i = 0 ; i < 30 ; i ++) {
("position" + i);
}
(list);
(adapter);
}
}

The above is the Android implementation of the free dragging effect of item in GridView that the editor introduces to you, and realizes the effect of simulating background data login. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!