ViewPager是一个视图滑动切换工具。可以用来做APP启动导航页,填充页面的话,需要自定义一个PagerAdapter数据适配器,和用ListView时有点类似。(ListView是自定义一个BaseAdapter数据适配器)
效果如下:
1、数据适配器MyPagerAdapter.java的代码如下:
package com.deepreality.viewpaperdemo;import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;import java.util.ArrayList;public class MyPagerAdapter extends PagerAdapter {private ArrayList viewLists;public MyPagerAdapter() {super();}public MyPagerAdapter(ArrayList viewLists) {super();this.viewLists = viewLists;}@Overridepublic int getCount() {return viewLists.size();}@Overridepublic boolean isViewFromObject(@NonNull View view, @NonNull Object object) {return view == object;}@Overridepublic Object instantiateItem(ViewGroup container, int position) {container.addView(viewLists.get(position));return viewLists.get(position);}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {container.removeView(viewLists.get(position));}
}
2、布局文件activity_main.xml的代码如下:
备注:四个页面的布局,自行设计即可。
3、数据处理操作MainActivity.java的代码如下:
package com.deepreality.viewpaperdemo;import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {private ViewPager vpGuiding;private MyPagerAdapter myPagerAdapter;private MyPagerWithTitleAdapter myPagerWithTitleAdapter;private ArrayList viewArrayList;private ArrayList titleArrayList;private String[] strTitles = new String[]{"第一页", "第二页", "第三页", "第四页"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);vpGuiding = findViewById(R.id.main_vpGuiding);//viewPagerWithTitleLookLike();viewPagerNormalLookLike();}//默认效果的public void viewPagerNormalLookLike() {//List集合赋值,用于给适配器传参数viewArrayList = new ArrayList();LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);viewArrayList.add(layoutInflater.inflate(R.layout.item_viewpaper_one, null, false));viewArrayList.add(layoutInflater.inflate(R.layout.item_viewpaper_two, null, false));viewArrayList.add(layoutInflater.inflate(R.layout.item_viewpaper_three, null, false));viewArrayList.add(layoutInflater.inflate(R.layout.item_viewpaper_four, null, false));//适配器赋值myPagerAdapter = new MyPagerAdapter(viewArrayList);//绑定数据适配器vpGuiding.setAdapter(myPagerAdapter);}//带有标题效果的public void viewPagerWithTitleLookLike() {//List集合赋值,用于给适配器传参数viewArrayList = new ArrayList();titleArrayList = new ArrayList();LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);viewArrayList.add(layoutInflater.inflate(R.layout.item_viewpaper_one, null, false));viewArrayList.add(layoutInflater.inflate(R.layout.item_viewpaper_two, null, false));viewArrayList.add(layoutInflater.inflate(R.layout.item_viewpaper_three, null, false));viewArrayList.add(layoutInflater.inflate(R.layout.item_viewpaper_four, null, false));for (String str : strTitles) {titleArrayList.add(str);}//适配器赋值myPagerWithTitleAdapter = new MyPagerWithTitleAdapter(viewArrayList, titleArrayList);//绑定数据适配器vpGuiding.setAdapter(myPagerWithTitleAdapter);}
}
备注:其中包含两种样式的方法。(不带标题和带标题)
另外,带标题的话,需要重写PagerAdapter的getPageTitle( )方法,用来设置每个页面的标题。