gpt4 book ai didi

android - 在 GridView 上实现滑动手势

转载 作者:太空宇宙 更新时间:2023-11-03 12:22:29 26 4
gpt4 key购买 nike

我想在 GridView 上实现滑动手势。例如:我有 3*3 GridView 和 9 张图片,当我将手指滑动到图片时 我想删除滑动的图片如果你有任何想法,有人可以帮助我。

谢谢

最佳答案

这就是例子,对我有用

public class GameActivity extends AppCompatActivity {

private GestureDetectorCompat detector;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);

detector = new GestureDetectorCompat(this, new MyGestureListener());

GridView board = (GridView) findViewById(R.id.board);
board.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
return false;
}
});
}

@Override
public boolean onTouchEvent(MotionEvent event) {
detector.onTouchEvent(event);
return super.onTouchEvent(event);
}

class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;

@Override
public boolean onDown(MotionEvent event) {
return true;
}

@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {

float diffY = event2.getY() - event1.getY();
float diffX = event2.getX() - event1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
}
return true;
}
}

private void onSwipeLeft() {

}

private void onSwipeRight() {

}

private void onSwipeTop() {

}

private void onSwipeBottom() {

}
}

关于android - 在 GridView 上实现滑动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11327095/

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