更改 viewPager 滑动方向 [英] Changing viewPager swipe direction

查看:25
本文介绍了更改 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 滑动方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆