长按图标触发拖动事件,可是当图标拖到layout边缘时就拖不出去了。有没有大虾研究过类似问题

解决方案 »

  1.   

    没试过诶
    只做过在root layout上面拖动的,当时root layout设置的是fill parent,所以可以拖到屏幕的外面去,必须控制边界才能保证图标不会跑到屏幕外.
    你这种情况应该是在图标在子layout上面吧,而且这个子layout还不是全屏的,代码里面有做边界控制吗?
      

  2.   

    我是这样设计的,root layout中有两个子layout,layout1和layout2,这两个layout中各有几个元素,现在我想把layout1中的元素拖到layout2中,但是当拖到边界时元素会被遮盖而不是随鼠标继续移动。
      

  3.   

    试着在出边界的时候把view从layout1中删掉,添加到layout2中,但是效果很不好,在换layout的时候会跳,这个估计要自己画layout了...试一下吧,虽然不能用,但是可能是解决的一个思路,我也继续在看看这个问题,挺好的一个问题诶..@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main1); 
            final Button btn = (Button) findViewById(R.id.btn_hello1); 
            final LinearLayout l1 = (LinearLayout)findViewById(R.id.childLayout1);
            final LinearLayout l2 = (LinearLayout)findViewById(R.id.childLayout2);
            
            btn.setOnTouchListener(new OnTouchListener() {
            int[] temp = new int[] { 0, 0 }; 
            boolean b = false;         public boolean onTouch(View v, MotionEvent event) { 
            int eventaction = event.getAction(); 

            int x = (int) event.getRawX(); 
            int y = (int) event.getRawY(); 

            switch (eventaction) { 
            case MotionEvent.ACTION_DOWN: // touch down so check if the 
            temp[0] = (int) event.getX();
            temp[1] = y - v.getTop();
            break; 

            case MotionEvent.ACTION_MOVE: // touch drag with the ball 
             if(y -v.getHeight()> l1.getBottom() && b == false)
             {
             l1.removeView(v);
             l2.addView(v);
             b = true;
             }
             if(b)
             v.layout(x - temp[0], y - temp[1] - l1.getHeight(), x + v.getWidth() 
             - temp[0], y - temp[1] + v.getHeight() - l1.getHeight()); 
             else
             v.layout(x - temp[0], y - temp[1], x + v.getWidth() 
             - temp[0], y - temp[1] + v.getHeight()); 
            

            v.postInvalidate(); 
            break; 

            case MotionEvent.ACTION_UP: 
             break; 
            }          return false; 
            } 
            }); 
        }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:gravity="center"
    android:id="@+id/rootLayout"> <LinearLayout android:layout_width="fill_parent"
       android:id="@+id/childLayout1"
    android:layout_height="200dp" android:background="#FF0000">
    <Button android:id="@+id/btn_hello1" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="button1" />
    </LinearLayout> <LinearLayout android:layout_width="fill_parent"
    android:id="@+id/childLayout2"
    android:layout_height="200dp" android:background="#FFFF00">
    <Button android:id="@+id/btn_hello2" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="button2" />
    </LinearLayout>
    </LinearLayout>
      

  4.   

    想了一下可能还是要自己画才行诶,用canvas里面的东西draw吧
      

  5.   

    这个方法我也试过,处理稍微麻烦一点,不过很谢谢你。你有没有做过分屏显示的?就像launcher里桌面切换那样的,有几个并列的桌面,当手势拖动一个桌面时会带动另一个桌面。桌面切换的动画好解决,就是随手势移动还没实现。这个是不是也要在移动的时候自己画layout?
      

  6.   

    怎么用呢?有没有具体的demo?
      

  7.   


    诶,刚试了一下,你可以这样做,效果不错,
    在KeyDown的时候把这个view从layout1中删掉,加到rootlayout中,在rootlayout里面移动效果就很好了,然后在keyUp的时候判断这个view的位置,如果在layout1中就从rootlayout中删掉,添加到layout1,在layout2中可以做相应的处理,在这些之外还可以做一个动画让它回到以前的位置.效果应该可以达到你的要求
      

  8.   

    恩 可是当layout1的top和left坐标不与rootlayout重叠时会出现问题
      

  9.   

    拖动事件是你自己写的啊,怎么设都可以,比如设置一个boolean变量初始为false表示图标不能被拖动
    onLongClick设置它为ture,在move的时候根据boolean的真假来决定是否拖动,拖动完了以后设成false
      

  10.   

    请问知不知道根据子layout找父layout地方法啊?
      

  11.   

    桌面就是一个很好的例子,父layout要自己定义。
      

  12.   


    在rootView里addView后自己的view位置很奇怪,而且layout也无效,不知有什么办法能很好的定位我加进去的view
      

  13.   

    搞定了,用getRootView,绘制的效果还可以。