截止本文發佈時間止,viewpager2的最新版本爲穩定版1.0.0,該版本與 1.0.0-rc01 相比沒有變化。
簡介
1.0.0 的主要功能
- 對之前的 ViewPager 實現的改進:
使用
要使用 ViewPager2,請將以下 AndroidX 依賴項添加到項目的 build.gradle 文件:
dependencies {
implementation "androidx.viewpager2:viewpager2:1.0.0"
}
ViewPager2爲androidx庫中的,簡單來說androidx庫就是對之前android-support的整合,如果項目還在使用android-support庫,那麼只有兩個選擇:
- 項目整體切換到androidx(需要有比較大的把我,也是未來的趨勢)
- 不使用ViewPager2
言歸正傳,我們繼續
先創建一個fragment的佈局文件就命名爲fragment_screen_slide_page.xml(引用的字符串內容需要你自己去定義):
<!-- fragment_screen_slide_page.xml -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView style="?android:textAppearanceMedium"
android:padding="16dp"
android:lineSpacingMultiplier="1.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/lorem_ipsum" />
</ScrollView>
創建一個Fragment類,在onCreateView中返回剛剛創建的佈局
import androidx.fragment.app.Fragment;
...
public class ScreenSlidePageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
return rootView;
}
}
然後創建一個ViewPager2對應的佈局,命名爲activity_screen_slide.xml
<!-- 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" />
創建一個適配器,命名爲ScreenSlidePagerAdapter,繼承FragmentStateAdapter
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import com.channel.myapplication.fragment.ScreenSlidePageFragment;
/**
* @author Administrator
*/
public class ScreenSlidePagerAdapter extends FragmentStateAdapter {
/**
* The number of pages (wizard steps) to show in this demo.
*/
private static final int NUM_PAGES = 5;
public ScreenSlidePagerAdapter(FragmentActivity fa) {
super(fa);
}
@NonNull
@Override
public Fragment createFragment(int position) {
return new ScreenSlidePageFragment();
}
@Override
public int getItemCount() {
return NUM_PAGES;
}
}
創建activity命名爲ScreenSlidePagerActivity,並在此activity中將ScreenSlidePagerAdapter與ViewPager2綁定
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
...
public class ScreenSlidePagerActivity extends FragmentActivity {
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager2 mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private FragmentStateAdapter pagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
// Instantiate a ViewPager and a PagerAdapter.
mPager = findViewById(R.id.pager);
pagerAdapter = new ScreenSlidePagerAdapter(this);
mPager.setAdapter(pagerAdapter);
}
@Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
}
運行項目進入ScreenSlidePagerActivity即可看到效果
效果圖(錄製來自谷歌官網)
推薦閱讀:
ViewPager2添加自定義動畫
關注我的公衆號(不是技術號),讓你的職業發展除技術外,多一種可能