我想将GestureDetector类添加到我的webview中,以便我可以管理常见的手势,例如doubletap.
这是我试图完成的事情:
关于DoubleTap - 有问题的网站有源代码,用于定义如何处理某些项目的单击和双击.它声明单击一次选择一个项目,双击打开该项目.我必须对这些项目含糊不清,因为它不是公共网站.
我不需要配置flings或swipes.我只想重新创建PC鼠标单击和双击.
编辑: 我在Pat的下面的建议中添加了.我根据自己的喜好改变了一些东西,并从其他来源添加了一些东西.
我使用这个问题来帮助构建我的活动以及Pat的帮助.使用"First Way"代码,它正确加载我的应用程序并成功重新加载页面.现在,我只需要添加其他事件处理程序并正确配置它们.当我开始工作时会发布最终代码,我会将Pat的答案标记为正确.
该用户提出相同的问题,没有回答: 如何使双击工作.
它说这是折旧的GestureDetector(新的MyGestureDetector()); - 我应该解决这个问题吗?
编辑2:由于问的实际问题是如何将webdetecor添加到webview,我能够做到这一点,我会将Pat的答案标记为正确.即使我确实想要doubleTap =双击,我也没有在我的标题中问这个,所以我会为此创建一个新问题.
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.SslErrorHandler;
import android.net.http.SslError;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
public class MainActivity extends Activity{
WebView webview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.webview);
//Do we want/need to enable Java?
webview.getSettings().setJavaScriptEnabled(true);
//Here we allow for zoom controls - …Run Code Online (Sandbox Code Playgroud) 我有一个用 ContentProvider 填充的ListFragment 。
我需要为每个项目附加一个手势侦听器,以便当用户向右滑动时,该项目将从列表中删除。
以下是我目前 ListFragment 中的所有内容。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
String selection = l.getItemAtPosition(position).toString();
Toast.makeText(v.getContext(), selection + "", Toast.LENGTH_LONG).show();
Log.d("TodoListFragment", selection+ "");
}
Run Code Online (Sandbox Code Playgroud)
我如何附加手势检测和监听?
我做了一些研究,发现了这段代码:
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { …Run Code Online (Sandbox Code Playgroud) 我定义了一个类来扩展GestureDetector.SimpleOnGestureListener和重写onDown和onFling方法
class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return super.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
Toast.makeText(PersianDatePicker.this.getContext(),
"fling " + velocityX + " - " + velocityY, Toast.LENGTH_LONG)
.show();
return super.onFling(e1, e2, velocityX, velocityY);
}
}
Run Code Online (Sandbox Code Playgroud)
我还定义了一个实例GestureDetectorCompat
@Override
public boolean onTouchEvent(MotionEvent event) {
mDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
Run Code Online (Sandbox Code Playgroud)
我在一个活动中测试了它,效果很好。我想在自定义视图中使用它,但它不起作用!而且我没有错误。
我想在滚动事件发生时实现 onScroll。但是,我不明白如何使用通过 onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) 接收到的参数自下而上检测滚动。
我很乐意得到一些如何实施它的指导方针或一些例子。
我的代码是:
GestureDetector(
child: Text('text'),
onLongPress: () {
print('++++++++++++++++++++++++');
},
onVerticalDragUpdate: (updateDetails) {
print('******************************************');
},
onHorizontalDragUpdate: (updateDetails){
print('-----------------------------------');
},
),
Run Code Online (Sandbox Code Playgroud)
当我触发onLongPress事件时,onVerticalDragUpdate事件不会被触发。为什么?
不知何故,我得到了相同的值,我无法理解颤振中全局位置和局部位置之间的区别。
RenderBox getBox = context.findRenderObject();
Offset position = getBox.localToGlobal(Offset.zero);
var local = getBox.globalToLocal(update.globalPosition);
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用堆栈创建一个多层屏幕,该堆栈允许用户与前台和后台小部件进行交互。
我已经开始绘制屏幕的基本轮廓,但是即使使用HitTestBehavior.translucent控制台,也仅red pressed在按下半透明红色容器时打印。我希望控制台在按下红色/紫色容器时打印两个字符串。
下面是我使用的代码。
@override
Widget build(BuildContext context) {
final children = <Widget>[
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
debugPrint('blue pressed');
},
child: Container(
height: 500,
width: MediaQuery.of(context).size.width,
color: Colors.blue,
),
),
];
if (exercise.instructions.isNotEmpty) {
children.add(
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
debugPrint('red pressed');
},
child: Container(
height: 400,
width: MediaQuery.of(context).size.width,
color: Colors.red.withAlpha(100),
),
),
);
}
return Stack(
alignment: Alignment.bottomCenter,
children: children,
);
}
Run Code Online (Sandbox Code Playgroud)
下面是制作出来的画面:
我正在创建一个带有颤动的绘图画布,我想同时使用缩放(用于缩放、平移和旋转)和平移(用于用手指绘图)手势,但颤动不允许这样做,因为缩放是平移的超集。有什么解决方法吗?
我尝试创建自己的自定义手势识别器来检测屏幕上的指针数量,以便如果两个指针在很短的时间间隔内(假设为 1 秒)与屏幕接触,则 Scale 接管,否则 pan 开始使用第一个触摸屏幕的指针。我正在使用matrix_gesture_detector包来做缩放部分。
这是我的自定义手势识别器代码
class ScaleAndPanRecognizer extends OneSequenceGestureRecognizer {
var manager = GestureArenaManager();
final Function onPanDown;
final Function onPanUpdate;
final Function onPanEnd;
ScaleAndPanRecognizer({
@required this.onPanDown,
@required this.onPanUpdate,
@required this.onPanEnd,
});
int numPointers = 0;
int timeFrame = 1;
int pointer1;
int pointer2;
var time1;
var time2;
var position1;
@override
void addPointer(PointerDownEvent event) {
print(numPointers);
if (numPointers == 0) {
pointer1 = event.pointer;
print(pointer1);
position1 = event.localPosition;
manager.hold(pointer1); //hold the assignment for this pointer …Run Code Online (Sandbox Code Playgroud) 我想创建一个旋转动画,其中包含一组按钮(排列成圆圈),这些按钮从单击的中心按钮旋转和延伸。
虽然动画和旋转工作良好,并且打开按钮和关闭按钮工作顺利并且尽管旋转但仍响应 onTap(),但外部按钮在“onTap”-GestureDetector 方面不起作用。
目标:我想让红色容器可点击(例如GestureDetector)。问题:红色容器对 onTap() 没有反应。
import "dart:developer" as dev;
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:sdg3/Widget/rotation_controller.dart';
import 'package:sdg3/Widget/rotation_menu_button.dart';
import 'package:vector_math/vector_math.dart' show radians, Vector3;
class RadialMenu extends StatefulWidget {
final Widget open;
final Widget? close;
final int startAngle;
final List<RotationMenuButton> rotationButtons;
final RotationController? rotationController;
const RadialMenu({
Key? key,
this.rotationController,
required this.open,
this.startAngle = 0,
this.close,
required this.rotationButtons,
}) : super(key: key);
@override
createState() => _RadialMenuState();
}
class _RadialMenuState extends State<RadialMenu>
with SingleTickerProviderStateMixin {
late AnimationController controller;
late double maxButtonRadius;
@override …Run Code Online (Sandbox Code Playgroud) 我有一个 Recyclerview,允许用户通过向上滑动来更改比例,我为此使用gestureDetector 和 onFling,这工作正常,但是用户应该能够单击 recyclerView 的项目,我通过创建一个界面来做到这一点RecyclerView 并单击“活动”中的“活动”,但现在向上滑动时出现此错误。
java.lang.NullPointerException:指定为非空的参数为空:方法 kotlin.jvm.internal.Intrinsics.checkNotNullParameter,参数 e1 位于 com.example.simmone.utils.SwipeServices$GestureListener.onFling(未知来源:2)位于 android .view.GestureDetector.onTouchEvent(GestureDetector.java:763) 在 com.example.simmone.utils.SwipeServices.onTouch(SwipeServices.kt:33)
SwipeService.kt
class SwipeServices : View.OnTouchListener {
enum class SwipeDirection {
bottomToTop
}
private var rootLayout: ViewGroup? = null
private var layoutToShowHide: ViewGroup? = null
private var gestureDetector: GestureDetector? = null
private var swipeDirections: MutableList<SwipeDirection>? = null
fun initialize(rootLayout: ViewGroup, layoutToShowHide:ViewGroup?, swipeDirections: MutableList<SwipeDirection>,maxSwipeDistance: Int = 1) {
val gestureListener = GestureListener()
gestureDetector = GestureDetector(rootLayout.context, gestureListener)
this.rootLayout = rootLayout
this.layoutToShowHide = layoutToShowHide
this.swipeDirections = swipeDirections
gestureListener.MAX_SWIPE_DISTANCE …Run Code Online (Sandbox Code Playgroud)android ontouchlistener onfling gesturedetector android-recyclerview