首页 > 代码库 > Android ViewPager 用法

Android ViewPager 用法

Android ViewPager 用法

场景:一般第一次打开应用程序时,程序会有一个提示页来给展现应用程序都有哪些功能;或者程序更新时,又更新哪些新特性,都可以使用ViewPager

Demo

描述:实现三个界面的相互切换,页面颜色分别为红,绿,蓝

activity_main:

主界面只添ViewPager这一个控件

android:overScrollMode="never" 作用是当页面已经滑动到最左边或最右时,去掉阴影
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never">

    </android.support.v4.view.ViewPager>
</LinearLayout>

item_image_view:

ViewPager里面显示内容的布局文件,里面只有一个ImageView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

MainActivity:

package com.zy.viewpager;

import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
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 android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

   //存放view List
<View> viewList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewList = new ArrayList<>(); {
       //获取item_image_view布局文件 View view
= LayoutInflater.from(this).inflate(R.layout.item_image_view, null);
       //根据item_image_view布局文件找到ImageView ImageView imageView
= (ImageView) view.findViewById(R.id.item_image);
       //给ImageView添加背景色 imageView.setBackgroundColor(Color.RED);
       //添加到list里边 viewList.add(view); } { View view
= LayoutInflater.from(this).inflate(R.layout.item_image_view, null); ImageView imageView = (ImageView) view.findViewById(R.id.item_image); imageView.setBackgroundColor(Color.GREEN); viewList.add(view); } { View view = LayoutInflater.from(this).inflate(R.layout.item_image_view, null); ImageView imageView = (ImageView) view.findViewById(R.id.item_image); imageView.setBackgroundColor(Color.BLUE); viewList.add(view); }      //获取ViewPager ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);      //为ViewPager 设置适配器,new一个匿名内部类,并实现里面抽象方法,并且重写instantiateItem和destroyItem方法 viewPager.setAdapter(new PagerAdapter() {
       //返回数据总长度 @Override
public int getCount() { return viewList.size(); }        // @Override public boolean isViewFromObject(View view, Object object) { return view == object; }        //装载当前页显示的内容(view) @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(viewList.get(position)); return viewList.get(position); }        //销毁已经滑过去的view @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(viewList.get(position)); } }); } }

 最终效果:

技术分享技术分享技术分享

 

Android ViewPager 用法