Last active
January 14, 2017 09:34
-
-
Save wang2bo2/4551304 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright 2013 Bo Wang | |
* | |
* 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.calciumion.widget; | |
import java.util.ArrayList; | |
import android.support.v4.view.PagerAdapter; | |
import android.view.View; | |
import android.view.ViewGroup; | |
/** | |
* This is an implementation of {@link PagerAdapter} that wraps it up like the | |
* {@link Adapter} interface. | |
* | |
* @author bowang | |
* | |
*/ | |
public abstract class BasePagerAdapter extends PagerAdapter { | |
ArrayList<Object> instantiatedItems = new ArrayList<Object>(); | |
ArrayList<Object> destroyedItems = new ArrayList<Object>(); | |
@Override | |
public final void startUpdate(ViewGroup container) { | |
instantiatedItems.clear(); | |
destroyedItems.clear(); | |
} | |
@Override | |
public final Object instantiateItem(ViewGroup container, int position) { | |
Object o = getItem(position); | |
instantiatedItems.add(o); | |
return o; | |
} | |
@Override | |
public final void destroyItem(ViewGroup container, int position, Object object) { | |
destroyedItems.add(object); | |
} | |
@Override | |
public final void finishUpdate(ViewGroup container) { | |
ArrayList<View> recycledViews = new ArrayList<View>(); | |
// Remove views backing destroyed items from the specified container, | |
// and queue them for recycling. | |
for (int i = 0; destroyedItems.size() > 0 && i < container.getChildCount(); i++) { | |
View v = container.getChildAt(i); | |
Iterator<Object> it = destroyedItems.iterator(); | |
while (it.hasNext()) { | |
if (isViewFromObject(v, it.next())) { | |
container.removeView(v); | |
recycledViews.add(v); | |
it.remove(); | |
break; | |
} | |
} | |
} | |
// Render views and attach them to the container. Page views are reused | |
// whenever possible. | |
for (Object instantiatedItem : instantiatedItems) { | |
View convertView = null; | |
if (recycledViews.size() > 0) | |
convertView = recycledViews.remove(0); | |
convertView = getView(instantiatedItem, convertView, container); | |
convertView.setTag(instantiatedItem); | |
container.addView(convertView); | |
} | |
instantiatedItems.clear(); | |
recycledViews.clear(); | |
} | |
@Override | |
public final boolean isViewFromObject(View view, Object object) { | |
return view.getTag() != null && view.getTag() == object; | |
} | |
/** | |
* Get the data item associated with the specified position in the data set. | |
* | |
* @param position | |
* Position of the item whose data we want within the adapter's | |
* data set. | |
* @return The data at the specified position | |
*/ | |
protected abstract Object getItem(int position); | |
/** | |
* Get a View that displays the data at the specified position in the data | |
* set. | |
* | |
* @param object | |
* The data item whose view we want to render. | |
* @param convertView | |
* The view to be reused. | |
* @param parent | |
* The parent that this view will eventually be attached to. | |
* @return A View corresponding to the data at the specified position. | |
*/ | |
protected abstract View getView(Object object, View convertView, ViewGroup parent); | |
} |
Hi!
How to use it?
Can you give me a sample?
Thanks.
Nice work!
I found a bug in finishUpdate(). If you re-add a recycled view after calling getView(), any change we make inside getView() will not work, e.g. calling setText() of a textview. The reason is that when calling removeView() in the first loop, the recycled view no longer exists in view hierachy. I've forked and modified your code, please take a look. Thanks so much for your work!
https://gist.github.com/tvkkpt/2e667cb707859123b422
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
Congrats for your great job with the adapter. It really helps me to manage my viewpager in a better way :-)
I have forked your file and have made some changes in order to allow recycling of the views. Take a look and let me know if the you like the solution.
Cheers!