使用两个适配器进行垂直和水平交换

时间:2018-01-06 20:27:49

标签: android android-fragments android-viewpager adapter

所以基本上我试图让我的应用程序像Snapchat一样,你可以向左,向右,向上和向下滑动。我的问题是该应用程序没有意识到有两个适配器,只能工作一个或两个(垂直或Horrizontally)。我怎么做它,以便我可以垂直和水平滑动我的

  

EmptyFragment

我目前将VerticalViewPagerAdapter扩展为FragmentPagerAdapter

public class VerticalViewPager extends FragmentPagerAdapter {
public VerticalViewPager(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    switch (position){
        case 0:
            return SettingsFragment.create();
        case 1:
            return EmptyFragment.create();
        case 2:
            return ExtrasFragment.create();
    }
    return null;
}

@Override
public int getCount() {
    return 3;
}

和我的Horizo​​ntalViewPagerAdapter

一样
public class HorizontalViewPager extends FragmentPagerAdapter {

    public HorizontalViewPager(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return ChatFragment.create();
            case 1:
                return EmptyFragment.create();
            case 2:
                return StoryFragment.create();
        }
        return null;
    }


@Override
public int getCount() {
    return 3;
}

不知怎的,我不能让它们同步其中一个或者我可以通过更改我的布局文件来改变我的应用是垂直还是水平

<View
android:id="@+id/am_background_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"/>
<android.support.v4.view.ViewPager
    android:id="@+id/am_view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
<me.kaelaela.verticalviewpager.VerticalViewPager
    android:id="@+id/am_scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

我不确定它是否与我的MainActivity(

)有关
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //VerticalViewPager
    me.kaelaela.verticalviewpager.VerticalViewPager verticalViewPager = findViewById(R.id.am_scrollView);
    VerticalViewPager scrollViewAdapter = new VerticalViewPager(getSupportFragmentManager());
    verticalViewPager.setAdapter(scrollViewAdapter);
    verticalViewPager.setPageTransformer(false, new DefaultTransformer());

    //HorizontalViewPager
    View background = findViewById(R.id.am_background_view);
    ViewPager viewPager = findViewById(R.id.am_view_pager);
    HorizontalViewPager adapter = new HorizontalViewPager(getSupportFragmentManager());
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(1);

1 个答案:

答案 0 :(得分:1)

here获取基础。

创建 GestureDetectorCompat 对象

GestureDetectorCompat gestureDetectorCompat;

并覆盖活动

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

或者如果您想在某些视图中进行检测,则可以覆盖 onTouch

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

并初始化gestureDetectorCompat,如下所示,最好在 onCreate()中完成。

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;
        }


    });

然后您可以使用mainContainer.setCurrentItem(number);转到其他位置/片段。

如果您的号码不同,请不要忘记号码会发生变化。喜欢这个

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;
}

当您在片段4中时,如果向左右滑动,则只能转到0,而不是2。

这应该是你的适配器

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;
}