温馨提示:本文最后更新于
2025-11-20 17:56:15,某些文章具有时效性,若有错误或已失效,请在下方留言。基本介绍
ViewPager 是一种布局管理器,允许用户在数据页面之间左右滑动切换。这通常用于构建包含 多个视图 或 功能区域 的应用,用户可以通过水平滑动在它们之间进行导航,类似于画廊或一系列页。
示例代码
创建 Fragment
实例中创建 3 个 Fragment,分别为 Fragment1、Fragment2 以及 Fragment3。
Fragment1 的相关代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#005b96"
tools:context=".Fragment1">
<TextView
android:textColor="@color/black"
android:textSize="48sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment 1" />
</FrameLayout>
package com.stewednoodles.viewpagerapp;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
public Fragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_1, container, false);
}
}
Fragment2 的相关代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#35a79c"
tools:context=".Fragment2">
<TextView
android:textColor="@color/black"
android:textSize="48sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment 2" />
</FrameLayout>
package com.stewednoodles.viewpagerapp;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
public Fragment2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_2, container, false);
}
}
Fragment3 的相关代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#a8e6cf"
tools:context=".Fragment3">
<TextView
android:textColor="@color/black"
android:textSize="48sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment 3" />
</FrameLayout>
package com.stewednoodles.viewpagerapp;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment3 extends Fragment {
public Fragment3() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_3, container, false);
}
}
适配器
创建适配器 ViewPagerAdapter
package com.stewednoodles.viewpagerapp;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import java.util.ArrayList;
public class ViewPagerAdapter extends FragmentStateAdapter {
private ArrayList<Fragment> fragments = new ArrayList<>() ;
public ViewPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
}
@NonNull
@Override
public Fragment createFragment(int position) {
return fragments.get(position);
}
@Override
public int getItemCount() {
return fragments.size();
}
/*
* Add a fragment to the adapter
*/
public void addFragment(Fragment fragment) {
fragments.add(fragment);
}
}
主页面
主页面相关的布局以及代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.stewednoodles.viewpagerapp;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.viewpager2.widget.ViewPager2;
public class MainActivity extends AppCompatActivity {
ViewPager2 viewPager;
ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), getLifecycle());
viewPagerAdapter.addFragment(new Fragment1());
viewPagerAdapter.addFragment(new Fragment2());
viewPagerAdapter.addFragment(new Fragment3());
viewPager = findViewById(R.id.view_pager);
// 设置 ViewPager2 的方向为水平
viewPager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
// 设置 ViewPager2 的适配器
viewPager.setAdapter(viewPagerAdapter);
}
}
运行效果
运行效果,如下所示
© 版权声明
THE END












暂无评论内容