1

ScrollViewでラップされたHorizo​​ntalScrollViewにカスタムビューがあり、タッチポイントが移動されたがスクロールが行われなかった場合、つまりビューがウィンドウサイズ以下の場合、onTouchEventのACTION_UPを操作したいと思います。移動せずにワンタッチすると、本来のACTION_UPが得られます。

2.1で(次の)コードを実行すると、期待どおりにACTION_UPが取得されます。2.2で同じコードを実行すると、ACTION_UPが取得されません(2.2エミュレーターでSDK 2.1と2.2の両方でテストしました)。

2.1バージョンで誤って「動作するバグ」を使用して2.2で修正されたことがありますか、それともこれは新しいエラーですか?回避策はありますか?

次のlogcatを参照してください。

2.1
I/CustomActivityView(  225): ACTION_DOWN at 307.000000,
I/CustomActivityView(  225): ACTION_MOVE at 297.000000,
I/CustomActivityView(  225): ACTION_MOVE at 292.000000,
I/CustomActivityView(  225): ACTION_MOVE at 290.000000,
I/CustomActivityView(  225): ACTION_MOVE at 289.000000,
I/CustomActivityView(  225): ACTION_MOVE at 286.000000,
I/CustomActivityView(  225): ACTION_UP at 286.000000,

2.2
/CustomActivityView(  279): ACTION_DOWN at 249.000000,
/CustomActivityView(  279): ACTION_MOVE at 249.000000,
/CustomActivityView(  279): ACTION_MOVE at 249.000000,
/CustomActivityView(  279): ACTION_MOVE at 249.000000,
/CustomActivityView(  279): ACTION_DOWN at 198.000000,
/CustomActivityView(  279): ACTION_MOVE at 199.000000,
/CustomActivityView(  279): ACTION_MOVE at 207.000000,
/CustomActivityView(  279): ACTION_MOVE at 214.000000,
/CustomActivityView(  279): ACTION_MOVE at 221.000000,

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#ffdddddd"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ScrollView android:id="@+id/ScrollView" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <HorizontalScrollView android:id="@+id/HorizontalScrollView" android:layout_width="wrap_content" android:layout_height="wrap_content">
            <com.example.eventconsumption.CustomActivityView
            android:background="#ffaaaaff"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/CustomActivityView"
            />
        </HorizontalScrollView>
    </ScrollView>
</LinearLayout>

景色:

package com.example.eventconsumption;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

public class CustomActivityView extends View {
    private static final String TAG = "CustomActivityView";
    private int mSetViewWidth = -1;
    private int mSetViewHeight = -1;
    private int mScreenWidth;
    private int mScreenHeight;

    public CustomActivityView(Context context) {
        super(context);
        init(context);
    }

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

    public CustomActivityView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        mScreenWidth = display.getWidth();
        mScreenHeight = display.getHeight();

        setWidthHeight(mScreenWidth / 2, mScreenHeight * 2);
        //setWidthHeight(mScreenWidth * 2, mScreenHeight / 2);
    }

    private void setWidthHeight(int w, int h) {
        mSetViewWidth = w;
        mSetViewHeight = h;
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mSetViewWidth != -1 && mSetViewHeight != -1) {
            setMeasuredDimension(mSetViewWidth, mSetViewHeight);
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.i(TAG, String.format("ACTION_DOWN at %f, %f", event.getX(), event.getY()));
            return true;
        case MotionEvent.ACTION_MOVE:
            Log.i(TAG, String.format("ACTION_MOVE at %f, %f", event.getX(), event.getY()));
            break;
        case MotionEvent.ACTION_UP:
            Log.i(TAG, String.format("ACTION_UP at %f, %f", event.getX(), event.getY()));
            return true;
        }

        return super.onTouchEvent(event);
    }
}

アクティビティ:

package com.example.eventconsumption;

import android.app.Activity;
import android.os.Bundle;

public class EventConsumptionTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
4

1 に答える 1

1

さて、私の回避策は、Androidバージョンがeclairより上にある場合はOnTouchListenerを使用し、それ以外の場合は古いコードを使用することです。登録時

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ECLAIR_MR1) {
    mUseOnTouchListener = true;
    mHorizontalScrollView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // ACTION_UP check here etc...

古いコード

case MotionEvent.ACTION_UP:
    if (!mUseOnTouchListener) {

等々...

于 2010-11-05T07:12:21.410 に答える