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

Make Swap Vertical and Horizontal Work With Two Adapters(使用两个适配器进行垂直和水平交换)

本文介绍了使用两个适配器进行垂直和水平交换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我试图让我的应用程序像 snapchat 一样,您可以在其中向左、向右、向上和向下滑动.我的问题是该应用程序没有意识到有两个适配器并且只能工作一个或一个(垂直或水平).我如何制作它以便我可以在我的

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 works one or the either(Vertically or Horrizontally).How do I make it so that I can swipe vertically as well as Horizontally on my

空片段

我目前将 VerticalViewPagerAdapter 扩展到 FragmentPagerAdapter

I currently have my VerticalViewPagerAdapter extending to a 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

As well as my HorizontalViewPagerAdapter

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

不知何故,我无法让它们同步,我可以通过更改布局文件来更改我的应用程序是垂直还是水平

And somehow I cant have them in sync its either one or the other and I can alter if my app goes vertical or horizontal by changing my layoutfile

<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 有关

Im not sure if it has to do with my MainActivity which is

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

推荐答案

从这里获取基础.

创建一个GestureDetectorCompat对象

GestureDetectorCompat gestureDetectorCompat;

并在活动中覆盖 onTouchEvent

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

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

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

并在 onCreate() 中按如下方式初始化gestureDetectorCompat,然后就完成了.

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


    });

然后你可以使用 mainContainer.setCurrentItem(number); 去其他位置/片段.

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

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

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

这应该是你的适配器

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

这篇关于使用两个适配器进行垂直和水平交换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用两个适配器进行垂直和水平交换

基础教程推荐