我没有经常使用 Fragments,现在我遇到了一些麻烦。在我的 MainActivity 中向右滑动时,我需要显示一个 ListFragment,所以我需要类似的东西:
我试图做的事情:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PageAdapter mPageAdapter = new PageAdapter(getSupportFragmentManager());
ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(mPageAdapter);
}
}
页面适配器:
public class PageAdapter extends FragmentPagerAdapter {
public PageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
PageListFrag f = new PageListFrag();
return f;
}
@Override
public int getCount() {
return 0;
}
@Override
public CharSequence getPageTitle(int position) {
return "Title";
}
}
列表片段:
public class PageListFrag extends ListFragment {
ArrayAdapter<String> mAdapter;
public PageListFrag() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> al = new ArrayList<String>();
mAdapter = new ArrayAdapter<String>(getActivity(),
R.layout.list_frag_item, al);
for (int i = 0; i < 20; i++) {
al.add("Item " + " " + Integer.toString(i));
mAdapter.notifyDataSetChanged();
}
setListAdapter(mAdapter);
}
}
我只看到一个空白屏幕,无法查看我的 ListFragment。我究竟做错了什么?