更改浏览器在Android中的RTL层中滑动方向

时间:2016-07-28 07:32:58

标签: android android-viewpager right-to-left android-tablayout direction

我尝试在viewpager和TabLayout的活动中使用RTL layerdirection。但是在RTL层中,viewpager滑动方向反转,并且将CurrentItem设置为ViewPager,TabLayout也要更改。

我在setupWithViewPager之后和之前尝试这个但是没有用。

任何想法.. !!

我试试这个:

    mViewPager = (ViewPager) findViewById(R.id.mViewPager );
    mViewPager.setAdapter(Adapter);
    mViewPager.setCurrentItem(mViewPager.getAdapter().getCount() - 1,false);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

和此:

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    mViewPager.setCurrentItem(mViewPager.getAdapter().getCount() - 1,false);

1 个答案:

答案 0 :(得分:0)

您可以创建自定义寻呼机适配器:

public class Custom_PagerAdapter extends FragmentPagerAdapter{

/*
* Attributes:
*
* the class attributes
*
* */

private ArrayList<Object> ItemList;

/*
* AutoRuns:
*
* these methods were called at startup
*
* */

/////////////
//////////////////Constructor:
//////////////
public Custom_PagerAdapter(FragmentManager fm){
    super(fm);
    ItemList = new ArrayList<>();
}

/*
* Setters:
*
* sets class attributes
*
* */

/////////////
//////////////////addItem:
//////////////
public void addItem(Object item){ItemList.add(item);}

/*
* Getters:
*
* gets class attributes
*
* */

/////////////
//////////////////getItem:
//////////////
@Override
public Fragment getItem(int position){
    return (Fragment) ItemList.get(getDirectionalPagerPosition(position));
}

/////////////
//////////////////getCount:
//////////////
@Override
public int getCount(){return ItemList.size();}

/*
* Other Functions:
*
* class other functions
*
* */

/////////////
//////////////////getDirectionalPagerPosition:
//calculates the pager position considering app direction
public int getDirectionalPagerPosition(int virtualPosition){
    int actualPosition;
    if(Core.getDirection() == Direction.LTR)//make your own if
        actualPosition = virtualPosition;
    else
        actualPosition = getCount()-1-virtualPosition;
    return actualPosition;
}

/////////////
//////////////////getDirectionalTabPosition:
//calculates the tab position considering app direction
public int getDirectionalTabPosition(int virtualPosition){
    int actualPosition;
    if(Core.getDirection() == Direction.LTR)
        actualPosition = virtualPosition;
    else{
        actualPosition = getCount()-2-virtualPosition;
        if(actualPosition < 0)
            actualPosition = 0;
        if(virtualPosition > getCount()-2)
            actualPosition = getCount()-1;
    }
    return actualPosition;
}

/////////////
//////////////////getDirectionalTabPositionOffset:
//calculates the tab position offset considering app direction
public float getDirectionalTabPositionOffset(float virtualPositionOffset){
    float actualPositionOffset;
    if(Core.getDirection() == Direction.LTR)
        actualPositionOffset = virtualPositionOffset;
    else
        actualPositionOffset = 1-virtualPositionOffset;
    return actualPositionOffset;
}

}

并使用它:

/*
* Initializers:
*
* were initialize the components
*
* */

/////////////
//////////////////initTabBar:
//////////////
private void initTabBar(){
    Main_TabLyt = (CustomTabLayout) findViewById(R.id.Main_TabLyt);
    /** count of tabs must be equal to count of pages in {@link #initPager()} */
    Main_TabLyt.addTab(Main_TabLyt.newTab().setText("Tab 1"));
    Main_TabLyt.addTab(Main_TabLyt.newTab().setText("Tab 2"));
    Main_TabLyt.addTab(Main_TabLyt.newTab().setText("Tab 3"));
    addTabBarOnTabSelectedListener();
}

/////////////
//////////////////initPager:
//////////////
private void initPager(){
    Main_PgrAdp = new Custom_PagerAdapter(getSupportFragmentManager());
    /** count of pages must be equal to count of tabs in {@link #initTabBar()} */
    Main_PgrAdp.addItem(new View1());
    Main_PgrAdp.addItem(new View2());
    Main_PgrAdp.addItem(new View3());
    Main_ViwPgr = (ViewPager) findViewById(R.id.Main_Pager);
    Main_ViwPgr.setAdapter(Main_PgrAdp);
    Main_ViwPgr.setCurrentItem(Main_PgrAdp.getDirectionalPagerPosition(0));//first page by considering app direction
    addPagerOnPageChangeListener();
}

/*
* Listeners:
*
* these methods are called after user action
*
* */

/////////////
//////////////////addTabBarOnTabSelectedListener:
//////////////
public void addTabBarOnTabSelectedListener(){
    Main_TabLyt.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
        @Override
        public void onTabSelected(Tab tab){//selects pager item by considering app direction
            Main_ViwPgr.setCurrentItem(Main_PgrAdp.getDirectionalPagerPosition(tab.getPosition()));
        }
        @Override
        public void onTabUnselected(Tab tab){}
        @Override
        public void onTabReselected(Tab tab){}
    });
}

/////////////
//////////////////addPagerOnPageChangeListener:
//////////////
private void addPagerOnPageChangeListener(){
    Main_ViwPgr.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels){//sets tab bar scroll position by considering app direction
            Main_TabLyt.setScrollPosition(Main_PgrAdp.getDirectionalTabPosition(position), Main_PgrAdp.getDirectionalTabPositionOffset(positionOffset), false);
        }
        @Override
        public void onPageSelected(int position){
            Tab tab = Main_TabLyt.getTabAt(Main_PgrAdp.getDirectionalPagerPosition(position));
            if(tab != null) tab.select();
        }
        @Override
        public void onPageScrollStateChanged(int state){}
    });
}