Skip to content

Instantly share code, notes, and snippets.

@pocmo
Created October 21, 2012 15:02
Show Gist options
  • Save pocmo/3927202 to your computer and use it in GitHub Desktop.
Save pocmo/3927202 to your computer and use it in GitHub Desktop.
ViewPagerAdapter: Implementation of PagerAdapter that represents each page as a View
/*
* Copyright (C) 2012 Sebastian Kaspari
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.androidzeitgeist.adapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
/**
* Implementation of {@link PagerAdapter} that represents each page as a {@link View}.
*
* @author Sebastian Kaspari <[email protected]>
*/
public abstract class ViewPagerAdapter extends PagerAdapter
{
/**
* Get a View that displays the data at the specified position in the data set.
*
* @param position The position of the item within the adapter's data set of the item whose view we want.
* @param pager The ViewPager that this view will eventually be attached to.
*
* @return A View corresponding to the data at the specified position.
*/
public abstract View getView(int position, ViewPager pager);
/**
* Determines whether a page View is associated with a specific key object as
* returned by instantiateItem(ViewGroup, int).
*
* @param view Page View to check for association with object
* @param object Object to check for association with view
*
* @return true if view is associated with the key object object.
*/
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
/**
* Create the page for the given position.
*
* @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.
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
ViewPager pager = (ViewPager) container;
View view = getView(position, pager);
pager.addView(view);
return view;
}
/**
* Remove a page for the given position.
*
* @param container The containing View from which the page will be removed.
* @param position The page position to be removed.
* @param view The same object that was returned by instantiateItem(View, int).
*/
@Override
public void destroyItem(ViewGroup container, int position, Object view) {
((ViewPager) container).removeView((View) view);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment