创建一个ViewPager列表:列表项空白

时间:2015-05-14 13:40:29

标签: android listview android-fragments android-listview

我想创建一个ViewPager的列表。

我使用由ListView管理的Adapter,其中为每个listview项目创建了一个viewPager。

这是主要活动(扩展FragmentActivity):

        list = (ListView)findViewById(R.id.listView1);

        titles = new String[]{"A","B","C","D","E","F","G","H"};
        descr = new String[]{"Lorem ipsum dolor sit amet","Lorem ipsum dolor sit amet","Lorem ipsum dolor sit amet","Lorem ipsum dolor sit amet","Lorem ipsum dolor sit amet","Lorem ipsum dolor sit amet","Lorem ipsum dolor sit amet","Lorem ipsum dolor sit amet"};
        FragmentManager fm = getSupportFragmentManager();
        adp = new ListAdapter(titles,descr,fm,context);

        list.setAdapter(adp);

ListAdapter:

public class ListAdapter extends BaseAdapter{

    Context c;
    String[] ts , ds;
    FragmentManager fm;
    List<Fragment> frags;
    Activity act;

    public ListAdapter(String[] _ts , String[] _ds , FragmentManager _fm ,Context _c){
        ts = _ts;
        ds = _ds;
        fm = _fm;
        c = _c;
    }

    @Override
    public int getCount() {
        return ts.length;
    }

    @Override
    public Object getItem(int position) {
        return ts[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null){
            LayoutInflater inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.item_list_pager, null);
            viewHolder = new ViewHolder();
            viewHolder.mPager = (ViewPager)convertView.findViewById(R.id.pager);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        frags = new ArrayList<Fragment>();
        frags.add(FragmentItemList.newInstance(ts[position],ds[position], c));
        frags.add(FragmentItemDelete.newInstance(ts[position],c));
        ScreenSlidePagerAdapter ad = new ScreenSlidePagerAdapter(fm,frags);
        viewHolder.mPager.setAdapter(ad);

        return convertView;
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{

        private List<Fragment> fragments;

        public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fragmentManager , List<Fragment> fragments) {
            super(fragmentManager);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return this.fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    public class ViewHolder{
        public ViewPager mPager;
    }

}

当我运行应用程序时,只有第一项正确创建,其他项目为空白,如下图

enter image description here

LogCat给了我这个错误:

05-14 15:30:32.816: W/View(18767): requestLayout() improperly called by android.support.v4.view.ViewPager{41f9e6f8 VFED.... ......ID 0,0-540,150 #7f070006 app:id/pager} during layout: running second layout pass

通常该错误是由ListView属性fastScrollEnabled引起的。我还没设置它,但无论如何我都禁用了它,但问题仍然存在。

任何建议?

0 个答案:

没有答案