PagerAdapter.java revision b097e1498ee2c8bb6265ef948dbea45a1e51ef84
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v4.view; 18 19import android.os.Parcelable; 20import android.view.View; 21 22/** 23 * Base class providing the adapter to populate pages inside of 24 * a {@link ViewPager}. You will most likely want to use a more 25 * specific implementation of this, such as 26 * {@link android.support.v4.app.FragmentPagerAdapter} or 27 * {@link android.support.v4.app.FragmentStatePagerAdapter}. 28 */ 29public abstract class PagerAdapter { 30 private DataSetObserver mObserver; 31 32 public static final int POSITION_UNCHANGED = -1; 33 public static final int POSITION_NONE = -2; 34 35 /** 36 * Used to watch for changes within the adapter. 37 */ 38 interface DataSetObserver { 39 public void onDataSetChanged(); 40 } 41 42 /** 43 * Return the number of views available. 44 */ 45 public abstract int getCount(); 46 47 /** 48 * Called when a change in the shown pages is going to start being made. 49 * @param container The containing View which is displaying this adapter's 50 * page views. 51 */ 52 public abstract void startUpdate(View container); 53 54 /** 55 * Create the page for the given position. The adapter is responsible 56 * for adding the view to the container given here, although it only 57 * must ensure this is done by the time it returns from 58 * {@link #finishUpdate()}. 59 * 60 * @param container The containing View in which the page will be shown. 61 * @param position The page position to be instantiated. 62 * @return Returns an Object representing the new page. This does not 63 * need to be a View, but can be some other container of the page. 64 */ 65 public abstract Object instantiateItem(View container, int position); 66 67 /** 68 * Remove a page for the given position. The adapter is responsible 69 * for removing the view from its container, although it only must ensure 70 * this is done by the time it returns from {@link #finishUpdate()}. 71 * 72 * @param container The containing View from which the page will be removed. 73 * @param position The page position to be removed. 74 * @param object The same object that was returned by 75 * {@link #instantiateItem(View, int)}. 76 */ 77 public abstract void destroyItem(View container, int position, Object object); 78 79 /** 80 * Called when the a change in the shown pages has been completed. At this 81 * point you must ensure that all of the pages have actually been added or 82 * removed from the container as appropriate. 83 * @param container The containing View which is displaying this adapter's 84 * page views. 85 */ 86 public abstract void finishUpdate(View container); 87 88 public abstract boolean isViewFromObject(View view, Object object); 89 90 public abstract Parcelable saveState(); 91 92 public abstract void restoreState(Parcelable state, ClassLoader loader); 93 94 /** 95 * Called when the host view is attempting to determine if an item's position 96 * has changed. Returns {@link #POSITION_UNCHANGED} if the position of the given 97 * item has not changed or {@link #POSITION_NONE} if the item is no longer present 98 * in the adapter. 99 * 100 * <p>The default implementation assumes that items will never 101 * change position and always returns {@link #POSITION_UNCHANGED}. 102 * 103 * @param object Object representing an item, previously returned by a call to 104 * {@link #instantiateItem(View, int)}. 105 * @return object's new position index from [0, {@link #getCount()}), 106 * {@link #POSITION_UNCHANGED} if the object's position has not changed, 107 * or {@link #POSITION_NONE} if the item is no longer present. 108 */ 109 public int getItemPosition(Object object) { 110 return POSITION_UNCHANGED; 111 } 112 113 /** 114 * This method should be called by the application if the data backing this adapter has changed 115 * and associated views should update. 116 */ 117 public void notifyDataSetChanged() { 118 if (mObserver != null) { 119 mObserver.onDataSetChanged(); 120 } 121 } 122 123 void setDataSetObserver(DataSetObserver observer) { 124 mObserver = observer; 125 } 126} 127