Skip to content

Instantly share code, notes, and snippets.

@billynyh
Created October 30, 2013 08:31
Show Gist options
  • Save billynyh/7229002 to your computer and use it in GitHub Desktop.
Save billynyh/7229002 to your computer and use it in GitHub Desktop.
#android AutoViewPager, simply take the children defined in xml as viewpager item [NOT well tested]
public class AutoViewPager extends ViewPager {
public AutoViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public AutoViewPager(Context context) {
super(context);
init();
}
private void init() {
this.setAdapter(new AutoPagerAdapter());
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
this.getAdapter().notifyDataSetChanged();
}
private class AutoPagerAdapter extends PagerAdapter {
public Object instantiateItem(View collection, int position) {
return AutoViewPager.this.getChildAt(position);
}
@Override
public int getCount() {
return AutoViewPager.this.getChildCount();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment