在RelativeLayout中显示ViewPager(LinearLayout不能转换为android.widget.



My ViewPager包含在我的listview_item.xml中的RelativeLayout中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="15dp"
            android:layout_gravity="center_horizontal">
            <android.support.v4.view.ViewPager
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/view_pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop">
            </android.support.v4.view.ViewPager>
    </RelativeLayout>
</LinearLayout>

这是ViewPager:的寻呼机项目

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/image_view"
        android:src="@drawable/template_swipeup"
        android:scaleType="centerCrop" />
</LinearLayout>

这就是我设置适配器的地方:

private void init(Context context) {
    View view = inflate(context, R.layout.listview_item, this);
    view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    ViewPager viewPager;
    CustomPagerAdapter adapter;
    viewPager = (ViewPager) findViewById(R.id.view_pager);
    adapter = new CustomPagerAdapter(context);
    viewPager.setAdapter(adapter);
}

这就是我得到的错误:

java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.RelativeLayout

编辑:

公共类CustomPagerAdapter扩展PagerAdapter{

private int[] image_resources = {R.drawable.template_shirt_1000_1500_overlay, R.drawable.template_leggings_1000_1500_overlay};
private Context ctx;
private LayoutInflater layoutInflater;
public CustomPagerAdapter(Context ctx) {
    this.ctx = ctx;
}
@Override
public int getCount() {
    return image_resources.length;
}
@Override
public boolean isViewFromObject(View view, Object o) {
    return (view == (RelativeLayout) o);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
    layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View item_view = layoutInflater.inflate(R.layout.pager_item, container, false);
    ImageView imageview = (ImageView) item_view.findViewById(R.id.image_view);
    imageview.setImageResource(image_resources[position]);
    container.addView(item_view);
    return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((RelativeLayout) object);
}

}

我哪里会出错?

您正在将线性布局转换为相对布局。

您使用

@Override
public boolean isViewFromObject(View view, Object o) {
return (view == (RelativeLayout) o);
}
@Override
public void destroyItem(ViewGroup container, int position, Object   object) {
container.removeView((RelativeLayout) object);
}

相反,你必须使用

@Override
public boolean isViewFromObject(View view, Object o) {
return (view == (LinearLayout) o);
}
@Override
public void destroyItem(ViewGroup container, int position, Object   object) {
container.removeView((LinearLayout) object);
}

我不确定。但是试一次。

更改以下方法:

 @Override
 public boolean isViewFromObject(View view, Object o) {
      return (view == (RelativeLayout) o);
 }

收件人:

@Override
 public boolean isViewFromObject(View view, Object o) {
      return (view == (LinearLayout) o);
 }

还有下面一个:

 @Override
 public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((RelativeLayout) object);
 }

收件人:

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
   container.removeView((LinearLayout) object);
}

最新更新