ViewPagerAdapter,我不明白的ViewPagerAdapter的行爲,我知道它創建一個向前的位置,從中選擇,例如,如果我選擇位置0在開始它返回我( 0,1),我選擇1我返回(2),然後到達最後一個位置已經不會返回任何東西,因爲在理論上penuiltima並收取utlima位置,如果我處於位置1並且想要不再返回負載,因爲我想它已經加載了,如果我從位置0移動到最後一個位置返回加載(4和3)有什麼辦法來取消這種行爲或只加載你在沒有前進位置的位置ViewPager功能的前兩個頁面
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");
}
}
`
這是一個例子,該片段實際上是動態的,但我想知道如果我能得到你選擇的位置,而不是下一個片段viewPager.getCurrentItem()的位置,但是當我將它發送到片段並加載它時,GetItem如果您返回到實際位置,則您沒有偵聽器。 –