当我将viewpager从tab0滚动到tab1时,tab1正确显示但是它击中了tab2 webservice。如果显示tab2,那么它就会点击tab3 webservice。
public class OrderDetailsTab extends Fragment {
View view;
ViewPager viewPager;
TabLayout tabLayout;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.tab_layout, container, false);
tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
viewPager = (ViewPager) view.findViewById(R.id.pager);
tabLayout.addTab(tabLayout.newTab().setText("STATUS"));
tabLayout.addTab(tabLayout.newTab().setText("TRUCKS"));
tabLayout.addTab(tabLayout.newTab().setText("CHARGES"));
tabLayout.addTab(tabLayout.newTab().setText("QUOTES"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager.setAdapter(new MyPagerAdapter(getActivity().getSupportFragmentManager()));
MyPagerAdapter myPagerAdapter=new MyPagerAdapter(getActivity().getSupportFragmentManager());
viewPager.setAdapter(myPagerAdapter);
myPagerAdapter.notifyDataSetChanged();
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
myPagerAdapter.getRegisteredFragment(viewPager.getCurrentItem());
return view;
}
public static class MyPagerAdapter extends DetailPagerAdapter{
private static int NUM_ITEMS = 4;
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return StatusTab.init(0);
case 1:
return TruckInfoTab.init(1);
case 2:
return ApprovalTab.init(2);
case 3:
return QuotesFragment.init(3);
default:
return null;
}
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
}
@Override
public void onResume() {
super.onResume();
((AppCompatActivity) getActivity()).getSupportActionBar().hide();
}
}
抽象适配器类:
public abstract class DetailPagerAdapter extends FragmentPagerAdapter {
// Sparse array to keep track of registered fragments in memory
private SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
public DetailPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Register the fragment when the item is instantiated
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
// Unregister when the item is inactive
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
// Returns the fragment for the position (if instantiated)
public Fragment getRegisteredFragment(int position) {
Log.e("pos---", "" + position);
return registeredFragments.get(position);
}
}
答案 0 :(得分:0)
默认情况下,viewpager会加载一个额外的页面以使体验流畅。
viewPager.setOffscreenPageLimit(int); //默认为一个
正常情况并且不会影响功能,因为您需要在相应的片段中显示服务数据。 例如:片段01:显示服务01数据等等。
设置应保留在页面两侧的页数 *处于空闲状态的视图层次结构中的当前页面。页面超出此范围 *需要时,将从适配器重新创建限制。 * *
这是作为优化提供的。如果您事先知道这个号码 *您需要支持的页面或具有延迟加载机制的页面 *在您的页面上,调整此设置可以获得感知平滑度的好处 *分页动画和互动。如果您的页数很少(3-4) *您可以一次保持活动状态,在布局上花费的时间更少 *新创建的视图子树作为用户来回翻页。
* *您应该将此限制保持在较低水平,尤其是当您的页面具有复杂的布局时。 *此设置默认为1.