天天看點

【Android】HorizontalScrollView内子控件橫向拖拽

前言

網上listview上下拖動的例子有,效果也很好,但是項目要橫着拖的,隻要硬着頭皮自己寫(主要是沒找到合适的),參考文章1修改而來,分享一下。

聲明

歡迎轉載,但請保留文章原始出處:) 

部落格園:http://www.cnblogs.com

農民伯伯: http://over140.cnblogs.com

正文

截圖

【Android】HorizontalScrollView内子控件橫向拖拽

代碼

java

【Android】HorizontalScrollView内子控件橫向拖拽

public class hodragactivity extends activity {

    private linearlayout main;

    private gesturedetector mgesturedetector;

    @override

    public void oncreate(bundle savedinstancestate) {

        super.oncreate(savedinstancestate);

        setcontentview(r.layout.main_cc);

        main = (linearlayout) findviewbyid(r.id.main);

        binddraplistener(r.id.myimage1);

        binddraplistener(r.id.myimage2);

        binddraplistener(r.id.myimage3);

        binddraplistener(r.id.myimage4);

        binddraplistener(r.id.myimage5);

        binddraplistener(r.id.myimage6);

        binddraplistener(r.id.myimage7);

        binddraplistener(r.id.myimage8);

        binddraplistener(r.id.myimage9);

        binddraplistener(r.id.myimage10);

        binddraplistener(r.id.myimage11);

        binddraplistener(r.id.myimage12);

        mgesturedetector = new gesturedetector(this, new drapgesturelistener());

    }

    private view mdrapview;

    private void binddraplistener(int id) {

        view v = findviewbyid(id);

        v.setontouchlistener(montouchlistener);

        v.setondraglistener(mondraglistener);

    private ontouchlistener montouchlistener = new ontouchlistener() {

        @override

        public boolean ontouch(view v, motionevent event) {

            mdrapview = v;

            if (mgesturedetector.ontouchevent(event))

                return true;

            switch (event.getaction() & motionevent.action_mask) {

            case motionevent.action_up:

                break;

            }

            return false;

        }

    };

    private ondraglistener mondraglistener = new ondraglistener() {

        public boolean ondrag(view v, dragevent event) {

            switch (event.getaction()) {

            case dragevent.action_drag_started:

                // do nothing

            case dragevent.action_drag_entered:

                v.setalpha(0.5f);

            case dragevent.action_drag_exited:

                v.setalpha(1f);

            case dragevent.action_drop:

                view view = (view) event.getlocalstate();

                for (int i = 0, j = main.getchildcount(); i < j; i++) {

                    if (main.getchildat(i) == v) {

                        // 目前位置

                        main.removeview(view);

                        main.addview(view, i);

                        break;

                    }

                }

            case dragevent.action_drag_ended:

            default:

            return true;

    private class drapgesturelistener extends simpleongesturelistener {

        public boolean onsingletapconfirmed(motionevent e) {

            return super.onsingletapconfirmed(e);

        public void onlongpress(motionevent e) {

            super.onlongpress(e);

            clipdata data = clipdata.newplaintext("", "");

            mydragshadowbuilder shadowbuilder = new mydragshadowbuilder(

                    mdrapview);

            mdrapview.startdrag(data, shadowbuilder, mdrapview, 0);

        public boolean ondown(motionevent e) {

    private class mydragshadowbuilder extends view.dragshadowbuilder {

        private final weakreference<view> mview;

        public mydragshadowbuilder(view view) {

            super(view);

            mview = new weakreference<view>(view);

        public void ondrawshadow(canvas canvas) {

            canvas.scale(1.5f, 1.5f);

            super.ondrawshadow(canvas);

        public void onprovideshadowmetrics(point shadowsize,

                point shadowtouchpoint) {

            // super.onprovideshadowmetrics(shadowsize, shadowtouchpoint);

            final view view = mview.get();

            if (view != null) {

                shadowsize.set((int) (view.getwidth() * 1.5f),

                        (int) (view.getheight() * 1.5f));

                shadowtouchpoint.set(shadowsize.x / 2, shadowsize.y / 2);

            } else {

                // log.e(view.view_log_tag,

                // "asked for drag thumb metrics but no view");

}

【Android】HorizontalScrollView内子控件橫向拖拽

代碼說明

1、 mydragshadowbuilder主要用于縮放拖拽的imageview,這裡放大了1.5倍,而又不至于影響原控件,這裡是放大兩倍

2、這裡是通過長按來喚起拖拽操作,是以監聽了onlongpress

3、注意拖拽ondraglistener是android 3.0才新增的api

文章

下載下傳

結束

歡迎交流!想做成拖動插入時自動兩邊擠開的效果,沒弄出來,出來是抖動的效果。 

轉載:http://www.cnblogs.com/over140/p/3599319.html

繼續閱讀