ViewPager 详解(一)——基本入门

来源:互联网 发布:电视柜淘宝 编辑:程序博客网 时间:2024/06/05 19:22

ViewPager。它是google SDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换

在主布局中放入Viewpager

activity_main.xml

<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/viewpager"        android:layout_width="match_parent"        android:layout_height="wrap_content"/></LinearLayout>

新建3个要进行切换的布局:layout1,layout2,layout3

layout1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="fill_parent"              android:layout_height="fill_parent"              android:background="#ff0000"              android:orientation="vertical"></LinearLayout>

layout2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="fill_parent"              android:layout_height="fill_parent"              android:background="#00ff00"              android:orientation="vertical"></LinearLayout>

layout3.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="fill_parent"              android:layout_height="fill_parent"              android:background="#0000ff"              android:orientation="vertical"></LinearLayout>

MainActivity.xml

import android.app.Activity;import android.os.Bundle;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import java.util.ArrayList;import java.util.List;public class MainActivity extends Activity {    private View view1,view2,view3;    private ViewPager viewPager;    private List<View> viewList;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        viewPager = (ViewPager)findViewById(R.id.viewpager);        LayoutInflater layoutInflater = getLayoutInflater();        view1 = layoutInflater.inflate(R.layout.layout1,null);        view2 = layoutInflater.inflate(R.layout.layout2,null);        view3 = layoutInflater.inflate(R.layout.layout3,null);        viewList = new ArrayList<View>();        viewList.add(view1);        viewList.add(view2);        viewList.add(view3);        PagerAdapter pagerAdapter = new PagerAdapter() {            @Override            public int getCount() {                return viewList.size();            }            @Override            public boolean isViewFromObject(View view, Object object) {                return view == object;            }            @Override            public void destroyItem(ViewGroup container, int position,                                    Object object) {                container.removeView(viewList.get(position));            }            @Override            public Object instantiateItem(ViewGroup container, int position) {                container.addView(viewList.get(position));                return viewList.get(position);            }        };        viewPager.setAdapter(pagerAdapter);    }}

对其中PageAdapter作说明:

//getCount():返回要滑动的VIew的个数@Override            public int getCount() {                return viewList.size();            }
//destroyItem():从当前container中删除指定位置(position)的View;@Override            public void destroyItem(ViewGroup container, int position,                                    Object object) {                container.removeView(viewList.get(position));            }
//instantiateItem():做了两件事,第一:将当前视图添加到container中,第二:返回当前View @Override            public Object instantiateItem(ViewGroup container, int position) {                container.addView(viewList.get(position));                return viewList.get(position);            }
//下一篇讲 @Override            public boolean isViewFromObject(View view, Object object) {                return view == object;            }

总结自:启航大神的博客http://blog.csdn.net/harvic880925/article/details/38453725

原创粉丝点击