本文介绍了安卓:四个方向导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我一直在努力实现四个方向轻扫导航。而且我对如何把东西放在一起的一些问题。

I've been trying to implement four directional swipe navigation. And I have some questions on how to put things together.

  1. 在考虑使用碎片。请问这锻炼?
  2. 在另一个想法是建立一个相对大的布局和安排意见,显示在图片。但是,可能会产生旋转装置波泰特/景观时的问题。
  3. 不知怎么模仿刷卡效果

如何所有这些问题都会锻炼?任何人都可以帮助它。我已经发现了关于向 TouchMove 功能问题

How all these points will workout? Can anyone help with it.I already found questions regarding to TouchMove feature

  1. 在只有四个方向触摸运动?
  2. Android - 基本手势检测
  3. https://github.com/JakeWharton/Android-DirectionalViewPager/
  1. Touch movement in only four directions?
  2. Android - basic gesture detection
  3. https://github.com/JakeWharton/Android-DirectionalViewPager/

任何建议和帮助是AP preciated,其原因是,我是比较新的Andr​​oid开发。

Any advice and help is appreciated, the reason is that I'm relatively new to android development.

推荐答案

不是那么难的任务,这要归功于它的滚轮几乎易蛋糕

not so hard task, thanks to Scroller its almost easy cake

如果你想放像按钮等一些互动性意见(我敢打赌,你会),你需要重写onInterceptTouchEvent(MotionEvent EV)

if you want to put some interactive Views like Button etc (and i bet you will) you need to override 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补充一点:

to test it add this in your Activity.onCreate:

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

这篇关于安卓:四个方向导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:12