ViewPager初识

来源:互联网 发布:网络结婚主持台词 编辑:程序博客网 时间:2024/06/15 17:47

现在一般的app都是ViewPager作为启动页面,ViewPager和Fragement做主页
今天就来做一个简单的ViewPager
这里写图片描述
效果图
这里写图片描述


写一个引导页轮播需要
1.界面上创建一个viewPager
2.把要加载的页面方法哦Viewpagerd list中,吧布局文件转化为对象
2.创建一个pageradapter
重写pageradapter中的方法


创建一个ViewPager

<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"                tools:context="com.feng.viewpagertest.MainActivity">    <android.support.v4.view.ViewPager        android:id="@+id/vp"        android:layout_height="match_parent"        android:layout_width="match_parent"        >    </android.support.v4.view.ViewPager>    <LinearLayout        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:orientation="horizontal"        android:background="#ffcacb"        android:layout_marginBottom="20dp"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true">        <Button            android:id="@+id/bt2"            android:layout_height="wrap_content"            android:layout_width="wrap_content"            android:text="prev"            />        <Button            android:id="@+id/bt1"            android:layout_height="wrap_content"            android:layout_width="wrap_content"            android:text="next"            />    </LinearLayout></RelativeLayout>

创建一个list 把要放到ViewPager中的View放入list中

 private List<View>list=new ArrayList<>();

把布局文件转换为对象

 LayoutInflater layoutInflater= LayoutInflater.from(this); list.add(layoutInflater.inflate(R.layout.page_1,null)); list.add(layoutInflater.inflate(R.layout.page_2,null)); list.add(layoutInflater.inflate(R.layout.page_3,null));

创建MyAdapter继承PagerAdapter重写4个构造方法

private class MyAdapter extends PagerAdapter{        @Override        public int getCount() {            return list.size();        }        //object 是list.get(position);        @Override        public boolean isViewFromObject(View view, Object object) {            //如果要显示的view和object是同一个对象则显示view            return view==object;        }        //相当于listview中的getView        @Override        public Object instantiateItem(ViewGroup container, int position) {            //把要显示的View添加到container            container.addView(list.get(position));            //吧要显示的view作为返回值            return list.get(position);        }        @Override        public void destroyItem(ViewGroup container, int position, Object object) {            container.removeView(list.get(position));        }    }

返回上一个ViewPager

bt2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                int position=mViewPager.getCurrentItem();                position--;                if(position<0){                    position=position+list.size();                }                mViewPager.setCurrentItem(position);            }        });

到下一个Viewpager

 bt1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                int position=mViewPager.getCurrentItem();                position++;                position=position%list.size();                mViewPager.setCurrentItem(position);            }        });

重点
得到当前位置

  int position=mViewPager.getCurrentItem();

通过位置设置显示

 mViewPager.setCurrentItem(position);

全部代码
MainActivity

public class MainActivity extends AppCompatActivity {    ViewPager mViewPager;    Button bt1,bt2;    private List<View>list=new ArrayList<>();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        LayoutInflater layoutInflater= LayoutInflater.from(this);        list.add(layoutInflater.inflate(R.layout.page_1,null));        list.add(layoutInflater.inflate(R.layout.page_2,null));        list.add(layoutInflater.inflate(R.layout.page_3,null));        init();        initCilck();    }    private void initCilck() {        // 下一个        bt1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                int position=mViewPager.getCurrentItem();                position++;                position=position%list.size();                mViewPager.setCurrentItem(position);            }        });//        上一个        bt2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                int position=mViewPager.getCurrentItem();                position--;                if(position<0){                    position=position+list.size();                }                mViewPager.setCurrentItem(position);            }        });    }    private void init() {        mViewPager=(ViewPager)findViewById(R.id.vp);        bt1=(Button)findViewById(R.id.bt1);        bt2=(Button)findViewById(R.id.bt2);        mViewPager.setAdapter(new MyAdapter());    }    private class MyAdapter extends PagerAdapter{        @Override        public int getCount() {            return list.size();        }        //object 是list.get(position);        @Override        public boolean isViewFromObject(View view, Object object) {            //如果要显示的view和object是同一个对象则显示view            return view==object;        }        //相当于listview中的getView        @Override        public Object instantiateItem(ViewGroup container, int position) {            //把要显示的View添加到container            container.addView(list.get(position));            //吧要显示的view作为返回值            return list.get(position);        }        @Override        public void destroyItem(ViewGroup container, int position, Object object) {            container.removeView(list.get(position));        }    }}

mainActivity的布局

<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"                tools:context="com.feng.viewpagertest.MainActivity">    <android.support.v4.view.ViewPager        android:id="@+id/vp"        android:layout_height="match_parent"        android:layout_width="match_parent"        >    </android.support.v4.view.ViewPager>    <LinearLayout        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:orientation="horizontal"        android:background="#ffcacb"        android:layout_marginBottom="20dp"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true">        <Button            android:id="@+id/bt2"            android:layout_height="wrap_content"            android:layout_width="wrap_content"            android:text="prev"            />        <Button            android:id="@+id/bt1"            android:layout_height="wrap_content"            android:layout_width="wrap_content"            android:text="next"            />    </LinearLayout></RelativeLayout>

page_1, page_2, page_3,
布局很简答,就不用写了