如何通过项目的长期preSS在列表视图执行项目的拖放 [英] How to perform Drag and Drop of Item in a List View by Long Press of items

查看:103
本文介绍了如何通过项目的长期preSS在列表视图执行项目的拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我有图像作为项目的清单列表视图。现在我想在列表视图执行的项目拖放。因此,对于我创建了一个类DragNDropListView延伸的ListView ,并覆盖此方法类中的列表视图的onTouch()方法,并执行拖放操作,它的工作原理罚款。

但我的要求是要做到这一点拖放项目滴上都在我的列表视图present图像的长preSS。那么,如何可以做。我应该做同样的事情,因为我在列表视图的onTouch()方法做了或者我应该要修改这个方法。

请帮我理清了这一点。

我的code onTouch法

  @覆盖
公共布尔onTouchEvent(MotionEvent EV)
{
    最终诠释行动= ev.getAction();
    最终诠释X =(INT)ev.getX();
    最终诠释Y =(int)的ev.getY();    如果(动作== MotionEvent.ACTION_DOWN)
    {
        mDragMode = TRUE;
    }    如果(!mDragMode)
        返回super.onTouchEvent(EV);    开关(动作)
    {
        案例MotionEvent.ACTION_DOWN:
        mStartPosition = pointToPosition(X,Y);
        如果(mStartPosition!= INVALID_POSITION)
        {
            INT mItemPosition = mStartPosition - getFirstVisiblePosition();
            mDragPointOffset = Y - getChildAt(mItemPosition).getTop();
            mDragPointOffset - =((INT)ev.getRawY()) - Ÿ;
            的startDrag(mItemPosition,Y);
            拖动(0,y)的;如果需要的话,其中x //替换0
        }
        打破;        案例MotionEvent.ACTION_MOVE:
        {
            拖动(0,y)的;如果需要的话,其中x //替换0
        }
        打破;        案例MotionEvent.ACTION_CANCEL:
        案例MotionEvent.ACTION_UP:        默认:
        mDragMode = FALSE;
        mEndPosition = pointToPosition(X,Y);
        调用stopDrag(mStartPosition - getFirstVisiblePosition());
        如果(mDropListener = NULL&放大器;!&安培;!mStartPosition = INVALID_POSITION&放大器;&安培;!mEndPosition = INVALID_POSITION)
             mDropListener.onDrop(mStartPosition,mEndPosition);
        打破;
    }
    返回true;
}//移动拖动视图
私人无效拖(INT X,int y)对
{
    如果(mDragView!= NULL)
    {
        WindowManager.LayoutParams的LayoutParams =(WindowManager.LayoutParams)mDragView.getLayoutParams();
        layoutParams.x = X;
        layoutParams.y = Y - mDragPointOffset;
        窗口管理mWindowManager =(窗口管理器)的getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        mWindowManager.updateViewLayout(mDragView,的LayoutParams);        如果(mDragListener!= NULL)
            mDragListener.onDrag(X,Y,NULL); //改变空为本,当准备使用
    }
}//启用拖动拖动视图
私人无效的startDrag(INT的ItemIndex,int y)对
{
    调用stopDrag(的ItemIndex);    查看项目= getChildAt(的ItemIndex);
    如果(项目== NULL)回报;
    item.setDrawingCacheEnabled(真);
    如果(mDragListener!= NULL)
        mDragListener.onStartDrag(项目);    //创建绘图缓存的副本,以便它不会得到回收
    //通过在列表试图清理内存架构
    位图的位图= Bitmap.createBitmap(item.getDrawingCache());    WindowManager.LayoutParams mWindowParams =新WindowManager.LayoutParams();
    mWindowParams.gravity = Gravity.TOP;
    mWindowParams.x = 0;
    mWindowParams.y = Y - mDragPointOffset;    mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
    mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
            | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
    mWindowParams.format = PixelFormat.TRANSLUCENT;
    mWindowParams.windowAnimations = 0;    上下文的背景下=的getContext();
    ImageView的V =新ImageView的(上下文);
    v.setImageBitmap(位图);    窗口管理mWindowManager =(窗口管理器)context.getSystemService(Context.WINDOW_SERVICE);
    mWindowManager.addView(ⅴ,mWindowParams);
    mDragView = V;
}//破坏拖曳视图
私人无效调用stopDrag(INT的ItemIndex)
{
    如果(mDragView!= NULL){
        如果(mDragListener!= NULL)
            mDragListener.onStopDrag(getChildAt(的ItemIndex));
        mDragView.setVisibility(GONE);
        窗口管理WM =(窗口管理器)的getContext()getSystemService(Context.WINDOW_SERVICE)。
        wm.removeView(mDragView);
        mDragView.setImageDrawable(NULL);
        mDragView = NULL;
    }
}

使用监听器

  ListView控件的ListView = getListView();    如果(ListView中的instanceof DragNDropListView)
    {
        ((DragNDropListView)的ListView).setDropListener(mDropListener);
        ((DragNDropListView)的ListView).setRemoveListener(mRemoveListener);
        ((DragNDropListView)的ListView).setDragListener(mDragListener);
    }私人DropListener mDropListener =新DropListener()
{
    公共无效onDrop(从int,int值)
    {
        ListAdapter适配器= getListAdapter();
        如果(适配器的instanceof ListViewAdapter)
        {
            ((ListViewAdapter)适配器).onDrop(从,到);
            。getListView()invalidateViews();
        }
    }
};私人的removeListener mRemoveListener =新的removeListener()
{
    公共无效onRemove(INT它)
    {
        ListAdapter适配器= getListAdapter();
        如果(适配器的instanceof ListViewAdapter){
            ((ListViewAdapter)适配器).onRemove(所);
            。getListView()invalidateViews();
        }
    }
};
私人DragListener mDragListener =新DragListener()
{
    公共ondrag当无效(INT X,INT Y,ListView控件ListView控件)
    {
    }    公共无效onStartDrag(查看ItemView控件)
    {
        itemView.setVisibility(View.INVISIBLE);
        ImageView的IV =(ImageView的)itemView.findViewById(R.id.image);
        如果(!IV = NULL)iv.setVisibility(View.VISIBLE);
    }    公共无效onStopDrag(查看ItemView控件)
    {
        itemView.setVisibility(View.VISIBLE);
        ImageView的IV =(ImageView的)itemView.findViewById(R.id.image);
        如果(!IV = NULL)iv.setVisibility(View.VISIBLE);
    }};


解决方案

是的,你可以简单地使用一些监听到你的列表视图 -

 公共接口DropListener {/ **
 *调用当一个产品被丢弃。
 *从@param - 索引项开始在。
 * @参数来 - 指数在放置物品。
 * /
    无效onDrop(从int,int值);
}

尝试看看这个例。

I Have list view in which i have image as an list of item. Now i want to perform drag and drop of an item in the list view. So for that i created an class "DragNDropListView" which extends ListView, and overide the onTouch() method of the list view inside this class and perform drag and drop operation and it works fine.

But my requirement is to do this drag and drop of item on long press of the image which are present in my list view. So how that can be done. Should i do the same thing as i have done in the onTouch() method of the list view or should i have to modify this method.

Please help me to sort this out.

My code onTouch Method

@Override
public boolean onTouchEvent(MotionEvent ev) 
{
    final int action = ev.getAction();
    final int x = (int) ev.getX();
    final int y = (int) ev.getY();  

    if (action == MotionEvent.ACTION_DOWN) 
    {
        mDragMode = true;
    }

    if (!mDragMode) 
        return super.onTouchEvent(ev);

    switch (action) 
    {
        case MotionEvent.ACTION_DOWN:
        mStartPosition = pointToPosition(x,y);
        if (mStartPosition != INVALID_POSITION) 
        {
            int mItemPosition = mStartPosition - getFirstVisiblePosition();
            mDragPointOffset = y - getChildAt(mItemPosition).getTop();
            mDragPointOffset -= ((int)ev.getRawY()) - y;
            startDrag(mItemPosition,y);
            drag(0,y);// replace 0 with x if desired
        }   
        break;

        case MotionEvent.ACTION_MOVE:
        {
            drag(0,y);// replace 0 with x if desired
        }
        break;

        case MotionEvent.ACTION_CANCEL:             
        case MotionEvent.ACTION_UP:

        default:
        mDragMode = false;
        mEndPosition = pointToPosition(x,y);
        stopDrag(mStartPosition - getFirstVisiblePosition());
        if (mDropListener != null && mStartPosition != INVALID_POSITION && mEndPosition != INVALID_POSITION) 
             mDropListener.onDrop(mStartPosition, mEndPosition);
        break;
    }
    return true;
}   

// move the drag view
private void drag(int x, int y) 
{
    if (mDragView != null) 
    {
        WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) mDragView.getLayoutParams();
        layoutParams.x = x;
        layoutParams.y = y - mDragPointOffset;
        WindowManager mWindowManager = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        mWindowManager.updateViewLayout(mDragView, layoutParams);

        if (mDragListener != null)
            mDragListener.onDrag(x, y, null);// change null to "this" when ready to use
    }
}

// enable the drag view for dragging
private void startDrag(int itemIndex, int y) 
{
    stopDrag(itemIndex);

    View item = getChildAt(itemIndex);
    if (item == null) return;
    item.setDrawingCacheEnabled(true);
    if (mDragListener != null)
        mDragListener.onStartDrag(item);

    // Create a copy of the drawing cache so that it does not get recycled
    // by the framework when the list tries to clean up memory
    Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache());

    WindowManager.LayoutParams mWindowParams = new WindowManager.LayoutParams();
    mWindowParams.gravity = Gravity.TOP;
    mWindowParams.x = 0;
    mWindowParams.y = y - mDragPointOffset;

    mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
    mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
            | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
    mWindowParams.format = PixelFormat.TRANSLUCENT;
    mWindowParams.windowAnimations = 0;

    Context context = getContext();
    ImageView v = new ImageView(context);
    v.setImageBitmap(bitmap);      

    WindowManager mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
    mWindowManager.addView(v, mWindowParams);
    mDragView = v;
}

// destroy drag view
private void stopDrag(int itemIndex) 
{
    if (mDragView != null) {
        if (mDragListener != null)
            mDragListener.onStopDrag(getChildAt(itemIndex));
        mDragView.setVisibility(GONE);
        WindowManager wm = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
        wm.removeView(mDragView);
        mDragView.setImageDrawable(null);
        mDragView = null;
    }
}   

Used of Listener

    ListView listView = getListView();

    if (listView instanceof DragNDropListView) 
    {
        ((DragNDropListView) listView).setDropListener(mDropListener);
        ((DragNDropListView) listView).setRemoveListener(mRemoveListener);
        ((DragNDropListView) listView).setDragListener(mDragListener);
    }

private DropListener mDropListener = new DropListener() 
{
    public void onDrop(int from, int to) 
    {
        ListAdapter adapter = getListAdapter();
        if (adapter instanceof ListViewAdapter) 
        {
            ((ListViewAdapter)adapter).onDrop(from, to);
            getListView().invalidateViews();
        }
    }
};

private RemoveListener mRemoveListener = new RemoveListener() 
{
    public void onRemove(int which) 
    {
        ListAdapter adapter = getListAdapter();
        if (adapter instanceof ListViewAdapter) {
            ((ListViewAdapter)adapter).onRemove(which);
            getListView().invalidateViews();
        }
    }
};


private DragListener mDragListener = new DragListener() 
{
    public void onDrag(int x, int y, ListView listView)
    {
    }

    public void onStartDrag(View itemView) 
    {
        itemView.setVisibility(View.INVISIBLE);
        ImageView iv = (ImageView)itemView.findViewById(R.id.image);
        if (iv != null) iv.setVisibility(View.VISIBLE);
    }

    public void onStopDrag(View itemView) 
    {
        itemView.setVisibility(View.VISIBLE);
        ImageView iv = (ImageView)itemView.findViewById(R.id.image);
        if (iv != null) iv.setVisibility(View.VISIBLE);
    }

};

解决方案

Yes, you can simply use some listener to your listview -

public interface DropListener {

/**
 * Called when an item is to be dropped.
 * @param from - index item started at.
 * @param to - index to place item at.
 */
    void onDrop(int from, int to);
}

Try to see this Example.

这篇关于如何通过项目的长期preSS在列表视图执行项目的拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆