Search code examples
androidandroid-layoutandroid-viewpagerimageview

Make imageview with viewpager


I'm trying to make a swipe imageview window in my app so I use viewpager. But android studio wrote about 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);

I know that it is another view. but i don`t know how connect right imageView. Please, help.

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

}

Solution

  • You are using the wrong layout in your MyPager class

    You need to use R.layout.pager_item instead of R.layout.authorization

    Also, please change findViewById() code also

    Use this

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

    instead of this

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