码迷,mamicode.com
首页 > 其他好文 > 详细

ViewPager的简单用法

时间:2015-03-18 21:43:24      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

1.首先要在layout写一个xml文件,写关于ViewPager的一个标签的xml文件

 activity_main.xml

<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

 

2.然后写你要滑动的view,每个view文件

view1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/tu1"
   
     >
     <!-- android:gravity="center_horizontal" -->
    <TextView 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="view1"
         android:textSize="28sp"
         android:id="@+id/textview"
         android:layout_gravity="center_horizontal"
        />
    

</LinearLayout>

 view2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/tu2"
   
     >
     <!-- android:gravity="center_horizontal" -->
    <TextView 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="view2"
         android:textSize="28sp"
         android:id="@+id/textview2"
         android:layout_gravity="center_horizontal"
        />
    

</LinearLayout>

 

view3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/tu3"
   
     >
     <!-- android:gravity="center_horizontal" -->
    <TextView 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="view3"
         android:textSize="28sp"
         android:id="@+id/textview3"
         android:layout_gravity="center_horizontal"
        />
    

</LinearLayout>

 

view4.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/tu4"
   
     >
     <!-- android:gravity="center_horizontal" -->
    <TextView 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="view4"
         android:textSize="28sp"
         android:id="@+id/textview4"
         android:layout_gravity="center_horizontal"
        />
    

</LinearLayout>

 

3、然后要把四个view (view1到4)用代码加载到ViewPager里面去,前提是要生成一个PagerAdapter,

    通过这个PagerAdapter把数据加载到ViewPager里面去

   MainActivity.java

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);
		
		LayoutInflater layoutInflater=getLayoutInflater().from(this);
		View view1=layoutInflater.inflate(R.layout.view1, null);
		View view2=layoutInflater.inflate(R.layout.view2, null);
		View view3=layoutInflater.inflate(R.layout.view3, null);
		View view4=layoutInflater.inflate(R.layout.view4, null);
		
		List<View> list=new ArrayList<View>();
		
		list.add(view1);
		list.add(view2);
		list.add(view3);
		list.add(view4);
		viewPager.setAdapter(new MyViewPagerAdapter(list));
		viewPager.setCurrentItem(0);
		
	}



}

 

    MyViewPagerAdapter.java

public class MyViewPagerAdapter extends PagerAdapter{
	List<View> list;
	public MyViewPagerAdapter (List<View> list){
		this.list=list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0 == arg1;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		// TODO Auto-generated method stub
		container.removeView(list.get(position));
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		// TODO Auto-generated method stub
		container.addView(list.get(position),0);
		return list.get(position);
	}

}

 

ViewPager的简单用法

标签:

原文地址:http://www.cnblogs.com/childhooding/p/4348691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!