MainActivity.java
public class MainActivity extends SherlockFragmentActivity {
static MyListfragment mf;
ViewPager mpager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mf = new MyListfragment();
ft.add(android.R.id.content,mf).commit();
}}
MyListfragment.java
public class MyListfragment extends SherlockListFragment {
MyDisplayfragment tempFrag;
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Connector.selection = position;
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
if(position==0)
{
tempFrag=new MyDisplayfragment();
tempFrag.setLayout(R.layout.law);
subFragmentAdapter mAdapter = new subFragmentAdapter(getChildFragmentManager());
tempFrag.setViewPagerAdapter(mAdapter);
}
ft.replace(android.R.id.content,tempFrag);
ft.addToBackStack(null);
ft.commit();
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;
setListAdapter(new ArrayAdapter<String>(getActivity(),
layout, Connector.list));
}}
MyDisplayfragment.java
public class MyDisplayfragment extends SherlockFragment {
int resource;
FragmentPagerAdapter mAdapter;
ViewPager v;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(Connector.selection==-1)
resource=R.layout.displayfragment;
return inflater.inflate(resource, container, false);
}
public void setViewPagerAdapter(FragmentPagerAdapter x)
{
mAdapter=x;
}
public void setLayout(int layout)
{
resource=layout;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
ViewPager pager=(ViewPager) view.findViewById(R.id.mpager);
pager.setAdapter(mAdapter);
}}
子片段.java
public final class subFragment extends SherlockFragment {
private int layout;
public static subFragment newInstance(int content) {
subFragment fragment = new subFragment();
fragment.layout=content;
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(layout,container,false);
}}
subFragmentAdapter.java
public class subFragmentAdapter extends FragmentPagerAdapter{
int[] LAYOUT={R.layout.cccc,R.layout.aaaa,R.layout.bbb};
private int mCount;
public subFragmentAdapter(FragmentManager fm) {
super(fm);
mCount = LAYOUT.length;
}
@Override
public Fragment getItem(int position) {
return subFragment.newInstance(LAYOUT[position]);
}
@Override
public int getCount() {
return mCount;
}
}
连接器.java
public class Connector {
static int selection = -1;
static String list[] = { "Law" };}
上述项目中
1)MyListfragment.java 用于导航
2)MyDisplayfragment.java 用于根据 MyListFragment 中的选择进行显示 3)Connector.java 用于上述两个片段之间的通信
4)subFragment.java 用于用片段填充 Viewpager
5)subFragmentAdapter.java 是 Viewpager 的适配器
当我运行这个项目时,viewpager 似乎是空白的,并且没有显示任何子片段,调试后我看到 subFragment.java 的 onCreateView() 方法没有被调用。我的问题是为什么不调用 onCreateView() 方法