Make Swap Vertical and Horizontal Work With Two Adapters

后端 未结 1 1756
萌比男神i
萌比男神i 2021-01-24 15:16

So basically I\'m trying to make my app like snapchat where you can swipe left,right,up and down. My problem is that the app does not realize there are two adapters and only wor

1条回答
  •  攒了一身酷
    2021-01-24 15:23

    Getting the base from here.

    Create a GestureDetectorCompat object

    GestureDetectorCompat gestureDetectorCompat;
    

    and override onTouchEvent in the activity

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

    or if you want to detect on some view then you can Override onTouch

    someView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    gestureDetectorCompat.onTouchEvent(motionEvent);
                    return false;
                }
            });
    

    and initialize gestureDetectorCompat as follows somewhere preferably in onCreate() and you are done.

    gestureDetectorCompat = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                float angle = (float) Math.toDegrees(Math.atan2(e1.getY() - e2.getY(), e2.getX() - e1.getX()));
    
                if (angle > -45 && angle <= 45) {
                    if(goRight != 5) mainContainer.setCurrentItem(goRight);
                    return true;
                }
    
                if (angle >= 135 && angle < 180 || angle < -135 && angle > -180) {
                    if(goLeft != 5) mainContainer.setCurrentItem(goLeft);
                    return true;
                }
    
                if (angle < -45 && angle >= -135) {
                    if(goUp != 5)mainContainer.setCurrentItem(goUp);
                    return true;
                }
    
                if (angle > 45 && angle <= 135) {
                    if(goDown != 5)mainContainer.setCurrentItem(goDown);
                    return true;
                }
    
                return false;
            }
    
    
        });
    

    Then you can use mainContainer.setCurrentItem(number); to go to other position/fragment.

    Don't forget that the number change if you are in different number. Like this

    switch(adapter.getCurrentItem()){
        case 0:
            goRight = 2;
            goLeft = 4;
            goUp = 3;
            goDown = 1;
            break;
        case 1:
            goRight = 5;
            goLeft = 5;
            goUp = 0;
            goDown = 5;
            break;
        case 2:
            goRight = 5;
            goLeft = 0;
            goUp = 5;
            goDown = 5;
            break;
        case 3:
            goRight = 5;
            goLeft = 5;
            goUp = 5;
            goDown = 0;
            break;
        case 4:
            goRight = 0;
            goLeft = 5;
            goUp = 5;
            goDown = 5;
            break;
    }
    

    When you are in the fragment 4 you can only go to 0, not 2 if you swipe left-right.

    And this should be your adapter

    public class SwipeViewPager extends FragmentPagerAdapter {
    
    public SwipeViewPager(FragmentManager fm) {
        super(fm);
    }
    
    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return EmptyFragment.create();
            case 1:
                return StoryFragment.create();
            case 2:
                return ExtrasFragment.create();
            case 3:
                return ChatFragment.create();
            case 4:
                return SettingsFragment.create();
        }
        return null;
    }
    
    
    @Override
    public int getCount() {
        return 5;
    }
    

    0 讨论(0)
提交回复
热议问题