PagerAdapter.java revision 2a4d8518f36346ea25a22a736453ff28f2954165
15c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn/*
25c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * Copyright (C) 2011 The Android Open Source Project
35c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn *
45c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
55c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * you may not use this file except in compliance with the License.
65c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * You may obtain a copy of the License at
75c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn *
85c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
95c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn *
105c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * Unless required by applicable law or agreed to in writing, software
115c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
125c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * See the License for the specific language governing permissions and
145c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * limitations under the License.
155c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn */
165c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
175c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornpackage android.support.v4.view;
185c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
195c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornimport android.os.Parcelable;
205c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornimport android.view.View;
215c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
225c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn/**
235c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * Base class providing the adapter to populate pages inside of
245c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * a {@link ViewPager}.  You will most likely want to use a more
255c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * specific implementation of this, such as
265c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * {@link android.support.v4.app.FragmentPagerAdapter} or
275c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * {@link android.support.v4.app.FragmentStatePagerAdapter}.
285c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn */
295c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornpublic abstract class PagerAdapter {
303661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    private DataSetObserver mObserver;
313661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell
323661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    public static final int POSITION_UNCHANGED = -1;
333661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    public static final int POSITION_NONE = -2;
343661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell
355c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    /**
363661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * Used to watch for changes within the adapter.
373661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     */
383661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    interface DataSetObserver {
393661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell        public void onDataSetChanged();
403661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    }
413661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell
423661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    /**
433661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * Return the number of views available.
445c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     */
455c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract int getCount();
465c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
475c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    /**
485c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * Called when a change in the shown pages is going to start being made.
495c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @param container The containing View which is displaying this adapter's
505c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * page views.
515c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     */
525c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract void startUpdate(View container);
535c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
545c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    /**
555c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * Create the page for the given position.  The adapter is responsible
565c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * for adding the view to the container given here, although it only
575c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * must ensure this is done by the time it returns from
585c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * {@link #finishUpdate()}.
595c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     *
605c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @param container The containing View in which the page will be shown.
615c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @param position The page position to be instantiated.
625c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @return Returns an Object representing the new page.  This does not
635c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * need to be a View, but can be some other container of the page.
645c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     */
655c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract Object instantiateItem(View container, int position);
665c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
675c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    /**
685c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * Remove a page for the given position.  The adapter is responsible
695c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * for removing the view from its container, although it only must ensure
705c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * this is done by the time it returns from {@link #finishUpdate()}.
715c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     *
725c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @param container The containing View from which the page will be removed.
735c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @param position The page position to be removed.
745c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @param object The same object that was returned by
755c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * {@link #instantiateItem(View, int)}.
765c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     */
775c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract void destroyItem(View container, int position, Object object);
785c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
795c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    /**
802a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     * Called to inform the adapter of which item is currently considered to
812a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     * be the "primary", that is the one show to the user as the current page.
822a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     *
832a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     * @param container The containing View from which the page will be removed.
842a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     * @param position The page position that is now the primary.
852a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     * @param object The same object that was returned by
862a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     * {@link #instantiateItem(View, int)}.
872a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     */
882a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    public void setPrimaryItem(View container, int position, Object object) {
892a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    }
902a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn
912a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    /**
925c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * Called when the a change in the shown pages has been completed.  At this
935c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * point you must ensure that all of the pages have actually been added or
945c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * removed from the container as appropriate.
955c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @param container The containing View which is displaying this adapter's
965c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * page views.
975c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     */
985c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract void finishUpdate(View container);
995c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
1005c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract boolean isViewFromObject(View view, Object object);
1015c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
1025c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract Parcelable saveState();
1035c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
1045c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract void restoreState(Parcelable state, ClassLoader loader);
1053661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell
1063661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    /**
1073661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * Called when the host view is attempting to determine if an item's position
1083661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * has changed. Returns {@link #POSITION_UNCHANGED} if the position of the given
1093661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * item has not changed or {@link #POSITION_NONE} if the item is no longer present
1103661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * in the adapter.
1113661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     *
1123661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * <p>The default implementation assumes that items will never
1133661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * change position and always returns {@link #POSITION_UNCHANGED}.
1143661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     *
1153661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * @param object Object representing an item, previously returned by a call to
1163661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     *               {@link #instantiateItem(View, int)}.
1173661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * @return object's new position index from [0, {@link #getCount()}),
1183661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     *         {@link #POSITION_UNCHANGED} if the object's position has not changed,
1193661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     *         or {@link #POSITION_NONE} if the item is no longer present.
1203661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     */
1213661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    public int getItemPosition(Object object) {
1223661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell        return POSITION_UNCHANGED;
1233661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    }
1243661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell
1253661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    /**
1263661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * This method should be called by the application if the data backing this adapter has changed
1273661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * and associated views should update.
1283661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     */
1293661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    public void notifyDataSetChanged() {
1303661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell        if (mObserver != null) {
1313661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell            mObserver.onDataSetChanged();
1323661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell        }
1333661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    }
1343661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell
1353661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    void setDataSetObserver(DataSetObserver observer) {
1363661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell        mObserver = observer;
1373661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    }
1385c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn}
139