按钮触摸监听器阻止自定义scrollview触摸事件监听器

时间:2014-09-24 13:36:02

标签: java android android-button ontouchlistener android-scrollview

我有像这样的自定义对角滚动视图

public class Vscroll extends ScrollView {
    public Vscroll(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public Vscroll(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Vscroll(Context context) {
        super(context);
    }

    public boolean onTouchEvent(MotionEvent ev) {
        return false;
    }
}

public class Hscroll extends HorizontalScrollView {
    public Hscroll(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public Hscroll(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Hscroll(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return false;
    }
}

这是XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.cukil.schoolfrenzy.Vscroll android:layout_height="fill_parent"
        android:scrollbars="none"
        android:layout_width="fill_parent" android:id="@+id/vScroll">
        <com.cukil.schoolfrenzy.Hscroll android:id="@+id/hScroll"
            android:scrollbars="none"
            android:layout_width="fill_parent" android:layout_height="fill_parent">

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/peta_container"
                android:background="@drawable/peta_kota" >

                <ImageView
                    android:id="@+id/bSekolah"
                    android:layout_width="120dp"
                    android:layout_height="100dp"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="114dp"
                    android:layout_marginTop="248dp"
                    android:src="@drawable/tmuka1" />

            </RelativeLayout>

        </com.cukil.schoolfrenzy.Hscroll>
    </com.cukil.schoolfrenzy.Vscroll>

    <RelativeLayout
        android:id="@+id/dashboard"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_alignParentTop="true" >

        <TextView
            android:id="@+id/tNama"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:textColor="#fff"
            android:text="test"
            android:textSize="20sp" />

        <Button
            android:id="@+id/bPause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/b_pause" />

    </RelativeLayout>
</RelativeLayout>

这是触摸事件监听器

public boolean onTouchEvent(MotionEvent event) {
        float curX, curY;
        final ImageView bSekolah = (ImageView) findViewById(R.id.bSekolah);

        switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:
            mx = event.getX();
            my = event.getY();
            break;

        case MotionEvent.ACTION_UP:
            curX = event.getX();
            curY = event.getY();

            if (mx == curX && my == curY) {
                bSekolah.setVisibility(View.VISIBLE);
                bSekolah.callOnClick();

            } else {
                bSekolah.setVisibility(View.INVISIBLE);
            }
            vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            break;
        case MotionEvent.ACTION_MOVE:
            curX = event.getX();
            curY = event.getY();

            vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            isBeingDragged = true;
            mx = curX;
            my = curY;
            break;
        }
        return false;
    }

我想给bSekolah一个点击监听器,当我点击它时,应用程序会进入另一个活动。

但问题是,当我在bSekolah的位置拖动滚动时,滚动视图变得凌乱。 请告诉我问题,帮助:)

0 个答案:

没有答案