ViewPagerAdapter,我不明白ViewPagerAdapter的行为,我理解它创建了一个从中选择它的前向位置,例如,如果我在开头选择位置0它返回我(0,1)和我选择1 me返回(2),然后到达最后一个位置已经没有返回任何东西,因为理论上penuiltima和充电utlima位置,如果我在位置1并且想要不再返回负载,因为我想它已经加载,如果我从位置0移动到最后位置返回负载(4和3)有任何方法可以取消此行为或仅加载您没有前进位置的位置
public class MainActivity extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("CONSOLE", "Main :::::::::: onCreateView");
View rootView = inflater.inflate(R.layout.activity_main, container, false);
tabLayout = (TabLayout) rootView.findViewById(R.id.tablayout_menu);
viewPager = (ViewPager) rootView.findViewById(R.id.viewPager_menu);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 4"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 5"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
CostumerFragment2 adapter = new CostumerFragment2(getActivity().getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
public class CostumerFragment2 extends FragmentStatePagerAdapter {
public CostumerFragment2(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
System.out.println("POSICION: " + index);
switch (index) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
case 3:
return new Fragment4();
case 4:
return new Fragment5();
}
return null;
}
@Override
public int getCount() {
return 5;
}
}
@Override
public void onResume() {
super.onResume();
Log.d("CONSOLE", "Main :::::::::: onResume");
}
@Override
public void onPause() {
super.onPause();
Log.d("CONSOLE", "Main :::::::::: onPause");
}
@Override
public void onStart() {
super.onStart();
Log.d("CONSOLE", "Main :::::::::: onStart");
}
}
***新**
`
public class Principal extends AppCompatActivity {
private FrameLayout frameLayoutFragment;
private BottomNavigationView bottomNavigationView;
private AHBottomNavigation ahBottomNavigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("CONSOLE", "PAPA onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.principal);
frameLayoutFragment = (FrameLayout) findViewById(R.id.myFragemnt);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigation);
ahBottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_navigation);
final AHBottomNavigationItem item1 = new AHBottomNavigationItem("Menú", R.drawable.menu);
AHBottomNavigationItem item2 = new AHBottomNavigationItem("Favoritos", R.drawable.favoritos_item);
AHBottomNavigationItem item3 = new AHBottomNavigationItem("Tu orden", R.drawable.item_charola);
AHBottomNavigationItem item4 = new AHBottomNavigationItem("Perfil", R.drawable.item_perfil);
ahBottomNavigation.addItem(item1);
ahBottomNavigation.addItem(item2);
ahBottomNavigation.addItem(item3);
ahBottomNavigation.addItem(item4);
ahBottomNavigation.setBehaviorTranslationEnabled(false);
ahBottomNavigation.setAccentColor(R.color.colorAccent);
ahBottomNavigation.setInactiveColor(R.color.colorPrimary);
ahBottomNavigation.setForceTint(true);
ahBottomNavigation.setTranslucentNavigationEnabled(true);
ahBottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
ahBottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
ahBottomNavigation.setColored(false);
ahBottomNavigation.setCurrentItem(1);
ahBottomNavigation.setNotification("10", 1);
ahBottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
@Override
public boolean onTabSelected(int position, boolean wasSelected) {
// Do something cool here...
switch (position) {
case 0:
//item1.setColor(getResources().getColor(R.color.colorAccent));
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
MainActivity mainActivity = new MainActivity();
fragmentTransaction.replace(R.id.myFragemnt, mainActivity);
fragmentTransaction.commit();
return true;
case 1:
FragmentManager fm2 = getSupportFragmentManager();
FragmentTransaction fragmentTransaction2 = fm2.beginTransaction();
FragmentFavoritos fragmentFavoritos = new FragmentFavoritos();
fragmentTransaction2.replace(R.id.myFragemnt, fragmentFavoritos);
fragmentTransaction2.commit();
return true;
case 2:
FragmentManager fm3 = getSupportFragmentManager();
FragmentTransaction fragmentTransaction3 = fm3.beginTransaction();
FragmentOrden fragmentOrden = new FragmentOrden();
fragmentTransaction3.replace(R.id.myFragemnt, fragmentOrden);
fragmentTransaction3.commit();
return true;
case 3:
FragmentManager fm4 = getSupportFragmentManager();
FragmentTransaction fragmentTransaction4 = fm4.beginTransaction();
FragmentPerfil fragmentPerfil = new FragmentPerfil();
fragmentTransaction4.replace(R.id.myFragemnt, fragmentPerfil);
fragmentTransaction4.commit();
return true;
}
return true;
}
});
}
@Override
protected void onResume() {
super.onResume();
Log.d("CONSOLE", "PAPA onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d("CONSOLE", "PAPA onPause");
}
@Override
protected void onStart() {
super.onStart();
Log.d("CONSOLE", "PAPA onStart");
}
}
`
答案 0 :(得分:0)
1。要获取当前selected
页position
:
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// Here position is the selected page-position
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
2。转到具体的page
:
viewPager.setCurrentItem(POSITION);
3。根据page
选择更改Tab
:
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
// Change page
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
<强>更新强>
#。使用onCreateView()
方法:
CostumerFragment2 adapter = new CostumerFragment2(getChildFragmentManager());
而不是:
CostumerFragment2 adapter = new CostumerFragment2(getActivity().getSupportFragmentManager());
#。在CostumerFragment2
添加以下方法:
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
希望这会有所帮助〜