Java tutorial
/* * Copyright (C) 2016 Jecelyin Peng <jecelyin@gmail.com> * * This file is part of 920 Text Editor. * * 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.jecelyin.common.adapter; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import com.jecelyin.common.utils.L; /** * @author Jecelyin Peng <jecelyin@gmail.com> */ public abstract class ViewPagerAdapter extends PagerAdapter { private View mCurrentPrimaryItem; /** * 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, ViewGroup 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) { View view = getView(position, container); if (view != mCurrentPrimaryItem) { view.setVisibility(View.INVISIBLE); } container.addView(view); return view; } @Override public void setPrimaryItem(ViewGroup container, int position, Object object) { super.setPrimaryItem(container, position, object); View view = (View) object; if (view != mCurrentPrimaryItem) { if (mCurrentPrimaryItem != null) { mCurrentPrimaryItem.setVisibility(View.INVISIBLE); } if (view != null) { view.setVisibility(View.VISIBLE); } mCurrentPrimaryItem = 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) { L.d("remove page %d", position); if (mCurrentPrimaryItem == view) mCurrentPrimaryItem = null; container.removeView((View) view); } }