2014-03-01 108 views
0

我有一個ViewPager的片段。 ViewPager根據用戶觸發的操作進行更新。那麼,當片段被重新創建,ViewPager的頁面不會更新,甚至不會顯示這個問題!當我滑過其他頁面然後回到那個頁面時,它會加載?Android ViewPager中片段不更新

這裏的片段:

public static class CardFrontFragment extends Fragment { 

    public CardFrontFragment() { 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = null; 

     rootView = inflater.inflate(R.layout.activity_dashboard, container, false); 

     TextView textviewLocation = (TextView)rootView.findViewById(R.id.textview_location); 
     TextView textviewConditions = (TextView)rootView.findViewById(R.id.textview_points); 
     ViewPager viewPagerDashboard = (ViewPager)rootView.findViewById(R.id.viewPager_Dashboard); 

     if(overviewData != null) { 

      textviewLocation.setText(overviewData.getLocation()); 
      textviewPoints.setText(overviewData.getPoints()); 

      viewPagerDashboard.setAdapter(adapterSectionsPager); 
      adapterSectionsPager.notifyDataSetChanged(); 
      viewPagerDashboard.destroyDrawingCache(); 

      textviewLocation.setText(overviewData.getWeathersLocation()); 
      textviewPoints.setText(overviewData.getWeathersConditions()); 
     } 

     return rootView; 
    } 
} 

這裏是適配器:

public class SectionsPagerAdapter extends FragmentStatePagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public android.support.v4.app.Fragment getItem(int position) { 
     return DashboardSectionFragment.create 
       (uiContext, position, overviewData, forecastData, hourlyData, radarData); 
    } 

    @Override 
    public int getCount() { 
     // Show 4 total pages: 
     return 4; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     Locale l = Locale.getDefault(); 
     switch (position) { 
     case 0: 

      return getString(R.string.title_section_1).toUpperCase(l); 
     case 1: 

      return getString(R.string.title_section_2).toUpperCase(l); 
     case 2: 

      return getString(R.string.title_section_3).toUpperCase(l); 
     case 3: 
      // Radar Page: 
      return getString(R.string.title_section_4).toUpperCase(l); 
     } 
     return null; 
    } 
} 

聽說我上延伸FragmentPagerAdapter正確的FragmentStatePagerAdapter。然而沒有任何工作。我已經在ViewPager重新創建時調用了ViewPager和適配器的許多方法,但它仍然不會首先加載,直到它出現在屏幕上然後重新開始...

謝謝!

回答

1

嘗試重寫下面這個和更新viewpagerpageradapater例如根據碎片的具體位置

@Override 
public void onPageScrolled(int arg0, float arg1, int arg2) { 
    viewPager.invalidate(); 
    pagerAdapter.notifyDataSetChanged(); 

} 

,否則你可以做以下

@Override 
public void onPageSelected(int arg0) { 
    if(int == 2)// In which specifi fragment you want to update the view 
    { 
     viewPager.invalidate(); 
     pagerAdapter.notifyDataSetChanged(); 
    } 
} 

我希望這會幫助你

0

嘗試更新視圖片段變得對用戶可見時如下:

public class MyFragment extends Fragment 
    @Override 
    public void setMenuVisibility(final boolean visible) { 
     super.setMenuVisibility(visible); 
     if (visible) { 
      **//Update views here** 
     } 
    }