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

Showing ViewPager within a RelativeLayout (LinearLayout cannot be cast to android.widget.RelativeLayout)

我的 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

编辑:

public class 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);
}