我有一个包含两个片段的活动:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.eventlist);
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), getFragments());
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
EventListFragment eventListFragment = (EventListFragment)
EventListFragment.instantiate(this, EventListFragment.class.getName());
EventGridFragment eventGridFragment = (EventGridFragment)
EventGridFragment.instantiate(this, EventGridFragment.class.getName());
fList.add(eventListFragment);
fList.add(eventGridFragment);
return fList;
}
这是我的页面适配器:
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public android.support.v4.app.Fragment getItem(int i) {
return fragments.get(i);
}
和有问题的片段:
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View view = inflater.inflate(R.layout.eventlist ,container, false);
String[] x = new String[]{"AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC"};
ListView listView = (ListView) view.findViewById(R.id.listView);
ArrayAdapter<String> test = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1,x);
listView.setAdapter(test);
return view;
}
上述片段的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v4.view.ViewPager
android:id="@id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
片段都在被创建,但这个片段中的 ListView 不是。我不知道为什么不:/