ViewPage的初使用(一)

来源:互联网 发布:smo算法 python实现 编辑:程序博客网 时间:2024/05/20 02:23

ViewPager是android扩展包v4包中的类,这个类可以让用户左右切换当前的view

1)ViewPager类直接继承了ViewGroup类,所有它是一个容器类,可以在其中添加其他的view类。

2)ViewPager类需要一个PagerAdapter适配器类给它提供数据。

3)ViewPager经常和Fragment一起使用,并且提供了专门的FragmentPagerAdapter和FragmentStatePagerAdapter类供Fragment中的ViewPager使用。

废话不多说,先上demo代码 实现三个view左右切换
1、先是整体代码

public class ViewPageActivity extends Activity{    private ViewPager viewPager;    private View view1,view2,view3;    private List<View> viewlist;//view 集合    private PagerAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.viewpage_activity);    viewPager = (ViewPager) findViewById(R.id.mviewpage);    LayoutInflater inflater = getLayoutInflater();    view1 = inflater.inflate(R.layout.my_viewpage1, null);    view2 = inflater.inflate(R.layout.my_viewpage2, null);    view3 = inflater.inflate(R.layout.my_viewpage3, null);    //添加要滑动的view    viewlist = new ArrayList<View>();    viewlist.add(view1);    viewlist.add(view2);    viewlist.add(view3);    setViewPage();    viewPager.setAdapter(adapter);}public void setViewPage(){        adapter = new PagerAdapter() {        @Override        public boolean isViewFromObject(View arg0, Object arg1) {            return arg0==arg1 ;        }        @Override        //返回要滑动的VIew的个数        public int getCount() {            return viewlist.size();        }        @Override        //从当前container中删除指定位置(position)的View        public void destroyItem(ViewGroup container, int position,                Object object) {                        container.removeView(viewlist.get(position));        }        @Override        //将当前视图添加到container中,第二:返回当前View        public Object instantiateItem(ViewGroup container, int position) {            container.addView(viewlist.get(position));            return viewlist.get(position);        }    };      }}

2、接下来贴布局文件
(1)主布局 viewpage_activity.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.support.v4.view.ViewPager        android:id="@+id/mviewpage"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center" /></LinearLayout>

(2)接下来是三个要滑动的view的xml布局
第一个view的布局 my_viewpage1.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" >    <ImageView        android:id="@+id/img_1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_gravity="center"        android:src="@drawable/db" /></LinearLayout>

第二个view的布局 my_viewpage2.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" >    <ImageView        android:id="@+id/img_2"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_gravity="center"        android:src="@drawable/ic_launcher" /></LinearLayout>

第三个view的布局 my_viewpage3.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:background="#ffff00"      android:orientation="vertical" ></LinearLayout>

三个要滑动的view都比较简单,就放了了一个imagview控件,。代码有具体注释,这里就不废话了,先手写的比较渣,莫怪!

1 0