ViewPager pager;
private ArrayList<ModelClassPager> arrayOfPager;
pager1 = (ViewPager)findViewById(R.id.pager);
arrayOfPager = new ArrayList<ModelClass>();
arrayOfPager.add(new ModelClass(R.drawable.first));
arrayOfPager.add(new ModelClass(R.drawable.second));
arrayOfPager.add(new ModelClass(R.drawable.third));
arrayOfPager.add(new ModelClass(R.drawable.four));
PagerAdapter pagerAdapter = new PagerAdapter (MainActivity.this,arrayOfPager);
pagerAdapter .setAdapter(pagerAdapter);
Custom Adapter for View Pager:
PagerAdapter extends PagerAdapter {
LayoutInflater inflates;
ArrayList<ModelClass> arrayOfPager;
Context context;
public PagerAdapter (Context context,
ArrayList<ModelClass> arrayOfPager_) {
this.context = context;
this.arrayOfPager = arrayOfPager_;
inflates = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return arrayOfPager.size();
}
public Object instantiateItem(View collection, int position) {
ViewGroup container = null;
ImageView pgrImage;
View viewPgaer = inflates.inflate(R.layout.row_pager, null);
pgrImage = (ImageView)viewPgaer
.findViewById(R.id.pagerImage);
pgrImage.setImageResource(arrayOfPager.get(position).getPagerImg());
((ViewPager) collection).addView(viewPgaer, 0);
return viewPgaer;
}
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
public boolean isViewFromObject(View view, Object object) {
return view == (object);
}
public void finishUpdate(View arg0) {
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
}
Model Class (Gettor Settor Class):
public class ModelClassPager {
int pagerImg = 0;
public int getPagerImg() {
return pagerImg;
}
public void setPagerImg(int pagerImg) {
this.pagerImg = pagerImg;
}
public ModelClassPager( int pagerImg_) {
// TODO Auto-generated constructor stub
pagerImg = pagerImg_;
}
}
XML Design For View Pager:
android:id="@+id/pagesCustmLayoutMAIN"
android:layout_width="fill_parent"
android:layout_height="160dp" />