android.support.v4.view.ViewPager $ LayoutParams无法强制转换为android.view.ViewGroup $ MarginLayoutParams

时间:2014-08-28 09:10:40

标签: android android-activity android-fragments

mPagerAdapter = new PagerAdapter() {
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

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

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager)container).removeView(views.get(position));
        }


        @Override
        public Object instantiateItem(View container, int position) {
            ((ViewPager)container).addView(views.get(position));
            return views.get(position);
        }
    };

        mTabPager.setAdapter(mPagerAdapter);
  

在My Fragment创建之后,我调用函数:

public void AddView(View v, int index)
{
    views.add(index, v);
    mPagerAdapter.notifyDataSetChanged();
}
  

但它报告android.support.v4.view.ViewPager $ LayoutParams无法转换为   android.view.ViewGroup $ MarginLayoutParams

1 个答案:

答案 0 :(得分:1)

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager)container).removeView(views.get(position));
    }


    @Override
    public Object instantiateItem(View container, int position) {
        ((ViewPager)container).addView(views.get(position));
        return views.get(position);
    }

试试这个,使用你的ViewPager实例来替换演员范围“((ViewPager)容器)”,像这样,mViewPager.removeView(views.get(position));