2017-02-10 78 views
0

我正在嘗試將活動轉換爲片段,但我遇到了問題getSupportFragmentManager無法解析,我的getDrawable也是如此。我在這裏失去了從哪裏出發。任何有關代碼示例的幫助將不勝感激。如何將活動轉換爲使用getSupportFragmentManager的片段

我試圖片段:

public class HomeFragment extends Fragment { 

    private MaterialViewPager mViewPager; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.activity_home, container, false); 

     mViewPager = (MaterialViewPager) rootView.findViewById(R.id.materialViewPager); 

     mViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) { 

      @Override 
      public Fragment getItem(int position) { 
       switch (position % 4) { 
        //case 0: 
        // return RecyclerViewFragment.newInstance(); 
        //case 1: 
        // return RecyclerViewFragment.newInstance(); 
        //case 2: 
        // return WebViewFragment.newInstance(); 
        default: 
         return RecyclerViewFragment.newInstance(); 
       } 
      } 

      @Override 
      public int getCount() { 
       return 4; 
      } 

      @Override 
      public CharSequence getPageTitle(int position) { 
       switch (position % 4) { 
        case 0: 
         return "Projects"; 
        case 1: 
         return "Missions"; 
        case 2: 
         return "Areas"; 
        case 3: 
         return "Flights"; 
       } 
       return ""; 
      } 
     }); 

     mViewPager.setMaterialViewPagerListener(new MaterialViewPager.Listener() { 

      @Override 
      public HeaderDesign getHeaderDesign(int page) { 
       switch (page) { 
        case 0: 
         return HeaderDesign.fromColorResAndDrawable(
           R.color.colorPrimaryDark, 
           getDrawable(R.drawable.kenburns_cityview)); 
        case 1: 
         return HeaderDesign.fromColorResAndDrawable(
           R.color.colorPrimaryDark, 
           getDrawable(R.drawable.kenburns_glendalough)); 
        case 2: 
         return HeaderDesign.fromColorResAndDrawable(
           R.color.colorPrimaryDark, 
           getDrawable(R.drawable.kenburns_goldengatebridge)); 
        case 3: 
         return HeaderDesign.fromColorResAndDrawable(
           R.color.colorPrimaryDark, 
           getDrawable(R.drawable.kenburns_dubai)); 
       } 

       return null; 
      } 
     }); 

     mViewPager.getViewPager().setOffscreenPageLimit(mViewPager.getViewPager().getAdapter().getCount()); 
     mViewPager.getPagerTitleStrip().setViewPager(mViewPager.getViewPager()); 

     return rootView; 
    } 

} 

從這個(原)活動時間:

public class MainActivity extends AppCompatActivity { 

    private MaterialViewPager mViewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mViewPager = (MaterialViewPager) findViewById(R.id.materialViewPager); 
     FoldingTabBar tabBar = (FoldingTabBar) findViewById(R.id.folding_tab_bar); 

     mViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) { 

      @Override 
      public Fragment getItem(int position) { 
       switch (position % 4) { 
        //case 0: 
        // return RecyclerViewFragment.newInstance(); 
        //case 1: 
        // return RecyclerViewFragment.newInstance(); 
        //case 2: 
        // return WebViewFragment.newInstance(); 
        default: 
         return RecyclerViewFragment.newInstance(); 
       } 
      } 

      @Override 
      public int getCount() { 
       return 4; 
      } 

      @Override 
      public CharSequence getPageTitle(int position) { 
       switch (position % 4) { 
        case 0: 
         return "Projects"; 
        case 1: 
         return "Missions"; 
        case 2: 
         return "Areas"; 
        case 3: 
         return "Flights"; 
       } 
       return ""; 
      } 
     }); 

     mViewPager.setMaterialViewPagerListener(new MaterialViewPager.Listener() { 

      @Override 
      public HeaderDesign getHeaderDesign(int page) { 
       switch (page) { 
        case 0: 
         return HeaderDesign.fromColorResAndDrawable(
           R.color.colorPrimaryDark, 
           getDrawable(R.drawable.kenburns_cityview)); 
        case 1: 
         return HeaderDesign.fromColorResAndDrawable(
           R.color.colorPrimaryDark, 
           getDrawable(R.drawable.kenburns_glendalough)); 
        case 2: 
         return HeaderDesign.fromColorResAndDrawable(
           R.color.colorPrimaryDark, 
           getDrawable(R.drawable.kenburns_goldengatebridge)); 
        case 3: 
         return HeaderDesign.fromColorResAndDrawable(
           R.color.colorPrimaryDark, 
           getDrawable(R.drawable.kenburns_dubai)); 
       } 

       return null; 
      } 
     }); 

     mViewPager.getViewPager().setOffscreenPageLimit(mViewPager.getViewPager().getAdapter().getCount()); 

    } 
} 

回答

1

獲取支持FragmentManagerAppCompatActivity的方法,所以在您的支持片段你」 d需要getActivity().getSupportFragmentManager()。您可以用類似的方式參考getDrawable方法。

0

如果它是活動的直接子節點,您也可以調用getFragmentManager