Android View Pager Example
Take dependencies in buid.gradle file
compile 'com.android.support:design:23.1.1'
MainActivity.java
package com.example.admin.imageview; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.Menu; public class MainActivity extends Activity { ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.viewPager); PagerAdapter adapter = new CustomAdapter(MainActivity.this); viewPager.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.admin.imageview.MainActivity"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/viewPager"> </android.support.v4.view.ViewPager> </RelativeLayout>
CustomAdapter.java
package com.example.admin.imageview; import android.app.Activity; import android.content.Context; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; public class CustomAdapter extends PagerAdapter { Context context; int[] imageId = {R.drawable.image1, R.drawable.image2, R.drawable.image3}; public CustomAdapter(Context context){ this.context = context; } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub LayoutInflater inflater = ((Activity)context).getLayoutInflater(); View viewItem = inflater.inflate(R.layout.image_item, container, false); ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView); imageView.setImageResource(imageId[position]); ((ViewPager)container).addView(viewItem); return viewItem; } @Override public int getCount() { // TODO Auto-generated method stub return imageId.length; } @Override public boolean isViewFromObject(View view, Object object) { // TODO Auto-generated method stub return view == ((View)object); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub ((ViewPager) container).removeView((View) object); } }
image_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/imageView"/> </LinearLayout>
Now your viewpager is ready to run. Thank you!
No comments:
Post a Comment