【问题标题】:Make imageview with viewpager使用 viewpager 制作图像视图
【发布时间】:2020-07-24 05:07:55
【问题描述】:

我正在尝试在我的应用程序中创建一个滑动图像视图窗口,因此我使用了 viewpager。但是 android studio 写了关于 java.lang.ClassCastException: androidx.viewpager.widget.ViewPager cannot be cast to android.widget.ImageView at this string:

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

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

MyPager 类:

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

}

【问题讨论】:

    标签: android android-layout android-viewpager imageview


    【解决方案1】:

    您在 MyPager 类中使用了错误的布局

    您需要使用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;
        }
    

    【讨论】:

      【解决方案2】:

      在“instantiateItem”函数中编辑通胀行

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多