Android的刷卡换工作不片段片段、工作、Android

由网友(俗酿)分享简介:我试图让一个应用程序,用户可以在刷卡并更改该片段他们看到在屏幕上。我不能使用视图寻呼机,因为我希望用户能够刷到不同的片段,直到永远。这里是我的片段检测器:I am trying to make an app where a user can swipe and change which fragment they a...

我试图让一个应用程序,用户可以在刷卡并更改该片段他们看到在屏幕上。我不能使用视图寻呼机,因为我希望用户能够刷到不同的片段,直到永远。这里是我的片段检测器:

I am trying to make an app where a user can swipe and change which fragment they are seeing on the screen. I can not use view pager because I want the user to be able to swipe to different fragments forever. Here is the detector in my fragment:

 class MyGestureDetector extends SimpleOnGestureListener {
            @Override
            public boolean onDown(MotionEvent e) {
            return true;
        }
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE & Math.abs(velocityX) > 10) {
                    left();
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE & Math.abs(velocityX) > 10) {
                    right();
                }

            return false;
        }
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY){
                  if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE & distanceX > distanceY) {
                    left();
                  }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE & distanceX > distanceY) {
                      right();
                  }
                return false;
            }
    }
    public void right(){
        mCallback.dateNumber(true);
        sportView.setText("Loading");
    }public void left(){
        mCallback.dateNumber(false);
        sportView.setText("Loading");
    }

在我的活动,这里是我加改变片段监听器:

In my activity, here is the listener that I added to change fragments:

                    @Override
            public void dateNumber(Boolean left_right) {
                //true == right
                //false == left
                if(left_right == false){
                    day = day + 1;
                    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();
                }else if(left_right == true){
                    day = day - 1;
                    Fragment1 leftFragment = new Fragment1();
                    Bundle args = new Bundle();
                    args.putInt("day", day);
                    leftFragment.setArguments(args);

                    android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.fragment_container, leftFragment);
                    transaction.addToBackStack(null);
                    transaction.commit();
                }
                left_right = null;
            }

我知道,滑动手势总是被公认,但有时新的片段会开不起来。没有人知道为什么?

I know that the swipe gesture is always being recognized but sometimes the new fragment won't open up. Does anyone know why?

推荐答案

首先,你真的可以使用的 droidQuery :

First of all, you can really simplify your swipe code using droidQuery:

//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];
        }
    }
});

您可以找到更多的片段交易here.

You can find more on Fragment transactions here.

此外,考虑保持一个内部偏移变量,跟踪的 +/- 从零偏移。因此,举例来说,你可以从的ArrayList 获得已实例化的片段,然后就换​​出一个在 mArrayList.get(补偿),和丢权当,做偏移++ ,和偏移 - '左刷卡

Also, consider keeping an int offset variable that keeps track of the +/- offset from zero. So for instance, you could get the already-instantiated Fragments from an ArrayList, then just swap out the one at mArrayList.get(offset), and when flinging right, do offset++, and 'offset--` for left swipes.

的要求,在评论,这code,可用于处理挥笔和一个小孩图片点击:

As requested in the comments, this code can be used to handle swipes and a child image click:

包括 SwipeInterceptorView 在主布局( RES /布局/ main.xml中):

<?xml version="1.0" encoding="utf-8"?>
<self.philbrown.SwipeInterceptorView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</self.philbrown.SwipeInterceptorView>

您需要有类变量:

SwipeInterceptorView view;//instantiated in onCreate
ImageView fragImage;//must be instantiated when the new Fragment is transitioned in

接下来,包括以下组件的onCreate

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //set main view to the main layout
    setContentView(R.layout.main);
    //get a reference to the content view
    view = (SwipeInterceptorView) findViewById(R.id.swipe_view);
    //add Swiper
    view.setSwipeListener(new SwipeListener() {
        public void onUpSwipe(View v) {
            //TODO handle up swipe
        }
        public void onRightSwipe(View v) {
            //TODO handle right swipe
        }
        public void onLeftSwipe(View v) {
            //TODO handle left swipe
        }
        public void onDownSwipe(View v) {
            //TODO handle down swipe
        }
    });
    view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return super.onTouch(v, event);
        }
    });
}

在包含ImageView的新片段转换,你需要引用它,并更新刷卡拦截器的onTouch方式:

When the new Fragment containing the ImageView is transitioned in, you need to reference it and update the swipe interceptor's onTouch method:

fragImage = (ImageView) fragment/* references the now non-null, on-display fragment */.getView().findViewById(R.id.yourImageId);
int[] origin = new int[2];
fragImage.getLocationOnScreen(origin);
final Rect bounds = new Rect(origin[0], origin[1], fragImage.getRight(), fragImage.getBottom());
view.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public void onTouch(View v, MotionEvent event) {
        if (bounds.contains(event.getRawX(), event.getRawY())) {
            return false;//now clicks will be handled by the Image.
        }
        return v.onTouchEvent(event);
    }
});
阅读全文

相关推荐

最新文章