Viewpager2+TabLayout+Fragment的简单使用,先了解如何使用viewpager2就可以简单实现引导页

导入依赖
implementation ‘androidx.viewpager2:viewpager2:1.0.0-alpha01’
//tablayout
implementation ‘com.google.android.material:material:1.2.0-alpha01’

比较简单直接上代码。。

效果图在下面!!!

Activity布局代码

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/my_tab"
        android:layout_width="match_parent"
        android:layout_height="60dp"/>
    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/my_pager2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

Activity代码

package com.wmc.test;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.widget.Button;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

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

/**
 * @author 吃鱼噎死的猫
 */
public class MainActivity extends AppCompatActivity {
     
    private TabLayout myTab;
    private ViewPager2 myPager2;

    List<String> titles=new ArrayList<>();
    List<Fragment> fragments=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myTab = findViewById(R.id.my_tab);
        myPager2 = findViewById(R.id.my_pager2);

        //添加标题
        titles.add("1");
        titles.add("2");

        //添加Fragment进去
        fragments.add(new MyFragment());
        fragments.add(new Fragment());

        //实例化适配器
        MyAdapter myAdapter=new MyAdapter(getSupportFragmentManager(),getLifecycle(),fragments);
        //设置适配器
        myPager2.setAdapter(myAdapter);
        //TabLayout和Viewpager2进行关联
        new TabLayoutMediator(myTab, myPager2, new TabLayoutMediator.TabConfigurationStrategy() {
     
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
     
                tab.setText(titles.get(position));
            }
        }).attach();

    }
}

配适器代码

package com.wmc.test;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import java.util.List;

/**
 * @author 吃鱼噎死的猫
 */
public class MyAdapter extends FragmentStateAdapter {
     
    List<Fragment> fragments;
    public MyAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, List<Fragment> fragments) {
     
        super(fragmentManager, lifecycle);
        this.fragments = fragments;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
     
        return fragments.get(position);
    }

    @Override
    public int getItemCount() {
     
        return fragments.size();
    }
}

Fragment代码

/**
 * @author 吃鱼噎死的猫
 */
public class MyFragment extends Fragment {
     
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     
        View inflate = inflater.inflate(R.layout.myfragment, null);
        return inflate;
    }
}

Fragment布局

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

效果图

Viewpager2+TabLayout+Fragment的简单使用,先了解如何使用viewpager2就可以简单实现引导页_第1张图片

Viewpager2+TabLayout+Fragment的简单使用,先了解如何使用viewpager2就可以简单实现引导页_第2张图片
页面切换可以滑动也可以点击TabLayout的标题
这是最简单的的实现效果 想要变得炫酷则需要加入动画。

你可能感兴趣的:(viewpager,android,studio,android,安卓)