1

Ich versuche RTL-Layer-Direktion in Aktivität mit Viewpager und TabLayout.aber in RTL-Layer Viewpager Swipe Richtung umgekehrt und mit CurrentItem zu ViewPager, TabLayout auch geändert werden.Ansicht ändern Swipe-Richtung in RTL-Ebene in Android

Ich versuche dies nach und vor setupWithViewPager, aber Dosis funktioniert nicht.

keine Idee .. !!

ich versuche dies:

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

und diese:

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

Antwort

0

Sie einen benutzerdefinierten Pager-Adapter erstellen:

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

}

und die Verwendung es:

/* 
* 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){} 
    }); 
}