码迷,mamicode.com
首页 > 其他好文 > 详细

ViewPager

时间:2019-09-15 22:31:52      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:extends   rri   res   ada   find   tde   view   roi   encoding   

之前我也写过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

标签:extends   rri   res   ada   find   tde   view   roi   encoding   

原文地址:https://www.cnblogs.com/zqm-sau/p/11524665.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!