Android ViewPager2 + Fragment 聯動
本篇主要介紹一下 ViewPager2 + Fragment , 上篇中簡單使用了ViewPager2 實現了一個圖片的滑動效果, 那圖片檢視可以滑動, ViewPager2也可以滑動 Fragment
概述
ViewPager2 官方對它的描述就是 以可滑動的格式顯示檢視或 Fragment
也就說明提供了滑動Fragment的實現 並且還很簡單, 下面來看看吧
實現思路
1.Activity 佈局檔案中引入 ViewPager2 控制元件
2.編寫 Fragment 用於填充到ViewPager2中
3.編寫Adapter 實現 FragmentStateAdapter
下面用實際程式碼 來展示 滑動Fragment
程式碼實現
Activity 佈局檔案中引入 ViewPager2 控制元件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ViewPage2FragmentActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager2fragment"
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/pink"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
編寫 Fragment 用於填充到ViewPager2中
package com.johnny.slzzing;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
/**
* A simple {@link Fragment} subclass.
* Use the {@link ViewPage2Fragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class ViewPage2Fragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public ViewPage2Fragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ViewPage2Fragment.
*/
// TODO: Rename and change types and number of parameters
public static ViewPage2Fragment newInstance(String param1, String param2) {
ViewPage2Fragment fragment = new ViewPage2Fragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_view_page2, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView textView = view.findViewById(R.id.fragmenttextview);
textView.setText(mParam1);
}
}
fragment_view_page2
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewPage2Fragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/fragmenttextview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"
android:gravity="center"
android:textSize="25sp"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment 很簡單 就在中間有個TextView 區分不同的fragment
編寫Adapter 實現 FragmentStateAdapter
package com.johnny.slzzing;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import java.util.Arrays;
import java.util.List;
public class ViewPage2FragmentActivity extends AppCompatActivity {
ViewPager2 viewPage2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_page2_fragment);
viewPage2 = findViewById(R.id.viewpager2fragment);
ViewPager2FragmentAdapter viewPager2FragmentAdapter = new ViewPager2FragmentAdapter(this , initFragmentList());
//重點 ! 把 viewPage2設定adapter 這個adapter 不是去直接繼承RecycleView.Adapter了
viewPage2.setAdapter(viewPager2FragmentAdapter);
}
//初始化3個fragment
private List<Fragment> initFragmentList() {
ViewPage2Fragment viewPage2Fragment = ViewPage2Fragment.newInstance("我是Fragment1", "");
ViewPage2Fragment viewPage2Fragment2 = ViewPage2Fragment.newInstance("我是Fragment2", "");
ViewPage2Fragment viewPage2Fragment3 = ViewPage2Fragment.newInstance("我是Fragment3", "");
return Arrays.asList(viewPage2Fragment,viewPage2Fragment2,viewPage2Fragment3);
}
// 提供了FragmentStateAdapter 只需要繼承它即可 不用繼承RecycleView.Adapter
static class ViewPager2FragmentAdapter extends FragmentStateAdapter{
private final List<Fragment> fragmentList;
public ViewPager2FragmentAdapter(@NonNull FragmentActivity fragmentActivity, List<Fragment> fragmentList) {
super(fragmentActivity);
this.fragmentList = fragmentList;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return fragmentList.get(position);
}
@Override
public int getItemCount() {
return fragmentList.size();
}
}
}
效果
粉色區域是可以滑動切換 不同的Fragment 的
總結
本篇主要介紹了 ViewPager2 結合 Fragment 的基本使用方式 , 讓我們可以快速的實現滑動Fragment 的功能. 後續還可以結合 BottomNavigationView 聯動 底部導航欄+滑動, 有機會再說把
歡迎大家訪問 個人部落格 Johnny小屋
歡迎關注個人公眾號