java - 在RelativeLayout中显示ViewPager(LinearLayout无法转换为android.widget.RelativeLayout)

标签 java android android-viewpager

我的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)类 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);
}

我不确定。但尝试一次。

关于java - 在RelativeLayout中显示ViewPager(LinearLayout无法转换为android.widget.RelativeLayout),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33864186/

相关文章:

java - 如何将框架布局的 y 值设置为屏幕高度的 3/4?

java - 从深度递归父子关系构建 JSON

java - 如何通过现有元素(不是 xpath)查找子(​​不是所有后代)元素

java - 无法从 ResultSet 获取值来填充 Java 中的 jLabel

Android 自定义形状按钮

正在进行的通话中的 Android 干扰

android - 如何在 Kotlin 中实现一个可以处理不同大小单元格的回收器 View ?

android - 当我移动到相邻页面之外时,带有 TabLayout 的 ViewPager 不显示来自 recyclerview 的列表

java - viewpager 中的 fragment 转换问题

android - 如何在 MainActivity 页面使用 ViewPager 按钮?