gpt4 book ai didi

java - Android:四向导航

转载 作者:太空狗 更新时间:2023-10-29 16:19:16 26 4
gpt4 key购买 nike

我一直在尝试实现四向滑动导航。我有一些关于如何把东西放在一起的问题。

  1. 考虑使用 fragment 。它会锻炼吗?
  2. 另一个想法是创建一个大的相对布局并如图所示排列 View 。但在旋转设备纵向/横向时可能会出现问题。
  3. 以某种方式模拟滑动效果

所有这些点将如何锻炼?任何人都可以帮助它。我已经找到关于 TouchMove 功能

的问题
  1. Touch movement in only four directions?
  2. Fling gesture detection on grid layout
  3. https://github.com/JakeWharton/Android-DirectionalViewPager/

感谢任何建议和帮助,原因是我对 android 开发还比较陌生。

enter image description here

最佳答案

不是那么难的任务,多亏了 Scroller 它几乎是简单的蛋糕

如果你想放置一些交互式 View ,比如 Button 等(我打赌你会的),你需要覆盖 onInterceptTouchEvent(MotionEvent ev)

public class FourDirectionLayout extends ViewGroup {
private GestureDetector mDetector;
private Scroller mScroller;
private final String[] TEXTS = {
"left view, left swipe only",
"right view, right swipe only",
"top view, top swipe only",
"bottom view, bottom swipe only",
"central view, swipe to the left, right, top or bottom",
};
private final int[] COLORS = {
0xaa0000ff, 0xaa0000ff, 0xaaff0000, 0xaaff0000, 0xaa00ff00
};
private final int[] PACKED_OFFSETS = {
-1, 0, 1, 0, 0, -1, 0, 1, 0, 0
};

public FourDirectionLayout(Context context) {
super(context);
for (int i = 0; i < TEXTS.length; i++) {
TextView tv = new TextView(context);
tv.setTag(i);
tv.setTextSize(32);
tv.setTypeface(Typeface.DEFAULT_BOLD);
tv.setTextColor(0xffeeeeee);
tv.setText(TEXTS[i]);
tv.setBackgroundColor(COLORS[i]);
addView(tv);
}
mDetector = new GestureDetector(context, mListener);
mScroller = new Scroller(context);
}

private OnGestureListener mListener = new SimpleOnGestureListener() {
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (!mScroller.isFinished()) {
return false;
}
int sx = getScrollX();
int sy = getScrollY();
int w = getWidth();
int h = getHeight();
int DURATION = 500;
// check if horizontal/vertical fling
if (Math.abs(velocityX) > Math.abs(velocityY)) {
if (sy != 0 || velocityX * sx < 0) {
return false;
}
// DURATION = (int) (1000 * w / Math.abs(velocityX));
int distance = velocityX < 0? w : -w;
mScroller.startScroll(sx, sy, distance, 0, DURATION);
} else {
if (sx != 0 || velocityY * sy < 0) {
return false;
}
// DURATION = (int) (1000 * h / Math.abs(velocityY));
int distance = velocityY < 0? h : -h;
mScroller.startScroll(sx, sy, 0, distance, DURATION);
}
invalidate();
return true;
}
};

@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
invalidate();
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
mDetector.onTouchEvent(event);
return true;
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int cnt = getChildCount();
for (int i = 0; i < cnt ; i++) {
View child = getChildAt(i);
int idx = (Integer) child.getTag() << 1;
int xOffset = (r - l) * PACKED_OFFSETS[idx];
int yOffset = (b - t) * PACKED_OFFSETS[idx + 1];
child.layout(l + xOffset, t + yOffset, r + xOffset, b + yOffset);
}
}
}

要测试它,请将此添加到您的 Activity.onCreate 中:

ViewGroup layout = new FourDirectionLayout(this);
setContentView(layout);

关于java - Android:四向导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19267763/

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