在Android中使用多种布局拖放自动滚动

在Android中使用多种布局拖放自动滚动,第1张

概述“当我拖放具有多种布局的图像时,自动滚动效果不佳”我到处搜索,但找不到解决方案.我从这个链接得到了一个解决方案:MakeascrollViewautoscrollwithdraganddropinAndroid@thehayro感谢您提供了一个很好的例子.但它仅适用于一种布局,自动滚动也适用.但是我在一个线性布

“当我拖放具有多种布局的图像时,自动滚动效果不佳”

我到处搜索,但找不到解决方案.
我从这个链接得到了一个解决方案:

Make a scrollView autoscroll with drag and drop in Android

@thehayro感谢您提供了一个很好的例子.

但它仅适用于一种布局,自动滚动也适用.但是我在一个线性布局中有4-5个以上的子布局,并且此布局在滚动视图中,我的布局文件如下所示:

<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不知道如何解决它.我已经尝试过但是我可以做到.我不知道,我的代码是:

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;@OverrIDeprotected 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

public class MyScrollVIEw  extends ScrollVIEw {public OnScrollVIEwListener mListener;public MyScrollVIEw(Context context, AttributeSet attrs) {    super(context, attrs);    // Todo auto-generated constructor stub}@OverrIDeprotected 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新手,所以不太了解,请告诉我如何解决.
对不起,我的英语和语法错误.
在此先感谢

解决方法:

大家好我自己解决了这个问题

               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中使用多种布局拖放自动滚动全部内容,希望文章能够帮你解决在Android中使用多种布局拖放自动滚动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1086823.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存