2016-08-10 53 views
1

我正在使用FragmentStatePagerAdapter實現ViewPager。代碼示例如下。訪問方向變化的片段視圖

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { 

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

    @Override 
    public Fragment getItem(int position) { 

     Log.d("--GetItem", "---"); 

     switch (position) { 

      case 0: 
       return ScreenSlidePageFragment.newInstance(b1); 
      case 1: 
       return ScreenSlidePageFragment.newInstance(b2); 
      case 2: 
       return ScreenSlidePageFragment.newInstance(b3); 
      default: 
       return null; 
     } 

    } 

    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     Log.d("--InstItem", "--"); 
     return super.instantiateItem(container, position); 

    } 

    @Override 
    public int getCount() { 
     return NUM_PAGES; //set as 3 
    } 


} 

ScreenSlidePageFragment如下。

public class ScreenSlidePageFragment extends Fragment implements View.OnClickListener{ 


    Bundle b; 

    public ScreenSlidePageFragment() { 

    } 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     Log.d("----onCreateFrgmnt--", "11"); 

     super.onCreate(savedInstanceState); 



    } 


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

     Log.d("----onCreateViewFrgmnt-", "11"); 

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

     return rootView; 

    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 

     super.onViewCreated(view, savedInstanceState); 

     Log.d("----onViewCreatedFrgmnt", "11"); 


     b = getArguments(); 

     TextView tv_city = (TextView) view.findViewById(R.id.city_field); 
     TextView tv_update = (TextView) view.findViewById(R.id.updated_field); 
     TextView tv_day = (TextView) view.findViewById(R.id.day); 

     tv_day.setText("DAY " + String.valueOf(b.getInt("day"))); 
     tv_city.setText(b.getString("city")); 

     tv_update.setText("Last update on : " + b.getString("date")); 

    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

    } 

    public static ScreenSlidePageFragment newInstance(Bundle myBundle) { 

     ScreenSlidePageFragment f = new ScreenSlidePageFragment(); 
     f.setArguments(myBundle); 
     return f; 
    } 
} 

MainActivity,我想處理的方向變化。我使用下面的一段代碼。

if (savedInstanceState != null) { 

    setContentView(R.layout.activity_main); 

    myViewPager = (MyCustomViewPager) findViewById(R.id.pager); 


    b1 = savedInstanceState.getBundle("Fragment1"); 
    b2 = savedInstanceState.getBundle("Fragment2"); 
    b3 = savedInstanceState.getBundle("Fragment3"); 



    myViewPager.setOffscreenPageLimit(NUM_PAGES - 1); 

    myPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); 

    myViewPager.setAdapter(myPagerAdapter); 

} 

片段重新創建方向更改。 onCreateView以及onViewCreated正在被調用(檢查LogCat)。當我嘗試更新片段中的視圖時出現問題。對於更新我使用下面的代碼:

FragmentStatePagerAdapter adapter = (FragmentStatePagerAdapter) myViewPager.getAdapter(); 

ScreenSlidePageFragment fragment1 = (ScreenSlidePageFragment) adapter.instantiateItem(myViewPager, 0); 
ScreenSlidePageFragment fragment2 = (ScreenSlidePageFragment) adapter.instantiateItem(myViewPager, 1); 
ScreenSlidePageFragment fragment3 = (ScreenSlidePageFragment) adapter.instantiateItem(myViewPager, 2); 

但是,當我打電話fragment1.getView(),則返回null。但是,當我不改變方向時,相同的代碼運行良好。

如何獲取定位更改的片段視圖?我檢查了一些發佈的解決方案,他們說要將碎片保存在數組中,但這些解決方案對我來說並不合適。我所放的代碼是否無法完成任務?謝謝。

回答

2

只要您不更新片段(只發生在方向更改上),您的代碼就可以正常運行。

但是,您的更新代碼將始終返回null對於getView(),因爲視圖尚未創建。 instantiateItem()將在調用時創建一個新的Fragment,尚未添加到您的應用程序中,並且此類所有生命週期回調(包括onCreateView(...)onViewCreated(...))尚未調用。

由於正確更新ViewPager的片段的複雜性,我會引用您的出色existing answer。您還可以在this question中找到更多詳細信息。