gpt4 book ai didi

java - 让 Imageview 区分单击和滚动

转载 作者:太空宇宙 更新时间:2023-11-04 12:27:15 25 4
gpt4 key购买 nike

我正在尝试在同一个 Imageview 上实现单击和滚动,我成功实现了单击,但现在我不知道如何实现滚动。我需要在单击和滚动上实现不同的操作,对于单击,我想启动一个我已经完成的 Activity ,现在在滚动上,我需要调用一个方法“updateData”。请帮我。下面是我的代码

    public class GestureListener extends
GestureDetector.SimpleOnGestureListener {

@Override
public boolean onDown(MotionEvent e) {

return true;
}

// event when double tap occurs
@Override
public boolean onSingleTapUp(MotionEvent e) {

tapped = !tapped;

if (tapped) {

Intent intent = new Intent(activity, MainActivity.class);

activity.startActivity(intent);
}




} else {



}

return true;
}
}

image.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return gestureDetector.onTouchEvent(event);
}

});

最佳答案

一个简单的方法是扩展 GestureDetector.SimpleOnGestureListener,然后为您的 ImageView 设置此自定义监听器。

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
final GestureDetectorCompat gdt = new GestureDetectorCompat(this, new CustomGestureListener());
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent event) {
gdt.onTouchEvent(event);
return true;
}
});
}

private class CustomGestureListener extends GestureDetector.SimpleOnGestureListener {

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d(TAG, "onSingleTapConfirmed() called with: " + "e = [" + e + "]");
return super.onSingleTapConfirmed(e);
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

// do some stuff maybe..
// do other stuff

updateData(e1, e2, distanceX, distanceY);

return super.onScroll(e1, e2, distanceX, distanceY);
}

}

private void updateData(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

Log.d(TAG, "updateData() called with: " + "e1 = [" + e1 + "], e2 = [" + e2 + "], distanceX = [" + distanceX + "], distanceY = [" + distanceY + "]");

}

}

关于java - 让 Imageview 区分单击和滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38243088/

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