gpt4 book ai didi

Android 2.2 使用 ACTION_UP 而 2.1 不使用(在嵌套 ScrollView 中)。我使用的是旧错误吗?

转载 作者:行者123 更新时间:2023-11-30 04:48:53 26 4
gpt4 key购买 nike

我在 Horizo​​ntalScrollView 中有自定义 View ,包裹在 ScrollView 中,当触摸点已移动但未完成滚动时,我想在 onTouchEvent 中对 ACTION_UP 进行操作,即当 View 小于或等于窗口大小时.不移动的单次触摸会按应有的方式提供 ACTION_UP。

在 2.1 下运行(以下)代码时,我得到了预期的 ACTION_UP。在 2.2 上运行相同的代码时,我没有得到 ACTION_UP(我已经在 2.2 模拟器上使用 SDK 2.1 和 2.2 进行了测试)。

我是否在 2.1 版本中不小心使用了一个“有效的错误”并且已在 2.2 版本中得到修复,或者这是一个新错误?有解决方法吗?

请参阅以下日志:

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,

主.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);
}
}

Activity :

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);
}
}

最佳答案

好的,所以我的解决方法是,如果 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) {

等等……

关于Android 2.2 使用 ACTION_UP 而 2.1 不使用(在嵌套 ScrollView 中)。我使用的是旧错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4096623/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com