使用 viewpager 制作 imageview

Make imageview with viewpager

我想在我的应用程序中制作滑动图像视图 window,所以我使用了 viewpager。但是 android studio 写了关于 java.lang.ClassCastException 的内容:androidx.viewpager.widget.ViewPager 不能转换为 android.widget.ImageView 这个字符串:

ImageView imageView = view.findViewById(R.id.view_pager);

我知道那是另一种观点。但我不知道如何连接正确的 imageView。请帮忙

MyPager class:

  import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.viewpager.widget.PagerAdapter;

public class MyPager extends PagerAdapter {
    private Context context;
    public MyPager(Context context) {
        this.context = context;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.authorization, null);
        ImageView imageView = view.findViewById(R.id.view_pager);
        imageView.setImageDrawable(context.getResources().getDrawable(getImageAt(position)));
        container.addView(view);
        return view;
    }

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

    @Override
    public int getCount() {
        return 7;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return object == view;
    }
    private int getImageAt(int position) {
        switch (position) {
            case 0:
                return R.drawable.logo0;
            case 1:
                return R.drawable.logo1;
            case 2:
                return R.drawable.logo2;
            case 3:
                return R.drawable.logo3;
            case 4:
                return R.drawable.logo4;
            case 5:
                return R.drawable.logo5;
            case 6:
                return R.drawable.logo6;
            default:
                return R.drawable.logo0;
        }
    }

}

编辑 'instantiateItem' 函数中的 inflation 行

View view = LayoutInflater.from(context).inflate(R.layout.authorization, null);

View view = LayoutInflater.from(context).inflate(R.layout.pager_item, null);

您在 MyPager class

中使用了错误的布局

您需要使用 R.layout.pager_item 而不是 R.layout.authorization

此外,请也更改 findViewById() 代码

使用这个

@Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.pager_item, null);
        ImageView imageView = view.findViewById(R.id.image);
        imageView.setImageDrawable(context.getResources().getDrawable(getImageAt(position)));
        container.addView(view);
        return view;
    }

而不是这个

 @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.authorization, null);
        ImageView imageView = view.findViewById(R.id.view_pager);
        imageView.setImageDrawable(context.getResources().getDrawable(getImageAt(position)));
        container.addView(view);
        return view;
    }