Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Image slides using ViewPager

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 521
    Comment on it

    To show slides of images, we use ViewPager in android that provided by the support library.

    First of all add images that we want to show in drawable folder.

    Then create array of these images.

      private int[] mResources = {
                R.drawable.slide_1,
                R.drawable.slide_2,
                R.drawable.slide_3,
                R.drawable.slide_4,
                R.drawable.slide_5,
    
        };


    in xml file create viewpager like this

     <android.support.v4.view.ViewPager
                android:id="@+id/pager"
    
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >
    
            </android.support.v4.view.ViewPager>


    Then in Java, initialize the ViewPager and create Adapter to set on it.
     

    mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
    ImagePagerAdapter mImagePagerAdapter;
    mImagePagerAdapter = new ImagePagerAdapter(this);
    mViewPager.setAdapter(mImagePagerAdapter);
    
       class ImagePagerAdapter extends PagerAdapter {
    
            Context mContext;
            LayoutInflater mLayoutInflater;
    
            public ImagePagerAdapter(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);
            }
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: