PagerAdapter.java revision 8fffe01871be1806a1bdefa1f7213b660fcf5ac0
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
198fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powellimport android.database.DataSetObservable;
208fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powellimport android.database.DataSetObserver;
215c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornimport android.os.Parcelable;
225c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornimport android.view.View;
235c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
245c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn/**
255c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * Base class providing the adapter to populate pages inside of
265c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * a {@link ViewPager}.  You will most likely want to use a more
275c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * specific implementation of this, such as
285c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * {@link android.support.v4.app.FragmentPagerAdapter} or
295c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * {@link android.support.v4.app.FragmentStatePagerAdapter}.
305c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn */
315c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornpublic abstract class PagerAdapter {
328fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell    private DataSetObservable mObservable = new DataSetObservable();
333661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell
343661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    public static final int POSITION_UNCHANGED = -1;
353661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    public static final int POSITION_NONE = -2;
363661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell
375c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    /**
383661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * Return the number of views available.
395c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     */
405c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract int getCount();
415c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
425c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    /**
435c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * Called when a change in the shown pages is going to start being made.
445c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @param container The containing View which is displaying this adapter's
455c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * page views.
465c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     */
475c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract void startUpdate(View container);
485c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
495c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    /**
505c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * Create the page for the given position.  The adapter is responsible
515c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * for adding the view to the container given here, although it only
525c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * must ensure this is done by the time it returns from
535c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * {@link #finishUpdate()}.
545c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     *
555c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @param container The containing View in which the page will be shown.
565c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @param position The page position to be instantiated.
575c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @return Returns an Object representing the new page.  This does not
585c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * need to be a View, but can be some other container of the page.
595c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     */
605c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract Object instantiateItem(View container, int position);
615c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
625c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    /**
635c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * Remove a page for the given position.  The adapter is responsible
645c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * for removing the view from its container, although it only must ensure
655c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * this is done by the time it returns from {@link #finishUpdate()}.
665c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     *
675c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @param container The containing View from which the page will be removed.
685c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @param position The page position to be removed.
695c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @param object The same object that was returned by
705c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * {@link #instantiateItem(View, int)}.
715c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     */
725c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract void destroyItem(View container, int position, Object object);
735c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
745c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    /**
752a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     * Called to inform the adapter of which item is currently considered to
762a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     * be the "primary", that is the one show to the user as the current page.
772a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     *
782a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     * @param container The containing View from which the page will be removed.
792a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     * @param position The page position that is now the primary.
802a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     * @param object The same object that was returned by
812a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     * {@link #instantiateItem(View, int)}.
822a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn     */
832a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    public void setPrimaryItem(View container, int position, Object object) {
842a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    }
852a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn
862a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    /**
875c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * Called when the a change in the shown pages has been completed.  At this
885c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * point you must ensure that all of the pages have actually been added or
895c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * removed from the container as appropriate.
905c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @param container The containing View which is displaying this adapter's
915c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * page views.
925c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     */
935c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract void finishUpdate(View container);
945c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
955c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract boolean isViewFromObject(View view, Object object);
965c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
975c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract Parcelable saveState();
985c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
995c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract void restoreState(Parcelable state, ClassLoader loader);
1003661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell
1013661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    /**
1023661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * Called when the host view is attempting to determine if an item's position
1033661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * has changed. Returns {@link #POSITION_UNCHANGED} if the position of the given
1043661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * item has not changed or {@link #POSITION_NONE} if the item is no longer present
1053661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * in the adapter.
1063661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     *
1073661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * <p>The default implementation assumes that items will never
1083661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * change position and always returns {@link #POSITION_UNCHANGED}.
1093661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     *
1103661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * @param object Object representing an item, previously returned by a call to
1113661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     *               {@link #instantiateItem(View, int)}.
1123661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * @return object's new position index from [0, {@link #getCount()}),
1133661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     *         {@link #POSITION_UNCHANGED} if the object's position has not changed,
1143661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     *         or {@link #POSITION_NONE} if the item is no longer present.
1153661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     */
1163661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    public int getItemPosition(Object object) {
1173661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell        return POSITION_UNCHANGED;
1183661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    }
1193661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell
1203661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    /**
1213661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * This method should be called by the application if the data backing this adapter has changed
1223661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     * and associated views should update.
1233661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell     */
1243661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    public void notifyDataSetChanged() {
1258fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell        mObservable.notifyChanged();
1268fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell    }
1278fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell
1288fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell    void registerDataSetObserver(DataSetObserver observer) {
1298fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell        mObservable.registerObserver(observer);
1303661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    }
1313661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell
1328fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell    void unregisterDataSetObserver(DataSetObserver observer) {
1338fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell        mObservable.unregisterObserver(observer);
1348fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell    }
1358fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell
1368fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell    /**
1378fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell     * This method may be called by the ViewPager to obtain a title string
1388fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell     * to describe the specified page. This method may return null
1398fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell     * indicating no title for this page. The default implementation returns
1408fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell     * null.
1418fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell     *
1428fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell     * @param position The position of the title requested
1438fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell     * @return A title for the requested page
1448fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell     */
1458fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell    public CharSequence getPageTitle(int position) {
1468fffe01871be1806a1bdefa1f7213b660fcf5ac0Adam Powell        return null;
1473661ad6c9143d58f741568f940f77ca9d38dec47Adam Powell    }
1485c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn}
149