更改 viewPager 滑动方向

Changing viewPager swipe direction(更改 viewPager 滑动方向)
本文介绍了更改 viewPager 滑动方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 TabLayout 和 viewPager 以及向左或向右滑动 viewPager 以在页面之间导航的选项.

I'm using TabLayout and viewPager with an option to swipe the viewPager to the left or right in order to navigate between pages.

我的问题是,我的应用程序是基于 RTL(从右到左)并且滑动方向是相反的.

My problem is, that my application is RTL based (right-to-left) and the swipe direction is reversed.

我正在尝试将滑动方向从默认更改为反向版本.我在网上搜索了很多,但找不到方法.

I'm trying to change the swipe direction from the default to the reversed version. I've been searching alot on the web and couldn't find a way how to do it.

我正在使用 android.support.v4.view.ViewPager;

这就是我使用 viewPager 初始化 TabLayout 的方式:

and this is how I initialize my TabLayout with the viewPager:

// View Page Adapter
        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        //View Page Adapter Configuration
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        viewPager.setOffscreenPageLimit(3);
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

总结:目前,当我向左滑动 viewPager 时,它会显示下一页.在 RTL 中,当您向右滑动 viewPager 时,它会显示下一页.

Summary: Currently, When I'm swiping the viewPager to the left, it shows the next page. in RTL, when you swipe the viewPager to the right, it shows the next page.

这可能很难理解,但就是这样.向右滑动显示下一页

It might be hard to understand, but here it is. Swiping right shows the next page

而我需要向右滑动才能显示上一页.

while I need swiping right to show the previous page.

推荐答案

改变Viewpager的滑动方向是非常简单的技术.

It is the very simple techniqe to change the swipe direction of the Viewpager.

有两个简单的步骤必须跟随你,

There are two simple step which have to follow you,

1.改变View pager的旋转,

1. Change the rotation of the View pager,

viewpager.setRotationY(180);

2.然后再改变viewpager的子片段容器的方向,

2. Then again change the direction of the fragment container which is the child of viewpager,

recyclerView.setRotationY(180);

注意:在我的例子中,我使用 recyclerview 作为视图寻呼机的子级.

Note: In my case I have used recyclerview as the child of the view pager.

这对我来说是 100% 的.

This is 100% worked for me.

这篇关于更改 viewPager 滑动方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)