FragmentStatePageAdapter ViewPager 5选项卡使用单个片段

时间:2017-11-24 10:29:32

标签: android android-fragments tabs

我正在使用ViewPager Tablayout显示5个选项卡,当选择从服务器获取数据以显示时。获取数据的唯一区别是选定的选项卡。因此,有5个片段做同样的事情是多余的。因此,我已经实现了如下所示使用一个片段。

活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_news_feed);

    mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
    final ViewPager viewPager  = (ViewPager)findViewById(R.id.pager);
    setupViewPager(viewPager);

    TabLayout tabLayout = (TabLayout)findViewById(R.id.tabLayout);
    tabLayout.setupWithViewPager(viewPager);

    tabLayout.getTabAt(0).setIcon(R.drawable.blogging);
    tabLayout.getTabAt(1).setIcon(R.drawable.tanzania);
    tabLayout.getTabAt(2).setIcon(R.drawable.world);
    tabLayout.getTabAt(3).setIcon(R.drawable.football);
    tabLayout.getTabAt(4).setIcon(R.drawable.coins);
}

private void  setupViewPager(ViewPager viewPager){
    Bundle bundle = new Bundle();
    bundle.putString("edttext", "0");

    NewsFeedTab1 fragObj1 = new NewsFeedTab1();
    fragObj1.setArguments(bundle);

    Bundle bundle2 = new Bundle();
    bundle2.putString("edttext", "8,15");

    NewsFeedTab1 fragObj2 = new NewsFeedTab1();
    fragObj2.setArguments(bundle2);

    Bundle bundle3 = new Bundle();
    bundle3.putString("edttext", "9,10,13");

    NewsFeedTab1 fragObj3 = new NewsFeedTab1();
    fragObj3.setArguments(bundle3);

    Bundle bundle4 = new Bundle();
    bundle4.putString("edttext", "11");

    NewsFeedTab1 fragObj4 = new NewsFeedTab1();
    fragObj4.setArguments(bundle4);

    Bundle bundle5 = new Bundle();
    bundle5.putString("edttext", "12");

    NewsFeedTab1 fragObj5 = new NewsFeedTab1();
    fragObj5.setArguments(bundle5);

    SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
    adapter.addFragment(fragObj1, "Habari");
    adapter.addFragment(fragObj2, "Kitaifa");
    adapter.addFragment(fragObj3, "Kimataifa");
    adapter.addFragment(fragObj4, "Michezo");
    adapter.addFragment(fragObj5, "Biashara");

    viewPager.setAdapter(adapter);
}

问题是,当Pagedapter启动时,它会初始化选项卡位置0和1(从研究中看是正常的),这会使选项卡中的选项卡2产生数据。我试图实现setUserVisibleHint,但我没有看到任何区别。我猜setUserVisibleHint是一个合适的解决方案,当每个标签有多个片段时。

片段

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (getView() != null) {
        isViewShown = true;
        // fetchdata() contains logic to show data when page is selected mostly asynctask to fill the data

    } else {
        isViewShown = false;
    }
}        
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view= inflater.inflate(R.layout.fragment_news_feed_tab1, container, false);
    fragView = view;    
     currTab = getArguments().getString("edttext"); 
    if (!isViewShown) {
        loadNewsFeed(isBottom = "0", isTop = "0", lastStart = "0", lastStop = "0");
    }
}

如何消除点击标签上混淆的问题,因为我猜双重调用启动片段。

1 个答案:

答案 0 :(得分:0)

对于那些可能有类似问题的人。我在https://stackoverflow.com/a/20412875/2448688找到了解决方案。 Basicaly而不是在OnCreateView中调用volley,它在方法fragmentBecameVisible中调用,该方法在接口中实现,以通知片段是否可见并且此时仅调用Volley。