gpt4 book ai didi

Android,Tabhost 中带有 GestureDetector 的 ViewFlipper

转载 作者:行者123 更新时间:2023-11-29 22:16:13 24 4
gpt4 key购买 nike

我想创建一个 tabhost 并且我想在一项 Activity 中添加一个 viewflipper。此 Activity 适用于 tabhost。我写了,但什么不起作用。有谁知道我该怎么做?我希望他们明白我想要什么。

enter image description here

我想在这个 Activity 中使用 GestureDetector。我可以用按钮更改图片,但不能用手指。

最佳答案

我以前做过。 ( https://github.com/vancexu/AimTo/tree/master/src/com/hackingtrace/vancexu AimToActivity.java)
只需实现您自己的 GestureDetector ,然后在您的选项卡 Activity 中注册一个 OnTouchListener

对了,我也实现了幻灯片动画,使用TabHost中的ViewFlipper

我的 TabHostActivity 中的一些代码

private static final int SWIPE_MIN_DISTANCE = 180; 
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;

public void onCreate(Bundle savedInstanceState) {
....
gestureDetector = new GestureDetector(new MyGestureDetector());

gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};

class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
TabHost tabHost = getTabHost();
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe, tab change to right
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.i("Gesture ", "right");
if (currentView == maxTabIndex) {
currentView = 0;
} else {
currentView++;
}
viewFlipperBody.setInAnimation(slideLeftIn);
viewFlipperBody.setOutAnimation(slideLeftOut);
viewFlipperBody.setDisplayedChild(currentView);

} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.i("Gesture ", "left");
if (currentView == 0) {
currentView = maxTabIndex;
} else {
currentView--;
}
viewFlipperBody.setInAnimation(slideRightIn);
viewFlipperBody.setOutAnimation(slideRightOut);
viewFlipperBody.setDisplayedChild(currentView);
}
} catch (Exception e) {
// nothing
e.printStackTrace();
}
tabHost.setCurrentTab(currentView);
return false;
}
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

if (gestureDetector.onTouchEvent(event)) {
event.setAction(MotionEvent.ACTION_CANCEL);
}
return super.dispatchTouchEvent(event);
}

关于Android,Tabhost 中带有 GestureDetector 的 ViewFlipper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8424668/

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