2015-10-16 163 views
0

我有三個頁面查看器和操作欄欄。當我嘗試切換回已選擇的選項卡時,不顯示任何數據,但logcat指示數據已從服務器中提取並添加到數組列表中。切換回標籤

有關爲什麼數據現在不顯示的任何建議?我做了大量的研究,但仍然無法理解發生了什麼。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_tabs); 

     viewPager = (ViewPager) findViewById(R.id.pager); 
     Log.v("Main-onCreate-viewPager", viewPager.toString()); 
     actionBar = getSupportActionBar(); 

     mAdapter = new TabsPagerAdapter(getSupportFragmentManager()); 
     viewPager.setAdapter(mAdapter); 

     actionBar.setHomeButtonEnabled(false); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Adding Tabs 
     for (String tab_name : tabs) { 
      actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this)); 
      Log.v("Main", "tab added"); 
     } 


     /** 
     * on swiping the viewpager make respective tab selected 
     **/ 
     viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

      @Override 
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
       Log.v("onPageScrolled-position", Integer.toString(position)); 
       Log.v("onPageScrolled-position", Float.toString(positionOffset)); 
       Log.v("onPageScrolled-position", Integer.toString(positionOffsetPixels)); 
      } 

      public void onPageSelected(int position) { 
       // on changing the page 
       // make respected tab selected 
       Log.v("onPageSelected", "onPageSelected"); 
       actionBar.setSelectedNavigationItem(position); 
       Log.v("onPageSelected-position", Integer.toString(position)); 
     } 


      public void onPageScrollStateChanged(int state) { 
       Log.v("StateChanged", "OnPageScrollStateChanged"); 
       Log.v("statechanged-state", Integer.toString(state)); 

      } 
     }); 
    } 
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { 
      Log.v("Main", "Tab REselected"); 
      Log.v("onTabReselected", tab.toString()); 
      Log.v("onTabReselected", ft.toString()); 

      if(tab.getPosition() == 0){ 
       Log.v("onTabReselected", "ImagesFragment"); 
       Fragment frag; 
       FragmentManager fm1 = Main.this.getSupportFragmentManager(); 
       FragmentTransaction ft1 = fm1.beginTransaction(); 
       frag = new ImagesFragment(); 
       //frag = new FeaturesFragment(); 
       ft1.replace(R.id.pager, frag); 
       ft1.commit(); 
       frag.setUserVisibleHint(true); 
      } 

      if (tab.getPosition() == 1) { 
       Log.v("onTabReselected", "FeaturesFragment"); 
       Fragment frag; 
       FragmentManager fm1 = Main.this.getSupportFragmentManager(); 
       FragmentTransaction ft1 = fm1.beginTransaction(); 
       frag = new FeaturesFragment(); 
       ft1.replace(R.id.pager, frag); 
       ft1.commit(); 
       frag.setUserVisibleHint(true); 

      } 

      if (tab.getPosition() == 2) { 
       Log.v("onTabReselected", "OptionsFragment"); 
       Fragment frag; 
       FragmentManager fm1 = Main.this.getSupportFragmentManager(); 
       FragmentTransaction ft1 = fm1.beginTransaction(); 
       frag = new OptionsFragment(); 
       ft1.replace(R.id.pager, frag); 
       ft1.commit(); 
       frag.setUserVisibleHint(true); 

      } 
     } 

     public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { 
      // on tab selected 
      // show respected fragment view 
      Log.v("onTabSelected", tab.toString()); 
      Log.v("onTabSelected", ft.toString()); 
      Log.v("tab position1", Integer.toString(tab.getPosition())); 
      viewPager.setOffscreenPageLimit(0); 
      //viewPager.setCurrentItem(tab.getPosition()); //null pointer exception here 
      tab.select(); //no null pointer exception, but no show data when swiping tabs 
      Log.v("onTabSelected", tab.toString()); 

      Log.v("tab position", Integer.toString(tab.getPosition())); 

      if (tab.getPosition() == 0) { 

       Fragment frag; 
       FragmentManager fm1 = Main.this.getSupportFragmentManager(); 
       FragmentTransaction ft1 = fm1.beginTransaction(); 
       frag = new ImagesFragment(); 
       ft1.replace(R.id.pager, frag); 
       ft1.commit(); 
       frag.setUserVisibleHint(true); 
      } 

       if (tab.getPosition() == 1) { 

        Fragment frag; 
        FragmentManager fm1 = Main.this.getSupportFragmentManager(); 
        FragmentTransaction ft1 = fm1.beginTransaction(); 
        frag = new FeaturesFragment(); 
        ft1.replace(R.id.pager, frag); 
        ft1.commit(); 
        frag.setUserVisibleHint(true); 

       } 

       if (tab.getPosition() == 2) { 
        Fragment frag; 
        FragmentManager fm1 = Main.this.getSupportFragmentManager(); 
        FragmentTransaction ft1 = fm1.beginTransaction(); 
        frag = new OptionsFragment(); 
        ft1.replace(R.id.pager, frag); 
        ft1.commit(); 
        frag.setUserVisibleHint(true); 

       } 
      } 


     public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) { 
      Log.v("Main", "Tab UNselected"); 
      Log.v("onTabUnselected", tab.toString()); 
      Log.v("onTabUnselected", ft.toString()); 
    } 




public class TabsPagerAdapter extends FragmentStatePagerAdapter { 

    public TabsPagerAdapter(FragmentManager fm) { 

     super(fm); 
    } 

    @Override 
    public Fragment getItem(int index) { 
     Log.v("index", Integer.toString(index)); 
     switch (index) { 
      case 0: 
       Log.v("TabsPagerAdapter", "case 0: ImagesFragment"); 
       return new ImagesFragment(); 
      case 1: 
       Log.v("TabsPagerAdapter", "case 1: FeaturesFragment"); 
       return new FeaturesFragment(); 
      case 2: 
       Log.v("TabsPagerAdapter", "case 2: OptionsFragment"); 
       return new OptionsFragment(); 
     } 

     return null; 
    } 



    @Override 
    public int getCount() { 
     // get item count - equal to number of tabs 
     return 3; 
    } 
} 
+0

你試過我的解決方案嗎? –

回答

0

使用FragmentStatePagerAdapter替代TabsPagerAdapter。

+0

請詳細說明。我有一個名爲TabsPagerAdapter的類,它擴展了FragmentStatePagerAdapter。代碼現已公佈在上面。 – JohnWilliams

+0

在此放置適配器類的代碼 –

+0

該帖子已更新。 – JohnWilliams

0

這可能會幫助你,我也有這個問題,我有4個選項卡,並切換它們重新設置適配器使用這個代碼後創建視圖,

viewPager.setAdapter(mAdapter); 
viewPager.setOffscreenPageLimit(5); //this one.! 

任何數量限制,你可以有,但最好有由於內存定製數量較低,在oncreate()方法中使用它(如果Activity)。