ViewPager控件之PagerAdapter适配器

原文地址:ViewPager控件之PagerAdapter适配器_viewpageradapter_撩得Android一次心动的博客-CSDN博客

一、ViewPager的基本用法
  1、简介
        ViewPager可以实现多个界面的左右滑动。ViewPager最典型的应用场景主要包 括引导页导航,轮转广告和页面菜单。

        ViewPager最早出自4.0版本,为了兼容低版本安卓设备,谷歌官方给我们提供了 一个的软件包android.support.v4.view。

  2、ViewPager的基本用法
1、在主界面中放置 ViewPager控件

2、创建用于滑动切 换的视图

3、将创建好的多个 视图装入View数组

4、定义PagerAdapter适配 器管理要显示的页

5、绑定适配器和 ViewPager控件

二、PagerAdapter适配器
1、PagerAdapter就是用于“将多个页面填充到ViewPager”的一个基类适配器。

2、在实际使用中,往往会自定义一个具体的适配器,并让它继承PagerAdapter。

3、继承PagerAdapter基类之后,往往需要在程序中重写相应的方法。

 三、代码部分
  1、activity_main.xml
   添加ViewPager控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v4.view.ViewPager>
</LinearLayout>
  2、page1.xml;page2.xml;page3.xml
   添加三个界面,并且添加各自图片用于滑动显示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/view1"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/view2"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/view3"/>
</RelativeLayout>
  3、MainActivity.java
package com.example.zsviewpager;
 
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;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    //1.创建View对象,以及View数组
    private View view1,view2,view3;
    private List<View> viewList;
    private MyPagerAdapter myPagerAdapter;
    private ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //2.将前面定义的滑动布局转换成view对象
        LayoutInflater  inflater = getLayoutInflater();
        view1 = inflater.inflate(R.layout.page1,null);
        view2 = inflater.inflate(R.layout.page2,null);
        view3 = inflater.inflate(R.layout.page3,null);
        //3.将前面定义的view对象存入view数组
        viewList = new ArrayList<View>();
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        //4.定义自定义适配器
        myPagerAdapter = new MyPagerAdapter(viewList);
        //5.将适配器和ViewPager控件进行绑定
        viewPager = findViewById(R.id.viewpager);
        viewPager.setAdapter(myPagerAdapter);
    }
}
  4、MyPagerAdapter.java
     这是继承于PagerAdapter的

package com.example.zsviewpager;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
public class MyPagerAdapter extends PagerAdapter {
    //1.定义view数组,用来接收从Activity传递过来的view数组
    private List<View> pageList;
    //2.定义MyPagerAdapter类的构造方法,接收数据
    public MyPagerAdapter(List<View> viewList){
        this.pageList = viewList;
    }
    @Override
    public int getCount() {
        return pageList.size();
    }
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        //将当前位置的view视图删除
        container.removeView(pageList.get(position));
    }
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        //将当前视图添加到container中
        container.addView(pageList.get(position));
        //返回当前的view视图
        return  pageList.get(position);
    }
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值