是否可以在另一个片段中添加带HORIZONTAL VIEW SWIPING WITH TABS的片段?
答案 0 :(得分:0)
我用这段代码解决了:
public class FragListaJogos extends Fragment{
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private View view;
private FragmentActivity myContext;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
view= inflater.inflate(R.layout.layout_lista_jogos, container, false);
mSectionsPagerAdapter = new SectionsPagerAdapter(myContext.getSupportFragmentManager());
mViewPager = (ViewPager) view.findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
return view;
}
@Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return ("Teste1").toUpperCase(l);
case 1:
return ("Teste2").toUpperCase(l);
case 2:
return ("Teste3").toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_lista_jogos, container, false);
return rootView;
}
}

基本上我像这样重写onAttach方法:
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}