アクティビティをフラグメントに変換しようとしていますが、getSupportFragmentManager
の問題を解決できず、私のgetDrawable
も同様です。私はここからどこへ行くのか分からない。コード例の助けをいただければ幸いです。この(元)Activityからアクティビティを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;
}
}
:
マイしようとしましたフラグメントは、サポートFragmentManager
を取得
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());
}
}