代码之家  ›  专栏  ›  技术社区  ›  Anh Vinh Huỳnh

Android-将图像从drawable加载到ViewPager

  •  0
  • Anh Vinh Huỳnh  · 技术社区  · 6 年前

    我有一个包含三页的ViewPager。每个页面都有一个ImageViews,稍后我将从drawable集中加载图像。

    这是我的代码:

    在片段上:

    @BindView(R.id.viewPager)    
    ViewPager mViewPager;
    
    ...
    
    private List<Integer> getIntroResourceIds() {
        List<Integer> introResourceIds = new ArrayList<>();
        introResourceIds.add(R.layout.layout_intro_step1);
        introResourceIds.add(R.layout.layout_intro_step2);
        introResourceIds.add(R.layout.layout_intro_step3);
        return introResourceIds;
    }
    

    在每个版面介绍中,我都有一个 ImageView 具有不同的id。例如 layout_intro_step_1 :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="10">
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="3">
    
            <ImageView
                android:id="@+id/quickpay_intro_step3_image"
                style="@style/IntroImage" />
        </FrameLayout>
    </LinearLayout>
    

    问题是,当用户单击下一页时,我无法为每个版面介绍设置图像视图。请帮我解决这个问题。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Fazal Hussain    6 年前

    创建自定义寻呼机适配器

    class CustomPagerAdapter extends PagerAdapter {
    
     int[] mResources = {
    
            R.drawable.first,
    
            R.drawable.second,
    
            R.drawable.third,
    
            R.drawable.fourth,
    
            R.drawable.fifth,
    
            R.drawable.sixth
    
    };
    
    
        Context mContext;
    
        LayoutInflater mLayoutInflater;
    
     
    
        public CustomPagerAdapter(Context context) {
    
            mContext = context;
    
            mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        }
    
     
    
        @Override
    
        public int getCount() {
    
            return mResources.length;
    
        }
    
     
    
        @Override
    
        public boolean isViewFromObject(View view, Object object) {
    
            return view == ((LinearLayout) object);
    
        }
    
     
    
        @Override
    
        public Object instantiateItem(ViewGroup container, int position) {
    
            View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
    
     
    
            ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
    
            imageView.setImageResource(mResources[position]);
    
     
    
            container.addView(itemView);
    
     
    
            return itemView;
    
        }
    
        @Override
    
        public void destroyItem(ViewGroup container, int position, Object object) {
    
            container.removeView((LinearLayout) object);
    
        }
    
    }
    

    在活动设置视图寻呼机适配器中

    mCustomPagerAdapter = new CustomPagerAdapter(this);
    
    mViewPager = (ViewPager) findViewById(R.id.pager);
    
    mViewPager.setAdapter(mCustomPagerAdapter);