public class ViewPager_2Activity extends Activity
{
private List<View> listViews;
private ViewPager viewPager;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listViews = new ArrayList<View>();
listViews.add(View.inflate(getApplicationContext(), R.layout.layout1,
null));
listViews.add(View.inflate(getApplicationContext(), R.layout.layout2,
null));
listViews.add(View.inflate(getApplicationContext(), R.layout.layout3,
null));

viewPager = (ViewPager) findViewById(R.id.v_Pager);
viewPager.setAdapter(new MyPagerAdapter(listViews));
viewPager.setCurrentItem(0);


View view = listViews.get(2);
TextView textView = (TextView) view.findViewById(R.id.text_3);
textView.setText("10");
Button button = (Button) view.findViewById(R.id.button_3);
button.setOnClickListener(new OnClickListener()
{

public void onClick(View v)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "你点击了按钮",
Toast.LENGTH_SHORT).show();
}
});
}

private class MyPagerAdapter extends PagerAdapter
{

private List<View> mListView;

private MyPagerAdapter(List<View> list)
{
// TODO Auto-generated method stub
this.mListView = list;
}

@Override
/**这个方法,是从ViewGroup中移出当前View**/
public void destroyItem(View container, int position, Object object)
{
// TODO Auto-generated method stub
((ViewGroup) container ).removeView(mListView.get(position));
}

@Override
public void finishUpdate(View view)
{
// TODO Auto-generated method stub

}

@Override
/**这个方法,是获取当前窗体界面数**/
public int getCount()
{
// TODO Auto-generated method stub
return mListView.size();
}

@Override
/**这个方法,return一个对象,这个对象表明了PagerAdapter适配器选择哪个对象*放在当前的ViewPager中**/
public Object instantiateItem(View container, int position)
{
// TODO Auto-generated method stub
((ViewGroup) container).addView(mListView.get(position), 0);
return mListView.get(position);
}

@Override
/**这个方法,在帮助文档中原文是could be implemented as return view == object,*也就是用于判断是否由对象生成界面**/
public boolean isViewFromObject(View view, Object object)
{
// TODO Auto-generated method stub
return view == (object);
}

@Override
public void restoreState(Parcelable state, ClassLoader loader)
{
// TODO Auto-generated method stub

}

@Override
public Parcelable saveState()
{
// TODO Auto-generated method stub
return null;
}

@Override
public void startUpdate(View v)
{
// TODO Auto-generated method stub

}

}
}

main.xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

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


</LinearLayout>

其余三个布局:layout_1.xml,layout_2.xml,layout_3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15"
android:textSize="150sp"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/button_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第1页面"
android:textSize="30sp"
></Button>

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="25"
android:textSize="150sp"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/button_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第2页面"
android:textSize="30sp"
></Button>

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="35"
android:textSize="150sp"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/button_3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第3页面"
android:textSize="30sp"
></Button>

</LinearLayout>


效果图:

ViewPager使用方法及子View的获取_ide

ViewPager页面拖动到最前或最后的时候回弹效果

看到一哥们用ViewPager写的左右滑动的屏幕,像UC浏览器那样,连接:

http://www.eoeandroid.com/thread-92508-1-1.html

但是当滑到最左端或者最右端就不能滑动了,感觉用户体验不如UC的,所以就试着弄了下

继承ViewPager类以后,在左面和右面各增加个空View,然后在onPageSelected方法中

@Override
public void onPageSelected(int arg0)
{
// TODO Auto-generated method stub
System.out.println("onPageSelected = " + arg0);
if (arg0 == 0)
mViewPaper.setCurrentItem(arg0 + 1);
else if (arg0 == mViewList.size() - 1)
mViewPaper.setCurrentItem(arg0 - 1);
}

ViewPager的onPageChangeListener里面的一些方法参数:


onPageSelected(int arg0){

}

arg0是表示你当前选中的页面,这事件是在你页面跳转完毕的时候调用的。

public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub


} arg0 ==1的时候表示正在滑动,arg0==2的时候表示滑动完毕了,arg0==0的时候表示什么都没做,就是停在那。



public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}表示在前一个页面滑动到后一个页面的时候,在前一个页面滑动前调用的方法。