一个RelativeLayout的范围内显示ViewPager(LinearLayout中不能被强制转换为android.widget.RelativeLayout) [英] Showing ViewPager within a RelativeLayout (LinearLayout cannot be cast to android.widget.RelativeLayout)

查看:2255
本文介绍了一个RelativeLayout的范围内显示ViewPager(LinearLayout中不能被强制转换为android.widget.RelativeLayout)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我ViewPager在我listview_item.xml包含一个RelativeLayout的范围内:

My ViewPager is contained within a RelativeLayout in my listview_item.xml:

<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寻呼机项目:

This is the pager item for the 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 {

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

}

在哪里可以我是想错了?

Where could I be going wrong?

推荐答案

您是铸造线布局到相对布局。

you are casting linear layout into the relative layout.

您使用

@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);
}

而不是,你必须用

instead of that you have to use

@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);
}

我不知道。但尝试一次。

I am not sure. But try once.

这篇关于一个RelativeLayout的范围内显示ViewPager(LinearLayout中不能被强制转换为android.widget.RelativeLayout)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆