Android开发中ViewPager实现导航页和引导页效果(总结)

ViewPager是一个视图滑动切换工具。可以用来做APP启动导航页,填充页面的话,需要自定义一个PagerAdapter数据适配器,和用ListView时有点类似。(ListView是自定义一个BaseAdapter数据适配器)

效果如下:

Android开发中ViewPager实现导航页和引导页效果(总结)_第1张图片

Android开发中ViewPager实现导航页和引导页效果(总结)_第2张图片

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;
    }

    @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的代码如下:




    

        
        

    

备注:四个页面的布局,自行设计即可。

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[]{"第一页", "第二页", "第三页", "第四页"};

    @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();
        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(  )方法,用来设置每个页面的标题。

你可能感兴趣的:(Android基本组件总结)