Android Open Source - DragListView Drag List View






From Project

Back to project page DragListView.

License

The source code is released under:

Apache License

If you think the Android project DragListView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.czl.struct.widget;
//from w w w  .j a v  a 2 s.c  o m
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.czl.struct.interfaces.DragItemChangeListener;

public class DragListView extends ListView {

    private WindowManager              windowManager;         // windows?????????   

    private WindowManager.LayoutParams windowParams;          // ?????????????????????     

    private ImageView                  dragImageView;         // ?????????(item)????????????ImageView   

    private int                        dragSrcPosition;       // ?????????????????????   

    private int                        dragPosition;          // ?????????????????,???????????????????.   

    private int                        dragPoint;             // ????????????????   

    private int                        dragOffset;            // ????????????????(??????????y??????)   

    private int                        upScrollBounce;        // ?????????????????????   

    private int                        downScrollBounce;      // ????????????????????   

    private final static int           step = 1;              // ListView ????????.   

    private int                        current_Step;          // ???????.   

    private int                        dragImageSourceId;

    private DragItemChangeListener     dragItemChangeListener;

    /*** 
     * ?????? 
     *  
     * @param context 
     * @param attrs 
     */
    public DragListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setDragImageSourceId(int dragImageSourceId) {
        this.dragImageSourceId = dragImageSourceId;
    }

    public void setDragItemChangeListener(DragItemChangeListener dragItemChangeListener) {
        this.dragItemChangeListener = dragItemChangeListener;
    }

    /*** 
     * touch?????? 
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // ????   
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            int x = (int) ev.getX();// ????????ListView??x????   
            int y = (int) ev.getY();// ????????ListView??y????   
            dragSrcPosition = dragPosition = pointToPosition(x, y);
            // ????????????   
            if (dragPosition == AdapterView.INVALID_POSITION) {
                return super.onInterceptTouchEvent(ev);
            }

            // ????????????(?????)   
            ViewGroup itemView = (ViewGroup) getChildAt(dragPosition - getFirstVisiblePosition());

            // ???????dragPoint????????????????item???????.   
            dragPoint = y - itemView.getTop();
            // ??????????:???????ListView??????????????????????????????+??????.   
            dragOffset = (int) (ev.getRawY() - y);

            // ?????????????   
            View dragger = itemView.findViewById(dragImageSourceId);

            // x > dragger.getLeft() - 20??????????????-20????????   
            if (dragger != null && x > dragger.getLeft() - 20) {

                upScrollBounce = getHeight() / 3;// ????????????????????????1/3   
                downScrollBounce = getHeight() * 2 / 3;// ???????????????????????2/3   

                itemView.setDrawingCacheEnabled(true);// ????cache.   
                Bitmap bm = Bitmap.createBitmap(itemView.getDrawingCache());// ????cache?????????bitmap????.   
                startDrag(bm, y);// ????????   
            }
        }

        return super.onInterceptTouchEvent(ev);
    }

    /** 
     * ?????????? 
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // item??view????????????dragPosition???   
        if (dragImageView != null && dragPosition != INVALID_POSITION) {
            int action = ev.getAction();
            switch (action) {
            case MotionEvent.ACTION_UP:
                int upY = (int) ev.getY();
                stopDrag();
                onDrop(upY);
                break;
            case MotionEvent.ACTION_MOVE:
                int moveY = (int) ev.getY();
                onDrag(moveY);
                break;
            case MotionEvent.ACTION_DOWN:
                break;
            default:
                break;
            }
            return true;// ???ListView????.   
        }

        return super.onTouchEvent(ev);
    }

    /** 
     * ?????????????????????? 
     *  
     * @param bm 
     * @param y 
     */
    private void startDrag(Bitmap bm, int y) {
        // stopDrag();   
        /*** 
         * ?????window. 
         */
        windowParams = new WindowManager.LayoutParams();
        windowParams.gravity = Gravity.TOP;
        windowParams.x = 0;
        windowParams.y = y - dragPoint + dragOffset;
        windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE// ??????????   
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE// ????????????   
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON// ?????????????????????????   
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;// ???????????????????????????????????????????????????????????????   

        windowParams.format = PixelFormat.TRANSLUCENT;// ????????????????????????.   
        windowParams.windowAnimations = 0;// ????????????????   

        ImageView imageView = new ImageView(getContext());
        imageView.setImageBitmap(bm);
        windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        windowManager.addView(imageView, windowParams);
        dragImageView = imageView;

    }

    /** 
     * ?????????Move????????? 
     *  
     * @param y 
     */
    public void onDrag(int y) {
        int drag_top = y - dragPoint;// ???view??top??????0???????????.   
        if (dragImageView != null && drag_top >= 0) {
            windowParams.alpha = 0.5f;// ?????   
            windowParams.y = y - dragPoint + dragOffset;// ???y?.//????????dragOffset??windowManager??????????????.(???????????????????)   
            windowManager.updateViewLayout(dragImageView, windowParams);// ?????.   
        }
        // ??????????????????????-1??????   
        int tempPosition = pointToPosition(0, y);
        if (tempPosition != INVALID_POSITION) {
            dragPosition = tempPosition;

        }
        doScroller(y);
    }

    /*** 
     * ListView?????. 
     * ???????????????????????????ListView??????????????????????????ListView???????????????????. 
     *  
     */

    public void doScroller(int y) {
        // ListView?????   
        if (y < upScrollBounce) {
            current_Step = step + (upScrollBounce - y) / 10;// ??????   
        }// ListView??????   
        else if (y > downScrollBounce) {
            current_Step = -(step + (y - downScrollBounce)) / 10;// ??????   
        } else {
            current_Step = 0;
        }

        // ????????????????????item?????view??????????????????position??   
        View view = getChildAt(dragPosition - getFirstVisiblePosition());
        // ?????????????setSelectionFromTop()   
        setSelectionFromTop(dragPosition, view.getTop() + current_Step);

    }

    /** 
     * ????????????? 
     */
    public void stopDrag() {
        if (dragImageView != null) {
            windowManager.removeView(dragImageView);
            dragImageView = null;
        }
    }

    /** 
     * ?????????? 
     *  
     * @param y 
     */
    public void onDrop(int y) {

        // ??????????????????????-1??????   
        int tempPosition = pointToPosition(0, y);
        if (tempPosition != INVALID_POSITION) {
            dragPosition = tempPosition;
        }

        // ?????????(???????????????Top?????????????????????)   
        if (y < getChildAt(0).getTop()) {
            // ????????   
            dragPosition = 0;
            // ?????????????????????????????????   
        } else if (y > getChildAt(getChildCount() - 1).getBottom()) {
            // ???????   
            dragPosition = getAdapter().getCount() - 1;
        }

        // ???????   
        if (dragPosition < getAdapter().getCount()) {

            dragItemChangeListener.onDragItemChange(dragSrcPosition, dragPosition);
        }

    }

}




Java Source Code List

com.czl.struct.activity.BaseActivity.java
com.czl.struct.activity.MainActivity.java
com.czl.struct.adapter.DemoAdapter.java
com.czl.struct.interfaces.DragItemChangeListener.java
com.czl.struct.widget.DragListView.java