-
-
Save hasankucuk/63c0d1da78f7fa61173086ded1345098 to your computer and use it in GitHub Desktop.
Recycled Pager Adapter inspired by RecyclerView
This file contains hidden or 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
package com.example; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import com.example.Foo; | |
import com.example.RecycledPagerAdapter; | |
public class FooAdapter extends RecycledPagerAdapter<FooViewHolder> { | |
private List<Foo> data; | |
private LayoutInflater layoutInflater; | |
@Override | |
public FooViewHolder onCreateViewHolder(ViewGroup parent) { | |
if (layoutInflater == null) { | |
layoutInflater = LayoutInflater.from(parent.getContext()); | |
} | |
// Inflate view | |
View v = layoutInflater.inflate(R.layout.item_player_card, parent, false); | |
// Return view holder | |
return new FooViewHolder(v); | |
} | |
@Override | |
public void onBindViewHolder(FooViewHolder viewHolder, int position) { | |
// Show foo inside viewHolder | |
viewHolder.show(data.get(position)); | |
} | |
@Override | |
public int getCount() { | |
return data == null ? 0 : data.size(); | |
} | |
@Override | |
public void setPrimaryItem(ViewGroup container, int position, Object object) { | |
// TODO: optional callback when current object has changed | |
} | |
public TrackSet getData() { | |
return data; | |
} | |
public void setData(TrackSet data) { | |
if (this.data != data) { | |
this.data = data; | |
notifyDataSetChanged(); | |
} | |
} | |
public static class FooViewHolder extends RecycledPagerAdapter.ViewHolder { | |
public FooViewHolder(View v) { | |
super(v); | |
// TODO: use findViewById or Butterknife | |
} | |
public void show(Foo foo) { | |
// TODO: update views with foo | |
} | |
} | |
} |
This file contains hidden or 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
package com.example; | |
import android.support.v4.view.PagerAdapter; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import com.example.RecycledPagerAdapter.ViewHolder; | |
import java.util.LinkedList; | |
import java.util.Queue; | |
/** | |
* See https://gist.github.com/tvkkpt/2e667cb707859123b422 | |
* @param <VH> | |
*/ | |
public abstract class RecycledPagerAdapter<VH extends ViewHolder> extends PagerAdapter { | |
public static abstract class ViewHolder { | |
final View itemView; | |
public ViewHolder(View itemView) { | |
this.itemView = itemView; | |
} | |
} | |
Queue<VH> destroyedItems = new LinkedList<>(); | |
@Override | |
public final Object instantiateItem(ViewGroup container, int position) { | |
VH viewHolder = destroyedItems.poll(); | |
if (viewHolder != null) { | |
// Re-add existing view before rendering so that we can make change inside getView() | |
container.addView(viewHolder.itemView); | |
onBindViewHolder(viewHolder, position); | |
} else { | |
viewHolder = onCreateViewHolder(container); | |
onBindViewHolder(viewHolder, position); | |
container.addView(viewHolder.itemView); | |
} | |
return viewHolder; | |
} | |
@Override | |
public final void destroyItem(ViewGroup container, int position, Object object) { | |
container.removeView(((VH) object).itemView); | |
destroyedItems.add((VH) object); | |
} | |
@Override | |
public final boolean isViewFromObject(View view, Object object) { | |
return ((VH) object).itemView == view; | |
} | |
/** | |
* Create a new view holder | |
* @param parent | |
* @return view holder | |
*/ | |
public abstract VH onCreateViewHolder(ViewGroup parent); | |
/** | |
* Bind data at position into viewHolder | |
* @param viewHolder | |
* @param position | |
*/ | |
public abstract void onBindViewHolder(VH viewHolder, int position); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment