PagerAdapter
先看API:直接继承与object
为了方便大家使用,将其官方给出的相关方法展现出来;
java.lang.Object | |
↳ | android.support.v4.view.PagerAdapter |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void |
destroyItem(
ViewGroup container, int position,
Object object)
Remove a page for the given position.
| ||||||||||
void |
destroyItem(
View container, int position,
Object object)
This method is deprecated. Use destroyItem(ViewGroup, int, Object)
| ||||||||||
void |
finishUpdate(
View container)
This method is deprecated. Use finishUpdate(ViewGroup)
| ||||||||||
void |
finishUpdate(
ViewGroup container)
Called when the a change in the shown pages has been completed.
| ||||||||||
abstract int |
getCount()
Return the number of views available.
| ||||||||||
int |
getItemPosition(
Object object)
Called when the host view is attempting to determine if an item's position has changed.
| ||||||||||
CharSequence |
getPageTitle(int position)
This method may be called by the ViewPager to obtain a title string to describe the specified page.
| ||||||||||
float |
getPageWidth(int position)
Returns the proportional width of a given page as a percentage of the ViewPager's measured width from (0.f-1.f]
| ||||||||||
Object |
instantiateItem(
ViewGroup container, int position)
Create the page for the given position.
| ||||||||||
Object |
instantiateItem(
View container, int position)
This method is deprecated. Use instantiateItem(ViewGroup, int)
| ||||||||||
abstract boolean |
isViewFromObject(
View view,
Object object)
Determines whether a page View is associated with a specific key object as returned by
instantiateItem(ViewGroup, int) .
| ||||||||||
void |
notifyDataSetChanged()
This method should be called by the application if the data backing this adapter has changed and associated views should update.
| ||||||||||
void |
registerDataSetObserver(
DataSetObserver observer)
Register an observer to receive callbacks related to the adapter's data changing.
| ||||||||||
void |
restoreState(
Parcelable state,
ClassLoader loader)
Restore any instance state associated with this adapter and its pages that was previously saved by
saveState() .
| ||||||||||
Parcelable |
saveState()
Save any instance state associated with this adapter and its pages that should be restored if the current UI state needs to be reconstructed.
| ||||||||||
void |
setPrimaryItem(
View container, int position,
Object object)
This method is deprecated. Use setPrimaryItem(ViewGroup, int, Object)
| ||||||||||
void |
setPrimaryItem(
ViewGroup container, int position,
Object object)
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.
| ||||||||||
void |
startUpdate(
View container)
This method is deprecated. Use startUpdate(ViewGroup)
| ||||||||||
void |
startUpdate(
ViewGroup container)
Called when a change in the shown pages is going to start being made.
| ||||||||||
void |
unregisterDataSetObserver(
DataSetObserver observer)
Unregister an observer from callbacks related to the adapter's data changing.
|
下面看代码的实现:
首先给viewpage提供一个资源,一般用List<View> listViews;其实就是一个viewgroup;
((ViewPager) container).addView(listViews.get(position), 0);这里是像viewpager中添加ListView对象;
((ViewPager) container).removeView(listViews.get(position));注意这里,对viewpager里面的ListView进行删除操作;
public class MypagerAdpter extends PagerAdapter {
private List<View> listViews;
public MypagerAdpter(List<View> listViews) {
super();
this.listViews = listViews;
}
@Override
public Object instantiateItem(View container, int position) {
((ViewPager) container).addView(listViews.get(position), 0);
return listViews.get(position);
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView(listViews.get(position));
}
@Override
public int getCount() {
return listViews.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}