SimpeViewPager.java
package com.example.viewpager;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
// This app simplify the demo app in Android document for understanding ViewPager
// without involving unrelated factors.
public class SimpleViewPager extends Activity {
PagerAdapter mAdapter;
ViewPager mPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new TextAdapter();
mPager.setAdapter(mAdapter);
Button button = (Button) findViewById(R.id.goto_first);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPager.setCurrentItem(0);
}
});
button = (Button) findViewById(R.id.go_last);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPager.setCurrentItem(mAdapter.getCount() - 1);
}
});
}
public static class TextAdapter extends PagerAdapter {
static final int TOTAL_PAGES = 5;
@Override
public int getCount() {
return TOTAL_PAGES;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
TextView itemView = new TextView(container.getContext());
int viewId = new Random().nextInt(Integer.MAX_VALUE) + 1;
itemView.setId(viewId);
itemView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
itemView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 180);
itemView.setText(String.valueOf(position));
ViewGroup.LayoutParams layoutParams = new ViewPager.LayoutParams();
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
container.addView(itemView, layoutParams);
// The return object is the key object associated with the item view just added.
// There are many ways to set up the mapping between the item view and its key
// Some examples use the view itself as the key, Here we generate an Integer
// which equals to the view id for testing purpose.
Integer itemKey = new Integer(viewId);
return itemKey;
}
@Override
public boolean isViewFromObject(View view, Object o) {
// We know how to check if object o is the key object of view.
// The relationship between two is set in initantiateItem()
Integer itemKey = (Integer) o;
int viewId = itemKey.intValue();
return view.getId() == viewId;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
Integer itemKey = (Integer) object;
int viewId = itemKey.intValue();
View itemView = container.findViewById(viewId);
container.removeView(itemView);
}
}
}
The layout file <?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">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
/>
<LinearLayout android:orientation="horizontal"
android:gravity="center"
android:measureWithLargestChild="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
<Button android:id="@+id/goto_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First"
/>
<Button android:id="@+id/go_last"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last"
/>
</LinearLayout>
</LinearLayout>