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<View> viewLists; public
MyPagerAdapter() { super(); } public MyPagerAdapter(ArrayList<View> viewLists)
{ super(); this.viewLists = viewLists; } @Override public int getCount() {
return viewLists.size(); } @Override public boolean isViewFromObject(@NonNull
View view, @NonNull Object object) { return view == object; } @Override public
Object instantiateItem(ViewGroup container, int position) {
container.addView(viewLists.get(position)); return viewLists.get(position); }
@Override public void destroyItem(ViewGroup container, int position, Object
object) { container.removeView(viewLists.get(position)); } }
2、布局文件activity_main.xml的代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v4.view.ViewPager android:id="@+id/main_vpGuiding"
android:layout_width="match_parent" android:layout_height="match_parent"> <!--
这个布局是为了给每一个页面添加标题 --> <!--<android.support.v4.view.PagerTabStrip
android:id="@+id/pagertitle" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="top" />-->
</android.support.v4.view.ViewPager> </LinearLayout>
备注:四个页面的布局,自行设计即可。

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<View>
viewArrayList; private ArrayList<String> titleArrayList; private String[]
strTitles = new String[]{"第一页", "第二页", "第三页", "第四页"}; @Override protected 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<View>(); 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<View>(); titleArrayList = new ArrayList<String>(); 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(  )方法,用来设置每个页面的标题。

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信