Skip to content

Instantly share code, notes, and snippets.

@yrom
Created May 30, 2015 08:12
Show Gist options
  • Save yrom/728237025575005a8fd3 to your computer and use it in GitHub Desktop.
Save yrom/728237025575005a8fd3 to your computer and use it in GitHub Desktop.
sample of RecycledViewPool
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTitleStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.view.ViewPager>
</LinearLayout>
public class PagerActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paper);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new PageAdapter(getSupportFragmentManager()));
}
static class PageAdapter extends FragmentPagerAdapter{
RecyclerView.RecycledViewPool mPool = new RecyclerView.RecycledViewPool(){
@Override
public RecyclerView.ViewHolder getRecycledView(int viewType) {
RecyclerView.ViewHolder scrap = super.getRecycledView(viewType);
Log.i("@@@", "get holder from pool: "+scrap );
return scrap;
}
@Override
public void putRecycledView(RecyclerView.ViewHolder scrap) {
super.putRecycledView(scrap);
Log.i("@@@", "put holder to pool: " + scrap);
}
@Override
public String toString() {
return "ViewPool in adapter@"+Integer.toHexString(hashCode());
}
};
public PageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
RecyclerViewFragment f = new RecyclerViewFragment();
f.mPool = mPool;
return f;
}
@Override
public int getCount() {
return 10;
}
@Override
public CharSequence getPageTitle(int position) {
return "pos:"+position;
}
}
public static class RecyclerViewFragment extends Fragment{
RecyclerView.RecycledViewPool mPool;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView view = new RecyclerView(inflater.getContext());
LinearLayoutManager layout = new LinearLayoutManager(inflater.getContext());
layout.setRecycleChildrenOnDetach(true);
view.setLayoutManager(layout);
if (mPool != null) {
view.setRecycledViewPool(mPool);
Log.i("@@@", "using view pool :" + mPool);
}
view.setAdapter(new DemoAdapter());
view.setItemViewCacheSize(10);
return view;
}
}
static class DemoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
String[] mTexts;
DemoAdapter(){
int count = new Random().nextInt(60);
mTexts = new String[count];
fillTexts(count);
}
private void fillTexts(int count) {
Random random = new Random();
for (int i = 0; i < count; i++) {
char[] buf = new char[random.nextInt(10)];
Arrays.fill(buf, (char) (random.nextInt(26) + 65));
mTexts[i] = new String(buf);
}
}
static final int TEXT = 0;
static final int TITLE = 1;
@Override
public int getItemViewType(int position) {
if (position % 10 == 0) return TITLE;
return TEXT;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
TextView view = new TextView(viewGroup.getContext());
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
if (viewType == TITLE) {
return new TitleTextViewHolder(view);
} else {
return new TextViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
TextView itemView = (TextView) viewHolder.itemView;
if(viewHolder instanceof TitleTextViewHolder) {
itemView.setText("~~~"+mTexts[position]+"~~~~");
}else{
itemView.setText(position+". "+mTexts[position]);
}
}
@Override
public int getItemCount() {
return mTexts.length;
}
static class TextViewHolder extends RecyclerView.ViewHolder{
public TextViewHolder(TextView itemView) {
super(itemView);
itemView.setTextAppearance(itemView.getContext(), R.style.TextAppearance_AppCompat_Medium);
}
}
static class TitleTextViewHolder extends RecyclerView.ViewHolder{
public TitleTextViewHolder(TextView itemView) {
super(itemView);
itemView.setTextAppearance(itemView.getContext(), R.style.TextAppearance_AppCompat_Headline);
itemView.setGravity(Gravity.CENTER_HORIZONTAL);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment