我正在尝试将活动转换为片段,但我遇到getSupportFragmentManager
无法解决的问题以及我的getDrawable
问题。我迷失了从这里出发的地方。任何有关代码示例的帮助都将不胜感激。
我的尝试片段:
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());
}
}
答案 0 :(得分:1)
获取支持FragmentManager
是AppCompatActivity
的方法,因此在您的支持片段中,您需要getActivity().getSupportFragmentManager()
。您可以以类似的方式引用getDrawable
方法。
答案 1 :(得分:0)
如果片段是活动的直接子项,您也可以调用片段的 getFragmentManager 。