长按拖动

添加长按拖动事件

mChannelGrid.setOnItemLongClickListener(new OnItemLongClickListener()
        {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                ClipData.Item item = new ClipData.Item(String.valueOf(arg2));
                ClipData dragData = new ClipData(String.valueOf(arg2), new String[] {"text/plain"}, item);
                ImageView iv = (ImageView)((LinearLayout)mChannelGrid.getChildAt(arg2)).getChildAt(1);
                iv.setDrawingCacheEnabled(true);
                iv.buildDrawingCache();
                DragShadowBuilder myShadow = new MyDragShadowBuilder(iv);
                arg1.startDrag(dragData, // the data to be dragged
                    myShadow, // the drag shadow builder
                    null, // no need to use local data
                    0 // flags (not currently used, set to 0)
                );
                return true;
            }
        });

 

 

public class MainActivity extends Activity
{
   
    GridView mGrid;
   
    private List<infoVo> mApps;
   
   
    int movePosition=-1;
   
    private AppsAdapter mAppsAdapter;
   
    boolean b=false;
   
    private Handler mHandler=new Handler(){

        @Override
        public void handleMessage(Message msg)
        {
            Log.e("loadAppsss", "apps size="+mApps.size()+"--what="+msg.what+"movePosition= "+movePosition);
            //这里更新数据
            mApps.remove(movePosition);
            mAppsAdapter.notifyDataSetChanged();
            super.handleMessage(msg);
        }
       
    };
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
       
        loadApps(); // do this in onresume?
       
        setContentView(R.layout.activity_main);
        mGrid = (GridView)findViewById(R.id.gridview);
        mAppsAdapter=new AppsAdapter();
        mGrid.setAdapter(mAppsAdapter);
       
        mGrid.setOnItemLongClickListener(new OnItemLongClickListener()
        {
           
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
            {
                b=true;
                movePosition=position;
                ClipData.Item item = new ClipData.Item(String.valueOf(position));
                ClipData dragData = new ClipData(String.valueOf(position), new String[] {"text/plain"}, item);
                ImageView iv = (ImageView)mGrid.getChildAt(position);
                iv.setDrawingCacheEnabled(true);
                iv.buildDrawingCache();
                DragShadowBuilder myShadow = new DragShadowBuilder(iv);
                view.startDrag(dragData, // the data to be dragged
                    myShadow, // the drag shadow builder
                    null, // no need to use local data
                    0 // flags (not currently used, set to 0)
                );
                return true;
            }
        });
    }
   
    private void loadApps()
    {
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
       
        infoVo appdinfo;
        mApps=new ArrayList<MainActivity.infoVo>();
        List<ResolveInfo> mmm=getPackageManager().queryIntentActivities(mainIntent, 0);
        for (ResolveInfo r : mmm)
        {
            appdinfo=new infoVo();
            appdinfo.setApp(r);
            mApps.add(appdinfo);
        }
       
        Log.e("loadApps", "apps size="+mApps.size());
       
    }
   
    class infoVo
    {
        int type=0;
       
        ResolveInfo app;

        public int getType()
        {
            return type;
        }

        public void setType(int type)
        {
            this.type = type;
        }

        public ResolveInfo getApp()
        {
            return app;
        }

        public void setApp(ResolveInfo app)
        {
            this.app = app;
        }
       
       
    }
   
    public class AppsAdapter extends BaseAdapter
    {
        public AppsAdapter()
        {
        }
       
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            ImageView i;
            i = new ImageView(MainActivity.this);
            i.setScaleType(ImageView.ScaleType.FIT_CENTER);
            i.setLayoutParams(new GridView.LayoutParams(71, 71));
           
            infoVo appdinfo = mApps.get(position);
            ResolveInfo info = appdinfo.getApp();
            int type = appdinfo.getType();
            if (type == 0)
            {
                i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
            }
            else if (type == 1)
            {
                //换成文件夹图标
                i.setImageDrawable(getResources().getDrawable(R.drawable.commend_icon_pressed));
            }
            else
            {
                return null;
            }
            MyOnDragListener m=new MyOnDragListener(position);
            m.setDraglistenerEnter(new DraglistenerEnter()
            {
               
                @Override
                public void itemCompany(int tag)
                {
                    b = false;
                    //这里进行文件夹合并,生成新的图标
                    mApps.get(tag).setType(1);
                    mHandler.sendEmptyMessage(tag);
                }
            });
            i.setOnDragListener(m);
            return i;
        }
        public final int getCount()
        {
            return mApps.size();
        }
       
        public final Object getItem(int position)
        {
            return mApps.get(position);
        }
       
        public final long getItemId(int position)
        {
            return position;
        }
    }

 

 

这个是到特定的地点的时候触发,把拖动的图标显示在这里

DragEventListener mDragEventListener = new DragEventListener(i, mContext, .this);
            logoImageList[i].setOnDragListener(mDragEventListener);

 

 重写监听事件,触发长按拖动松开等效果

class MyOnDragListenerimplements OnDragListener

 

public int tag = -1;
   
    boolean b=false;
   
    public MyOnDragListener(int tag)
    {
        this.tag=tag;
    }
   
    @Override
    public boolean onDrag(View v, DragEvent event)
    {
        Log.e("onDrag", "getAction="+event.getAction()+"--Tag="+tag);
        switch (event.getAction())
        {
            case DragEvent.ACTION_DRAG_STARTED:
                b=true;
                break;
               
            case DragEvent.ACTION_DRAG_LOCATION:
                //TODO 当放在上面的时候背景图标改变
                break;
            case DragEvent.ACTION_DROP:
                if (mdraglistenerEnter != null&&b)
                {
                    b=false;
                    mdraglistenerEnter.itemCompany(tag);
                }
                break;
            case DragEvent.ACTION_DRAG_ENDED:
               
                break;
            default:
                break;
        }
       
        return true;
    }
   
    private DraglistenerEnter mdraglistenerEnter;
   
    public void setDraglistenerEnter(DraglistenerEnter l)
    {
        mdraglistenerEnter = l;
    }
   
    interface DraglistenerEnter
    {
        void itemCompany(int tag);
    } 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值