拖放自动滚动在Android的多个布局 [英] drag and drop auto scroll with multiple layout in android

查看:156
本文介绍了拖放自动滚动在Android的多个布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自动滚动不工作得很好,当我拖动并与多个布局删除图像

"auto scroll not working very well when i drag and drop the image with multiple layout"

我找遍了,也没有找到一个解决方案。 我从这个链接的解决方案:

I searched all over, but could not find a solution. i got a solution from this link:

<一个href="http://stackoverflow.com/questions/7352236/make-a-scrollview-autoscroll-with-drag-and-drop-in-android/7366321#7366321">Make滚动型自动滚屏拖放在Android中

@thehayro感谢这样一个很好的例子。

@thehayro thanks for such a nice example.

但它的工作只有一个布局和自动翻滚还曾。但我有一个线性布局超过4-5子布局和该布局是在滚动查看我的布局文件是这样的:

but it is working for only one layout and auto scroll is also worked. but i have more than 4-5 child layout in one linear layout and this layout is in the scroll view my layout file is like:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.dragvdropdemo.MyScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/scroll_view">
<LinearLayout 
 android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/lldrag">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="200dip" 
    android:id="@+id/ll1"
    android:layout_marginTop="10dip"
     android:background="@android:color/darker_gray">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/dr_logo" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/fb" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="200dip" 
    android:id="@+id/ll2"
    android:layout_marginTop="10dip"
    android:background="@android:color/darker_gray">


    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/twitter" />

    <ImageView
        android:id="@+id/ImageView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/ImageView03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="200dip" 
    android:id="@+id/ll3"
    android:layout_marginTop="10dip"
     android:background="@android:color/darker_gray">


    <ImageView
        android:id="@+id/ImageView1_l3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/twitter" />

    <ImageView
        android:id="@+id/ImageView2_l3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/ImageView3_l3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="200dip" 
    android:id="@+id/ll4"
    android:layout_marginTop="10dip"
     android:background="@android:color/darker_gray">


    <ImageView
        android:id="@+id/ImageView1_l4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/twitter" />

    <ImageView
        android:id="@+id/ImageView2_l4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/ImageView3_l4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
 </com.example.dragvdropdemo.MyScrollView>

我想拖,并从一个到另一个layout.i下降形象不知道如何解决它。我已经尝试过,但我可以做it.i'm不知道veryand我的code是:

i want to drag and drop image from one to another layout.i don't know how to solve it. i have tried but i could do it.i'm don't know veryand my code is:

public class MyActivity extends Activity {


ImageView   img1,img2,img3,img01;
ImageView img02,img03,img1_l3,img2_l3,img3_l3,img1_l4,img2_l4,img3_l4;
LinearLayout ll1,ll2,ll3,ll4,lldrag;

MyScrollView myScrollView;
int mScrollDistance;

private static int oldvalue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylist);

    //SCROLLVIEW
       myScrollView = (MyScrollView) findViewById(R.id.scroll_view);
         myScrollView.setOnScrollViewListener(new         MyScrollView.OnScrollViewListener() {

            @Override
            public void onScrollChanged1(OnScrollViewListener listener) {
                // TODO Auto-generated method stub
                mScrollDistance = myScrollView.getScrollY();
            }
        });

    img1 = (ImageView)findViewById(R.id.imageView1);
    img2 = (ImageView)findViewById(R.id.imageView2);
    img3 = (ImageView)findViewById(R.id.imageView3);
    img01 = (ImageView)findViewById(R.id.ImageView01);
    img02 = (ImageView)findViewById(R.id.ImageView02);
    img03 = (ImageView)findViewById(R.id.ImageView03);
    img1_l3 = (ImageView)findViewById(R.id.ImageView1_l3);
    img2_l3 = (ImageView)findViewById(R.id.ImageView2_l3);
    img3_l3 = (ImageView)findViewById(R.id.ImageView3_l3);
    img1_l4 = (ImageView)findViewById(R.id.ImageView1_l4);
    img2_l4 = (ImageView)findViewById(R.id.ImageView2_l4);
    img3_l4 = (ImageView)findViewById(R.id.ImageView3_l4);
    ll1 = (LinearLayout)findViewById(R.id.ll1);
    ll2 = (LinearLayout)findViewById(R.id.ll2);
    ll3 = (LinearLayout)findViewById(R.id.ll3);
    ll4 = (LinearLayout)findViewById(R.id.ll4);
    lldrag = (LinearLayout)findViewById(R.id.lldrag);

    img1.setOnTouchListener(new ChoiceTouchListener());
    img2.setOnTouchListener(new ChoiceTouchListener());
    img3.setOnTouchListener(new ChoiceTouchListener());
    img01.setOnTouchListener(new ChoiceTouchListener());
    img02.setOnTouchListener(new ChoiceTouchListener());
    img03.setOnTouchListener(new ChoiceTouchListener());
    img1_l3.setOnTouchListener(new ChoiceTouchListener());
    img2_l3.setOnTouchListener(new ChoiceTouchListener());
    img3_l3.setOnTouchListener(new ChoiceTouchListener());
    img1_l4.setOnTouchListener(new ChoiceTouchListener());
    img2_l4.setOnTouchListener(new ChoiceTouchListener());
    img3_l4.setOnTouchListener(new ChoiceTouchListener());

    ll1.setOnDragListener(new ChoiceDragListener());
    ll2.setOnDragListener(new ChoiceDragListener());
    ll3.setOnDragListener(new ChoiceDragListener());
    ll4.setOnDragListener(new ChoiceDragListener());
    lldrag.setOnDragListener(new ChoiceDragListener());


}

private final class ChoiceTouchListener implements OnTouchListener {

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        // TODO Auto-generated method stub


        ClipData data=ClipData.newPlainText("", "");
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        //start dragging the item touched''
        view.startDrag(data, shadowBuilder, view, 0);
        return true;
    } 
} 

/**
 * DragListener will handle dragged views being dropped on the drop area
 * - only the drop action will have processing added to it as we are not
 * - amending the default behavior for other parts of the drag process
 *
 */
class ChoiceDragListener implements OnDragListener {



    @Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            //no action necessary
            Log.i("", "DRAGSTARTED");
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            //no action necessary
            Log.i("","DRAGENTERED");
            break;
        case DragEvent.ACTION_DRAG_EXITED:        
            //no action necessary
            Log.i("","DRAGEXITED");
            break;
        case DragEvent.ACTION_DROP:
            //handle the dragged view being dropped over a drop view
            Log.i("","DROP");
            View view = (View) event.getLocalState();

            LinearLayout ll=(LinearLayout) view.getParent();
            ll.removeView(view);
            LinearLayout dropTarget = (LinearLayout) v;
            ImageView dropped = (ImageView) view;

            dropTarget.addView(dropped,0);

            Object tag = dropTarget.getTag();

            if(tag!=null)
            {

                int existingID = (Integer)tag;

                findViewById(existingID).setVisibility(View.VISIBLE);
            }
            //set the tag in the target view being dropped on - to the ID of the view being dropped
            dropTarget.setTag(dropped.getId());
            break;

        case DragEvent.ACTION_DRAG_LOCATION:
            //no action necessary
            int y = Math.round(event.getY());
              int translatedY = y - mScrollDistance;
              Log.i("translated",""+translatedY+" "+ mScrollDistance+" "+y);
              int threshold =50 ;
              // make a scrolling up due the y has passed the threshold
              if (translatedY < threshold) {
                 // make a scroll up by 30 px
                 myScrollView.scrollBy(0, -30);
              }
              // make a autoscrolling down due y has passed the 500 px border
              if (translatedY + threshold > 200) {
                 // make a scroll down by 30 px
                 myScrollView.scrollBy(0, 30);
              }
            break;

        case DragEvent.ACTION_DRAG_ENDED:
            //no action necessary
            break;
        default:
            break;
        }
        return true;
    }

} 
}

MySrollView

MySrollView

public class MyScrollView  extends ScrollView {

public OnScrollViewListener mListener;

public MyScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    // TODO Auto-generated method stub
    super.onScrollChanged(l, t, oldl, oldt);
    if (mListener != null) {
         mListener.onScrollChanged1(mListener);
     }
}

public void setOnScrollViewListener(OnScrollViewListener listener) {
    mListener = listener;
}
public static interface OnScrollViewListener {
       public void onScrollChanged1(OnScrollViewListener listener);
    }
}

我是android的初学者所以不知道它很舒服,所以告诉我怎么解决这个问题。 对不起,我的英文不好和语法错误。 感谢进步

i'm android beginner so don't know it very well so tell me how to solve it. sorry for my bad english and grammer mistake. thanks in advances

推荐答案

嘿家伙,我已经解决了这个问题,通过myself.Here是解决方案...

Hey guys i have solved this problem by myself.Here is the solution...

               DragEvent.ACTION_DRAG_LOCATION:
        //no action necessary
          int y = Math.round(v.getY())+Math.round(event.getY());
              int translatedY = y - mScrollDistance;
              Log.i("translated",""+translatedY+" "+ mScrollDistance+" "+y);
              int threshold =50 ;
              // make a scrolling up due the y has passed the threshold
              if (translatedY < 200) {
                 // make a scroll up by 30 px
                 myScrollView.smoothScrollBy(0, -15);
              }
              // make a autoscrolling down due y has passed the 500 px border
              if (translatedY + threshold > 500) {
                 // make a scroll down by 30 px
                 myScrollView.smoothScrollBy(0, 15);
              }

            break;

这篇关于拖放自动滚动在Android的多个布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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