本文实例为大家分享了Viewpager2实现登录注册引导页面的具体代码,供大家参考,具体内容如下
介绍
屏幕滑动是两个完整屏幕之间的切换,在设置向导或幻灯片等界面中很常见
实现图(图片来源于网络):
例子
1、创建视图
我这里只创建了3个XML
fragment0.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/p0"
/>
</LinearLayout>
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/p1"
/>
</LinearLayout>
fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/p2"
/>
</LinearLayout>
2、创建 Fragment
根据构造方法传进来的 int i;返回不同的视图(i是等下用到的FragmentStateAdapter适配器中传进去的)
package com.example.xianyu;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class mFragment extends Fragment {
int i = 0;
mFragment(int i){
this.i = i;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = null;
switch (i){
case 0: {
view = inflater.inflate(R.layout.fragment0, container, false);
break;
}
case 1: {
view = inflater.inflate(R.layout.frament1, container, false);
break;
}
case 2: {
view = inflater.inflate(R.layout.fragment2, container, false);
break;
}
}
return view;
}
}
3、主Activity,并创建自定义适配器继承自FragmentStateAdapter
activity_screen_slide.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- activity_screen_slide.xml -->
<androidx.viewpager2.widget.ViewPager2
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
homeActivity
package com.example.xianyu;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
public class homeActivity extends FragmentActivity {
//要显示的页数
private static final int NUM_PAGES = 3;
private ViewPager2 viewPager2;
// 适配器,为ViewPager2提供页面
private FragmentStateAdapter pagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
viewPager2 = findViewById(R.id.pager);
pagerAdapter = new ScreenSlidePagerAdapter(this);
viewPager2.setAdapter(pagerAdapter);
}
@Override
public void onBackPressed() {
if (viewPager2.getCurrentItem() == 0) {
super.onBackPressed();
} else {
viewPager2.setCurrentItem(viewPager2.getCurrentItem() - 1);
}
}
//自定义的类,继承自FragmentStateAdapter适配器
private class ScreenSlidePagerAdapter extends FragmentStateAdapter {
public ScreenSlidePagerAdapter(FragmentActivity fa) {
super(fa);
}
//主要是createFragment这个方法
@Override
public Fragment createFragment(int position) {
return new mFragment(position);
}
@Override
public int getItemCount() {
return NUM_PAGES;
}
}
}