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 公共方法

猜你喜欢

转载自blog.csdn.net/changhuzichangchang/article/details/82463684