列表视图上的项目未显示

时间:2013-09-01 12:17:55

标签: android listview android-fragments

我的活动包含两个片段:

@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不是。我不知道为什么不:/

1 个答案:

答案 0 :(得分:0)

您似乎重写了FragmentPagerAdapter,在这种情况下,您不必重写 isViewFromObject 。尝试删除该方法。