ViewPager

Posted zqm-sau

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ViewPager相关的知识,希望对你有一定的参考价值。

之前我也写过ViewPager的用法(现在忘干净了),复习一遍:
布局activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    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=".MainActivity">

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

</android.support.constraint.ConstraintLayout>

list_view_pager.xml:

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

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

</android.support.constraint.ConstraintLayout>

MainActivity:

package com.fitsoft;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
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 

    ViewPager viewPager;

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

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager  = findViewById(R.id.myViewPager);

        //添加四个View(颜色不一样)
        
            View view = LayoutInflater.from(this).inflate(R.layout.list_view_pager, null);
            ImageView imageView = view.findViewById(R.id.itemImageView);
            imageView.setBackgroundColor(Color.RED);
            list.add(view);
        

        
            View view = LayoutInflater.from(this).inflate(R.layout.list_view_pager, null);
            ImageView imageView = view.findViewById(R.id.itemImageView);
            imageView.setBackgroundColor(Color.GREEN);
            list.add(view);
        

        
            View view = LayoutInflater.from(this).inflate(R.layout.list_view_pager, null);
            ImageView imageView = view.findViewById(R.id.itemImageView);
            imageView.setBackgroundColor(Color.BLUE);
            list.add(view);
        

        
            View view = LayoutInflater.from(this).inflate(R.layout.list_view_pager, null);
            ImageView imageView = view.findViewById(R.id.itemImageView);
            imageView.setBackgroundColor(Color.BLACK);
            list.add(view);
        

        //设置适配器
        viewPager.setAdapter(new PagerAdapter() 
            @Override
            public int getCount() 
                return list.size();
            

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object o) 
                return view == o;
            

            //初始化方法
            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) 
                container.addView(list.get(position));
                return list.get(position);
            

            //销毁方法
            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) 
                container.removeView(list.get(position));
            
        );


    

之前写过,不过多介绍,注意ViewPager是在v4包里的。
效果图:

技术图片

以上是关于ViewPager的主要内容,如果未能解决你的问题,请参考以下文章

转载---ViewPager,PagerAdapter,FragmentPagerAdapter和FragmentStatePagerAdapter的分析对比

Android开发笔记(二十一)横幅轮播页Banner

android ViewPager+Fragment 如何在ViewPager的Activity中获取Fragment中的控件对象