本文整理了Java中cn.youngkaaa.yviewpager.YPagerAdapter
类的一些代码示例,展示了YPagerAdapter
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。YPagerAdapter
类的具体详情如下:
包路径:cn.youngkaaa.yviewpager.YPagerAdapter
类名称:YPagerAdapter
[英]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:
PagerAdapter is more general than the adapters used for android.widget.AdapterView. 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.
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). By the time #finishUpdate(ViewGroup) returns the views associated with the key objects returned by #instantiateItem(ViewGroup,int) should be added to the parent ViewGroup passed to these methods and the views associated with the keys passed to #destroyItem(ViewGroup,int,Object)should be removed. The method #isViewFromObject(View,Object) identifies whether a page View is associated with a given key 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 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 android.widget.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).
[中]基类提供用于填充ViewPager内部页面的适配器。您很可能希望使用更具体的实现,例如FragmentPagerAdapter或FragmentStatePagerAdapter。
实现PagerAdapter时,必须至少覆盖以下方法:
*#实例化项(视图组,int)
*#destroyItem(视图组、int、对象)
*#getCount()
*#isViewFromObject(视图,对象)
PagerAdapter比用于android的适配器更通用。小装置。AdapterView。ViewPager没有直接提供视图循环机制,而是使用回调来指示更新过程中采取的步骤。如果需要,PagerAdapter可以实现一种形式的视图循环,或者使用更复杂的方法来管理页面视图,例如片段事务,其中每个页面都由自己的片段表示。
ViewPager将每个页面与一个键对象关联,而不是直接使用视图。此键用于跟踪和唯一标识给定页面,而不受其在适配器中的位置影响。调用PagerAdapter方法#startUpdate(ViewGroup)表示ViewPager的内容即将更改。随后将有一个或多个对#InstanceItem(ViewGroup,int)和/或#destroyItem(ViewGroup,int,Object)的调用,并通过对#finishUpdate(ViewGroup)的调用发出更新结束的信号。当#finishUpdate(ViewGroup)返回与#InstanceItem(ViewGroup,int)返回的键对象相关联的视图时,应将其添加到传递给这些方法的父视图组中,并删除与传递给#destroyItem(ViewGroup,int,Object)的键相关联的视图。方法#isViewFromObject(视图,对象)标识页面视图是否与给定的键对象关联。
一个非常简单的PagerAdapter可以选择将页面视图本身用作关键对象,在创建后从#InstanceItem(ViewGroup,int)返回它们,并将它们添加到父视图组中。匹配的#destroyItem(ViewGroup,int,Object)实现将从父视图组中删除视图,#isViewFromObject(View,Object)可以实现为return view == object;
。
PagerAdapter支持数据集更改。数据集更改必须发生在主线程上,并且必须以调用#notifyDataSetChanged()结束,类似于从android派生的AdapterView适配器。小装置。BaseAdapter。数据集更改可能涉及页面的添加、删除或位置更改。如果适配器实现#getItemPosition(Object)方法,ViewPager将保持当前页面处于活动状态。
代码示例来源:origin: open-android/ViewPager
/**
* Remove a page for the given position. The adapter is responsible
* for removing the view from its container, although it only must ensure
* this is done by the time it returns from {@link #finishUpdate(ViewGroup)}.
*
* @param container The containing View from which the page will be removed.
* @param position The page position to be removed.
* @param object The same object that was returned by
* {@link #instantiateItem(View, int)}.
*/
public void destroyItem(ViewGroup container, int position, Object object) {
destroyItem((View) container, position, object);
}
代码示例来源:origin: open-android/ViewPager
/**
* Called when the a change in the shown pages has been completed. At this
* point you must ensure that all of the pages have actually been added or
* removed from the container as appropriate.
*
* @param container The containing View which is displaying this adapter's
* page views.
*/
public void finishUpdate(ViewGroup container) {
finishUpdate((View) container);
}
代码示例来源:origin: open-android/ViewPager
private int getAdapterCount() {
if (isCirculatory) {
return mAdapterCirculatoryCount;
} else {
return mAdapter.getCount();
}
}
代码示例来源:origin: open-android/ViewPager
ItemInfo addNewItem(int position, int index) {
int newPos = position % mAdapter.getCount();
Log.d(TAG, "addNewItem newPos=>" + newPos + ",position=>" + position);
if (newPos == 0) {
mCurrentStartPos = position;
Log.d(TAG, "addNewItem mCurrentStartPos=>" + mCurrentStartPos);
}
ItemInfo ii = new ItemInfo();
ii.position = position;
ii.object = mAdapter.instantiateItem(this, newPos);
ii.widthFactor = mAdapter.getPageWidth(newPos);
if (index < 0 || index >= mItems.size()) {
mItems.add(ii);
} else {
mItems.add(index, ii);
}
return ii;
}
代码示例来源:origin: open-android/ViewPager
/**
* 根据传入的position来构造一个ItemInfo对象的实例 然后加入到mItems中去
*
* @param position
* @param index
* @return
*/
ItemInfo addNewItem(int position, int index) {
ItemInfo ii = new ItemInfo();
ii.position = position;
ii.object = mAdapter.instantiateItem(this, position);
ii.widthFactor = mAdapter.getPageWidth(position);
if (index < 0 || index >= mItems.size()) {
mItems.add(ii);
} else {
mItems.add(index, ii);
}
return ii;
}
代码示例来源:origin: open-android/ViewPager
mAdapter.startUpdate(this);
final int N = mAdapter.getCount();
mAdapter.destroyItem(this, pos, ii.object);
mAdapter.destroyItem(this, pos, ii.object);
ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
mAdapter.setPrimaryItem(this, mCurItem, curItem != null ? curItem.object : null);
mAdapter.finishUpdate(this);
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
代码示例来源:origin: open-android/ViewPager
mAdapter.setViewPagerObserver(null);
mAdapter.startUpdate(this);
for (int i = 0; i < mItems.size(); i++) {
final ItemInfo ii = mItems.get(i);
mAdapter.destroyItem(this, ii.position, ii.object);
mAdapter.finishUpdate(this);
mItems.clear();
removeNonDecorViews();
mObserver = new PagerObserver();
mAdapter.setViewPagerObserver(mObserver);
mPopulatePending = false;
final boolean wasFirstLayout = mFirstLayout;
mFirstLayout = true;
mExpectedAdapterCount = mAdapter.getCount();
if (mRestoredCurItem >= 0) {
mAdapter.restoreState(mRestoredAdapterState, mRestoredClassLoader);
if (isVertical) {
setCurrentItemInternalVertical(mRestoredCurItem, false, true);
代码示例来源:origin: open-android/ViewPager
void dataSetChanged() {
final int adapterCount = mAdapter.getCount();
mExpectedAdapterCount = adapterCount;
boolean needPopulate = mItems.size() < mOffscreenPageLimit * 2 + 1
for (int i = 0; i < mItems.size(); i++) {
final ItemInfo ii = mItems.get(i);
final int newPos = mAdapter.getItemPosition(ii.object);
mAdapter.startUpdate(this);
isUpdating = true;
mAdapter.destroyItem(this, ii.position, ii.object);
needPopulate = true;
mAdapter.finishUpdate(this);
代码示例来源:origin: open-android/ViewPager
offset += mAdapter.getPageWidth(pos) + marginOffset;
pos++;
offset -= mAdapter.getPageWidth(pos) + marginOffset;
pos--;
offset -= mAdapter.getPageWidth(pos--) + marginOffset;
final ItemInfo ii = mItems.get(i);
while (pos < ii.position) {
offset += mAdapter.getPageWidth(pos++) + marginOffset;
代码示例来源:origin: open-android/ViewPager
private void calculatePageOffsetsVertical(ItemInfo curItem, int curIndex, ItemInfo oldCurInfo) {
final int N = mAdapter.getCount();
final int height = getClientHeight();
final float marginOffset = height > 0 ? (float) mPageMargin / height : 0;
offset += mAdapter.getPageWidth(pos) + marginOffset;
pos++;
offset -= mAdapter.getPageWidth(pos) + marginOffset;
pos--;
offset -= mAdapter.getPageWidth(pos--) + marginOffset;
final ItemInfo ii = mItems.get(i);
while (pos < ii.position) {
offset += mAdapter.getPageWidth(pos++) + marginOffset;
代码示例来源:origin: open-android/ViewPager
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.position = mCurItem;
if (mAdapter != null) {
ss.adapterState = mAdapter.saveState();
}
return ss;
}
代码示例来源:origin: open-android/ViewPager
ItemInfo infoForChild(View child) {
for (int i = 0; i < mItems.size(); i++) {
ItemInfo ii = mItems.get(i);
if (mAdapter.isViewFromObject(child, ii.object)) {
return ii;
}
}
return null;
}
代码示例来源:origin: open-android/ViewPager
@Override
public void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
if (mAdapter != null) {
mAdapter.restoreState(ss.adapterState, ss.loader);
if (isVertical) {
setCurrentItemInternalVertical(ss.position, false, true);
} else {
setCurrentItemInternalHorizontal(ss.position, false, true);
}
} else {
mRestoredCurItem = ss.position;
mRestoredAdapterState = ss.adapterState;
mRestoredClassLoader = ss.loader;
}
}
代码示例来源:origin: open-android/ViewPager
/**
* Called to inform the adapter of which item is currently considered to
* be the "primary", that is the one show to the user as the current page.
*
* @param container The containing View from which the page will be removed.
* @param position The page position that is now the primary.
* @param object The same object that was returned by
* {@link #instantiateItem(View, int)}.
*/
public void setPrimaryItem(ViewGroup container, int position, Object object) {
setPrimaryItem((View) container, position, object);
}
代码示例来源:origin: open-android/ViewPager
/**
* Create the page for the given position. The adapter is responsible
* for adding the view to the container given here, although it only
* must ensure this is done by the time it returns from
* {@link #finishUpdate(ViewGroup)}.
*
* @param container The containing View in which the page will be shown.
* @param position The page position to be instantiated.
* @return Returns an Object representing the new page. This does not
* need to be a View, but can be some other container of the page.
*/
public Object instantiateItem(ViewGroup container, int position) {
return instantiateItem((View) container, position);
}
代码示例来源:origin: open-android/ViewPager
mAdapter.startUpdate(this);
mAdapter.destroyItem(this, pos, ii.object);
itemIndex--;
curIndex--;
mAdapter.destroyItem(this, pos, ii.object);
ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
mAdapter.setPrimaryItem(this, mCurItem, curItem != null ? curItem.object : null);
mAdapter.finishUpdate(this);
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
代码示例来源:origin: open-android/ViewPager
Log.d(TAG, "setAdapter() in");
if (mAdapter != null) {
mAdapter.setViewPagerObserver(null);
mAdapter.startUpdate(this);
for (int i = 0; i < mItems.size(); i++) {
final ItemInfo ii = mItems.get(i);
mAdapter.destroyItem(this, ii.position, ii.object);
mAdapter.finishUpdate(this);
mItems.clear();
removeNonDecorViews();
mObserver = new PagerObserver();
mAdapter.setViewPagerObserver(mObserver);
mPopulatePending = false;
final boolean wasFirstLayout = mFirstLayout;
Log.d(TAG, "setAdapter() mCurItem2=>" + mCurItem);
if (mRestoredCurItem >= 0) {
mAdapter.restoreState(mRestoredAdapterState, mRestoredClassLoader);
if (isVertical) {
setCurrentItemInternalVertical(mRestoredCurItem, false, true);
代码示例来源:origin: open-android/ViewPager
for (int i = 0; i < mItems.size(); i++) {
final YViewPager.ItemInfo ii = mItems.get(i);
final int newPos = mAdapter.getItemPosition(ii.object);
mAdapter.startUpdate(this);
isUpdating = true;
mAdapter.destroyItem(this, ii.position, ii.object);
needPopulate = true;
mAdapter.finishUpdate(this);
代码示例来源:origin: open-android/ViewPager
offset += mAdapter.getPageWidth(pos) + marginOffset;
pos++;
offset -= mAdapter.getPageWidth(pos) + marginOffset;
pos--;
offset -= mAdapter.getPageWidth(pos--) + marginOffset;
final ItemInfo ii = mItems.get(i);
while (pos < ii.position) {
offset += mAdapter.getPageWidth(pos++) + marginOffset;
代码示例来源:origin: open-android/ViewPager
final int N = mAdapter.getCount();
final int width = getClientWidth();
final float marginOffset = width > 0 ? (float) mPageMargin / width : 0;
offset += mAdapter.getPageWidth(pos) + marginOffset;
pos++;
offset -= mAdapter.getPageWidth(pos) + marginOffset;
pos--;
offset -= mAdapter.getPageWidth(pos--) + marginOffset;
final ItemInfo ii = mItems.get(i);
while (pos < ii.position) {
offset += mAdapter.getPageWidth(pos++) + marginOffset;
内容来源于网络,如有侵权,请联系作者删除!