我在一个viewpager和3个片段中有3个标签我正在使用改装调用服务器但是当我启动活动时,前两个片段被调用,并且从两个网络被调用,当我去位置下一个标签时没有显示,位置变为2。
问题是当我来到活动视图寻呼机被初始化并且寻呼机适配器返回位于0和1位置的两个片段并且两者都膨胀以便从两者中调用网络但是当我将标签移动到下一个位置时2 ......它应该是1。
适配器类:
public class PagerAdapter extends FragmentStatePagerAdapter {
private int mNumTabs;
public PagerAdapter(FragmentManager fm, int numTabs) {
super(fm);
this.mNumTabs= numTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment1 tab0 = new Fragment1();
return tab0;
case 1:
Fragment2 tab1 = new Fragment2();
return tab1;
case 2:
Fragment3 tab2 = new Fragment3();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
return mNumTabs;
}
}
活动类:
public class UserProfileScreen extends AppCompatActivity {
private ViewPager viewPager;
private TabLayout tabLayout;
private Context context;
private PagerAdapter PagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile_screen);
context = this;
initViews();
tabLayout.addTab(tabLayout.newTab().setText("About"));
tabLayout.addTab(tabLayout.newTab().setText("Session"));
tabLayout.addTab(tabLayout.newTab().setText("Subjects"));
pagerAdapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(profilePagerAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
//Toast.makeText(context, "Unselected",Toast.LENGTH_LONG).show();
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
//Toast.makeText(context, "Reselected",Toast.LENGTH_LONG).show();
}
});
}
private void initViews(){
myToolbar = (Toolbar) findViewById(R.id.toolbar);
tabLayout = (TabLayout) findViewById(R.id.profile_tab_layout);
setSupportActionBar(myToolbar);
viewPager = (ViewPager) findViewById(R.id.profile_viewPager);
}
}
Fragment1:
public class Fragment1 extends Fragment {
private RecyclerView sessions_recycler_view;
private SessionsAdapter sessionsAdapter;
private SessionProfileFragmentPresenter presenter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sessionsAdapter = new SessionsAdapter(getContext(),this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile,container,false);
sessions_recycler_view = (RecyclerView) view.findViewById(R.id.sessions_list);
sessions_recycler_view.setAdapter(sessionsAdapter);
//Server Call to get List<HashMap<String,Object>> data
setDataToAdapter(data);
return view;
}
private void setDataToAdapter(List<HashMap<String, Object>> sessions) {
sessionsAdapter.setData(sessions);
}
}
Fragment2也与Fragment1相同。
答案 0 :(得分:0)
我支持Pramod Garg的回答
viewPager的默认OffscreenPageLimit为1,因此以下代码可能对您有用。我刚刚添加了一行viewpager.setOffscreenPageLimit(2)。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile_screen);
context = this;
initViews();
tabLayout.addTab(tabLayout.newTab().setText("About"));
tabLayout.addTab(tabLayout.newTab().setText("Session"));
tabLayout.addTab(tabLayout.newTab().setText("Subjects"));
pagerAdapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(profilePagerAdapter);
viewpager.setOffscreenPageLimit(2)
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
//Toast.makeText(context, "Unselected",Toast.LENGTH_LONG).show();
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
//Toast.makeText(context, "Reselected",Toast.LENGTH_LONG).show();
}
});
}