• Android学习笔记 23. ViewPager


    Android学习笔记

    Android基础开发——ViewPager

    23. ViewPager

    页面滑动效果

    23.1 页面布局

    layout1.xml

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff00ffff"
        android:orientation="vertical"
        >
    
        <TextView
            android:textSize="30sp"
            android:text="layout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    layout2.xml

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff00ff00"
        android:orientation="vertical"
        >
    
        <TextView
            android:textSize="30sp"
            android:text="layout2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    layout3.xml

    
    
    
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    23.2 创建ViewPager

    activity_main.xml

    
    
        
        
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    23.3 创建适配器Adapter
    package com.dingjiaxiong.myviewpager;
    
    import android.view.View;
    import android.view.ViewGroup;
    
    import androidx.annotation.NonNull;
    import androidx.viewpager.widget.PagerAdapter;
    
    import java.util.List;
    
    public class MyAdapter extends PagerAdapter {
        
        //构造方法
        private List<View> mListView;
    
        public MyAdapter(List<View> mListView) {
            this.mListView = mListView;
        }
    
        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) { //将给定位置的view添加到viewgroup容器中,并显示出来;返回一个key值,与view一一对应
            container.addView(mListView.get(position),0);
            return mListView.get(position);
        }
    
        @Override
        public int getCount() { //获得ViewPager中有多少个View
            return mListView.size();
        }
    
        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { //判断key与view是否相等对应
            return view == object;
        }
        
        //销毁
        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(mListView.get(position));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    23.4 设置视图数据

    MainActivity.java

    package com.dingjiaxiong.myviewpager;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.viewpager.widget.ViewPager;
    
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            LayoutInflater lf = getLayoutInflater().from(this);
            View view1 = lf.inflate(R.layout.layout1, null);
            View view2 = lf.inflate(R.layout.layout2, null);
            View view3 = lf.inflate(R.layout.layout3, null);
    
            //添加到集合中
            List<View> viewList = new ArrayList<>();
            viewList.add(view1);
            viewList.add(view2);
            viewList.add(view3);
    
            ViewPager viewPager = findViewById(R.id.vp);
    
            MyAdapter myAdapter = new MyAdapter(viewList);
            viewPager.setAdapter(myAdapter);
    
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    运行

    在这里插入图片描述

  • 相关阅读:
    小林图解系统-二.硬件结构 2.6什么是软中断?
    【QT】信号和槽机制
    [Unity] 实现AssetBundle资源加载管理器
    php后端+JQuery+Ajax简单表单提交
    社区老给我推Canvas,于是我也学习Canvas做了个简历编辑器
    #力扣:26. 删除有序数组中的重复项@FDDLC
    神级编程网站,堪称程序员的充电站,我给你找好了不能错过
    C4D遇到的动力学模拟问题怎么办?看完本文就知道
    【Gazebo入门教程】第六讲 控制器插件的编写与配置(下)
    EA&UML日拱一卒 总目录
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126259024