1. 程式人生 > >Android官方文件翻譯PagerAdapter

Android官方文件翻譯PagerAdapter

PagerAdapter

public abstract class PagerAdapter
extends Object
(公共抽象類 繼承自object)
java.lang.Object
↳ androidx.viewpager.widget.PagerAdapter

Known direct subclasses(已知的直接子類)

FragmentPagerAdapter, FragmentStatePagerAdapter

官方文件傳送門

翻譯工具傳送門

Android初級知識–ViewPager介面卡:PagerAdapter和FragmentPagerAdapter的最基本使用

Base class providing the adapter to populate pages inside of a ViewPager. You will most likely want to use a more specific implementation of this, such as FragmentPagerAdapter or FragmentStatePagerAdapter.
When you implement a PagerAdapter, you must override the following methods at minimum:

基類,提供介面卡以填充ViewPager中的頁。您很可能希望使用更具體的實現,例如FragmentPagerAdapter或FragmentStatePagerAdapter。在實現PagerAdapter時,必須至少覆蓋以下方法:
instantiateItem(ViewGroup, int)
destroyItem(ViewGroup, int, Object)
getCount()
isViewFromObject(View, Object)

PagerAdapter is more general than the adapters used for AdapterViews. Instead of providing a View recycling mechanism directly ViewPager uses callbacks to indicate the steps taken during an update. A PagerAdapter may implement a form of View recycling if desired or use a more sophisticated method of managing page Views such as Fragment transactions where each page is represented by its own Fragment.

PagerAdapter比AdapterViews使用的介面卡更通用。ViewPager沒有直接提供檢視回收機制,而是使用回撥來指示更新期間所採取的步驟。如果需要,PagerAdapter可以實現檢視回收的形式,或者使用更復雜的方法來管理頁面檢視,例如片段事務,其中每個頁面都由自己的片段表示。

ViewPager associates each page with a key Object instead of working with Views directly. This key is used to track and uniquely identify a given page independent of its position in the adapter. A call to the PagerAdapter method startUpdate(ViewGroup) indicates that the contents of the ViewPager are about to change. One or more calls to instantiateItem(ViewGroup, int) and/or destroyItem(ViewGroup, int, Object) will follow, and the end of an update will be signaled by a call to finishUpdate(ViewGroup).

ViewPager將每個頁面與一個關鍵物件關聯起來,而不是直接處理檢視。此鍵用於跟蹤和唯一標識給定頁,而不依賴於其在介面卡中的位置。對PagerAdapter方法startUpdate(ViewGroup)的呼叫表示ViewPager的內容即將發生變化。對instantiateItem(ViewGroup,int)和/或deadyItem(ViewGroup,int,Object)的一個或多個呼叫將隨後進行,更新的結束將通過對FinishUpdate(ViewGroup)的呼叫發出訊號。

By the time finishUpdate returns the views associated with the key objects returned by instantiateItem should be added to the parent ViewGroup passed to these methods and the views associated with the keys passed to destroyItem should be removed. The method isViewFromObject(View, Object) identifies whether a page View is associated with a given key object.

到FinishUpdate返回時,與instantiateItem 返回的鍵物件關聯的檢視應該新增到傳遞給這些方法的父ViewGroup中,並且應該刪除與傳遞給deadyItem的鍵相關的檢視。方法isViewFromObject(View,Object)標識頁面檢視是否與給定的鍵物件關聯。

A very simple PagerAdapter may choose to use the page Views themselves as key objects, returning them from instantiateItem(ViewGroup, int) after creation and adding them to the parent ViewGroup. A matching destroyItem(ViewGroup, int, Object) implementation would remove the View from the parent ViewGroup and isViewFromObject(View, Object) could be implemented as return view == object;.

一個非常簡單的PagerAdapter可以選擇使用頁面檢視本身作為關鍵物件,在建立後從例項化Item(ViewGroup,int)返回它們,並將它們新增到父ViewGroup中。匹配的deadyItem(ViewGroup,int,Object)實現將從父ViewGroup中刪除View,isViewFromObject(View,Object)可以實現為返回檢視=Object;

PagerAdapter supports data set changes. Data set changes must occur on the main thread and must end with a call to notifyDataSetChanged() similar to AdapterView adapters derived from BaseAdapter. A data set change may involve pages being added, removed, or changing position. The ViewPager will keep the current page active provided the adapter implements the method getItemPosition(Object).

PagerAdapter支援資料集更改。資料集更改必須發生在主執行緒上,並且必須以呼叫NotifyDataSetChanged()結束,類似於從BaseAdapter派生的AdapterView介面卡。資料集更改可能涉及新增、刪除或更改位置的頁。如果介面卡實現getItemPosition(Object)方法,ViewPager將保持當前頁面處於活動狀態。

Summary概要

Constants常量

int POSITION_NONE 無位置
int POSITION_UNCHANGED 不變位置

Public constructors 公共構造器

PagerAdapter()

Public methods 公共方法