Android:ViewPager详解

本文介绍了如何在Android应用中使用ViewPager实现页面滑动切换。首先展示了三个不同颜色背景的页面布局,接着演示了如何在XML中创建ViewPager,并在Java代码中获取其对象。然后创建了一个自定义的PagerAdapter,但`isViewFromObject`方法返回了错误的值,这会导致适配器无法正常工作。最后,文章提及了在主方法中调用Adapter,但未提供完整代码。

准备3个页面

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

    <TextView
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="页面1"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffff00"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="页面2"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ff00ffff"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="页面3"/>
</LinearLayout>

创建ViewPager

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

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

Java代码中获取ViewPager对象

package com.example.viewpager01;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

        ViewPager viewPager = findViewById(R.id.view);
    }
}

创建Adapter

在这里插入图片描述

package com.example.viewpager01;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.List;

public class ViewAdapter extends PagerAdapter {

    private List<View> myListView;

    public ViewAdapter(List<View> myListView) {
        this.myListView = myListView;
    }

    @Override
    public int getCount() {
        return myListView.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return false;
    }
}

在main方法中调用Adapter

@Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return false;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        container.addView(myListView.get(position),0);
        return myListView.get(position);
    }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值