Determining Fling Direction

你离开我真会死。 提交于 2019-12-24 17:27:08

问题


I'm using a GestureDetector to have onFling() called. It seems to be correctly detecting my flings as it triggers a Log message I created. I'm trying to determine the direction of the fling, but am running into problems. The x values are the same for both MotionEvent objects passed into the onFling() method so I cannot determine the direction. For example, I get:

08-05 16:36:08.679: DEBUG/mView(14616): fling2: 131.0 131.0

When I do:

Log.d("mView", "fling2: " + e1.getX() + " " + e2.getX());

When performing the fling, I am only moving my finger horizontal so this makes no sense to me. What could be going wrong here?


回答1:


You can use droidQuery: https://github.com/phil-brown/droidQuery. It will really simplify your code and make it easy to use. Here is all you need to put in your activities onCreate():

//global variables
private boolean isSwiping = false;
private SwipeDetector.Direction swipeDirection = null;
private View v;//set to the parent layout of the fragments.

//swipe-handling code
$.with(v).swipe(new Function() {
    @Override
    public void invoke($ droidQuery, Object... params) {
        if (params[0] == SwipeDetector.Direction.START)
            isSwiping = true;
        else if (params[0] == SwipeDetector.Direction.STOP) {
            if (isSwiping) {
                isSwiping = false;
                if (swipeDirection != null) {
                    switch(swipeDirection) {
                        case DOWN :
                            //TODO: Down swipe complete, so do something
                            break; 
                        case UP :
                            //TODO: Up swipe complete, so do something
                            break; 
                        case LEFT :
                            //TODO: Left swipe complete, so do something
                            break; 
                        case RIGHT :
                            //TODO: Right swipe complete, so do something (such as):
                            day++;
                            Fragment1 rightFragment = new Fragment1();
                            Bundle args = new Bundle();
                            args.putInt("day", day);
                            rightFragment.setArguments(args);

                            android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                            transaction.replace(R.id.fragment_container, rightFragment);
                            transaction.addToBackStack(null);
                            transaction.commit();
                            break; 
                        default :
                            break; 
                    }
                }
            }
        }
        else {
            swipeDirection = (SwipeDetector.Direction) params[0];
        }
    }
});


来源:https://stackoverflow.com/questions/18067336/determining-fling-direction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!