首页 > 其他分享 >viewPager2页面的切换

viewPager2页面的切换

时间:2023-04-13 15:13:59浏览次数:43  
标签:color add titles 切换 viewPager2 import android public 页面

使用流程:

  1.定义ViewPager

  2.为ViewPager创建Adapter

ViewPagerAdapter

package com.example.viewpagerandfragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public  class ViewPagerAdapter extends  RecyclerView.Adapter<ViewPagerAdapter.ViewPagerViewHolder>{
        private List<String> titles = new ArrayList<>();
        private List<Integer> color = new ArrayList<>();
        public ViewPagerAdapter(){
            titles.add("苹果");
            titles.add("香蕉");
            titles.add("apple");
            titles.add("banana");
            titles.add("葡萄");
            titles.add("哈密瓜");
            titles.add("吃");
            titles.add("就");
            titles.add("完");
            titles.add("了");
            color.add(R.color.white);
            color.add(R.color.purple_200);
            color.add(R.color.red);
            color.add(R.color.purple_500);
            color.add(R.color.white);
            color.add(R.color.purple_200);
            color.add(R.color.red);
            color.add(R.color.white);
            color.add(R.color.purple_200);
            color.add(R.color.red);



        }

        @NonNull
        @Override
        public ViewPagerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewPagerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pager,parent,false));

        }

        @Override
        public void onBindViewHolder(@NonNull ViewPagerViewHolder holder, int position) {
            holder.textView.setText(titles.get(position));
            holder.relativeLayout.setBackgroundResource(color.get(position));
        }

        @Override
        public int getItemCount() {
            return 10;
        }
        class  ViewPagerViewHolder extends RecyclerView.ViewHolder{

            TextView textView ;
            RelativeLayout relativeLayout;
            public ViewPagerViewHolder(@NonNull View itemView) {
                super(itemView);
                relativeLayout = itemView.findViewById(R.id.container);
                textView = itemView.findViewById(R.id.tv_title);
            }
        }
    }

MainActivity

package com.example.viewpagerandfragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager2 viewPager2 = findViewById(R.id.viewPage);
        ViewPagerAdapter viewPage2Adapter =new ViewPagerAdapter();
        viewPager2.setAdapter(viewPage2Adapter);
    }
}

activity_main.xml

<?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">


    <androidx.viewpager2.widget.ViewPager2
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewPage"
        android:background="@color/purple_200"
        />
</LinearLayout>

item_pager.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"
    android:id="@+id/container"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_title"
        android:textColor="#ff4532"
        android:layout_centerInParent="true"
        android:textSize="32dp"
        android:text="我喜欢学习"
        />

</RelativeLayout>

标签:color,add,titles,切换,viewPager2,import,android,public,页面
From: https://www.cnblogs.com/lin513/p/17314906.html

相关文章