使用TabAdapter滚动水平和垂直

时间:2018-01-07 01:05:23

标签: java android android-fragments adapter

基本上我已经有了一个Horizo​​ntalAdapters,这意味着我只能向左和向右滑动,在那些滑动中我有3页。

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

现在我尝试在我的EmptyFragment顶部和下方添加两个页面,这样当我向上滑动时,它会转到另一个页面,当我向下滑动时。

我有两个片段,我想放在我的空片段顶部和下面。

这是我的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);

这是我的EmptyFragment

public class EmptyFragment extends Fragment {

    public static EmptyFragment create(){
        return new EmptyFragment();


    }

如果你不理解我试图做的事情基本上只是一个你可以向左,向右,向上和向下滑动的页面,它会查看不同的页面。

MainLayout文件

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="c.kristofer.jax2.MainActivity">

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


</RelativeLayout>

我的两个布局文件,我想放在我的空片段上面和下面

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

</LinearLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/yellow">

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

我回答完全相同的问题here。可能你们都是同一个班级,所以请用ctrl + c和ctrl + v代码注意。

我会复制

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

答案 1 :(得分:0)

在其中创建一个包含VerticalViewPager的片段,并添加top,empty和bottom以查看分页器并将默认页面设置为空页面片段

MainActivity布局应如下所示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="c.kristofer.jax2.MainActivity">

    <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"/>
</RelativeLayout>

Horizo​​ntalViewPager应如下所示,请参阅VerticalScrollFragment

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 VerticalScrollFragment.create();
            case 2:
                return StoryFragment.create();
        }
        return null;
    }


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

然后VerticalFragment布局vertical.xml文件将是这样的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="c.kristofer.jax2.MainActivity">

<me.kaelaela.verticalviewpager.VerticalViewPager
    android:id="@+id/am_scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

然后VerticalFragment类将如下所示

公共类VerticalScrollFragment扩展Fragment {

VerticalViewPager verticalViewPager;
VerticalViewPagerAdapter verticalPageAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.vertical, null);
    verticalViewPager = view.findViewById(R.id.am_scrollView);
    verticalPageAdapter = new VerticalViewPagerAdapter(getChildFragmentManager());
    verticalViewPager.setAdapter(verticalPageAdapter)
    return inflater.inflate(R.layout.vertical, null);
}

public class VerticalViewPagerAdapter extends FragmentPagerAdapter {


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

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


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

}

}