布局
<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" > <android.support.v4.view.ViewPager
android:id="@+id/ViewPager1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/Container_PagerIndicator" >
</android.support.v4.view.ViewPager> <LinearLayout
android:id="@+id/Container_PagerIndicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/Container_bottom"
android:layout_gravity="bottom"
android:gravity="center_horizontal" > <ImageView
android:id="@+id/page0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null"
android:scaleType="matrix"
android:src="@drawable/page_now" /> <ImageView
android:id="@+id/page1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:contentDescription="@null"
android:scaleType="matrix"
android:src="@drawable/page" /> <ImageView
android:id="@+id/page2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:contentDescription="@null"
android:scaleType="matrix"
android:src="@drawable/page" /> <ImageView
android:id="@+id/page3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:contentDescription="@null"
android:scaleType="matrix"
android:src="@drawable/page" />
</LinearLayout> <LinearLayout
android:id="@+id/Container_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" > <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/register" >
</Button> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/login" >
</Button>
</LinearLayout> </RelativeLayout>
代码
package com.wangzhen.myviewpager; import java.util.ArrayList; import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject; import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.content.Context;
import android.os.Bundle; @ContentView(R.layout.activity_main)
public class MainActivity extends ActionBarActivity implements
OnPageChangeListener {
Context mContext;
LayoutInflater mInflater; @ViewInject(R.id.ViewPager1)
private ViewPager ViewPager1;
@ViewInject(R.id.page0)
private ImageView page0;
@ViewInject(R.id.page1)
private ImageView page1;
@ViewInject(R.id.page2)
private ImageView page2;
@ViewInject(R.id.page3)
private ImageView page3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
ViewUtils.inject(this);
mContext = this;
ViewPager1.setOnPageChangeListener(this);
InflateViewPager();
} /**
* 填充ViewPager数据
*/
private void InflateViewPager() { mInflater = LayoutInflater.from(mContext);
View view1 = mInflater.inflate(R.layout.pager1, null);
View view2 = mInflater.inflate(R.layout.pager2, null);
View view3 = mInflater.inflate(R.layout.pager3, null);
View view4 = mInflater.inflate(R.layout.pager4, null); // 将View装入List中
final ArrayList<View> views = new ArrayList<View>();
views.add(view1);
views.add(view2);
views.add(view3);
views.add(view4); // 填充Pager的数据适配器
PagerAdapter mAdapter = new PagerAdapter() { @Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
} @Override
public int getCount() {
return views.size();
} @Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView(views.get(position));
} @Override
public Object instantiateItem(View container, int position) {
((ViewPager) container).addView(views.get(position));
return views.get(position);
} }; ViewPager1.setAdapter(mAdapter);
} @Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub } @Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub } @Override
public void onPageSelected(int arg0) {
switch (arg0) {
case 0:
page0.setImageDrawable(getResources().getDrawable(
R.drawable.page_now));
page1.setImageDrawable(getResources().getDrawable(R.drawable.page));
page2.setImageDrawable(getResources().getDrawable(R.drawable.page));
page3.setImageDrawable(getResources().getDrawable(R.drawable.page));
break;
case 1:
page0.setImageDrawable(getResources().getDrawable(R.drawable.page));
page1.setImageDrawable(getResources().getDrawable(
R.drawable.page_now));
page2.setImageDrawable(getResources().getDrawable(R.drawable.page));
page3.setImageDrawable(getResources().getDrawable(R.drawable.page));
break;
case 2:
page0.setImageDrawable(getResources().getDrawable(R.drawable.page));
page1.setImageDrawable(getResources().getDrawable(R.drawable.page));
page2.setImageDrawable(getResources().getDrawable(
R.drawable.page_now));
page3.setImageDrawable(getResources().getDrawable(R.drawable.page));
break;
case 3:
page0.setImageDrawable(getResources().getDrawable(R.drawable.page));
page1.setImageDrawable(getResources().getDrawable(R.drawable.page));
page2.setImageDrawable(getResources().getDrawable(R.drawable.page));
page3.setImageDrawable(getResources().getDrawable(
R.drawable.page_now));
break; default:
break;
} } }