Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dominando ViewPagers

Dominando ViewPagers

Slides sobre algumas dicas bacanas no uso de ViewPagers, apresentadas no terceiro encontro do GDG Android Meetup, em São Paulo

Ubiratan Soares

May 07, 2013
Tweet

More Decks by Ubiratan Soares

Other Decks in Programming

Transcript

  1. public class CardsPagerAdapter extends FragmentPagerAdapter { public final static int

    FIRST_PAGE = 0; public final static int COFFE_PAGE = 10; public final static int UNSURE_PAGE = 11; private static final String[] CARDS = new String[] { "0", "1", "2", "3", "5", "8", "13", "20", "40", "100", "", "?" }; public CardsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { PokerCardFragment fragment = new PokerCardFragment(); Bundle args = new Bundle(); args.putInt("position", position); args.putString("value", CARDS[position % CARDS.length]); fragment.setArguments(args); return fragment; } @Override public int getCount() { return CARDS.length; } }
  2. public class ScaleFadePageTransformer implements PageTransformer { private int mScreenXOffset; @SuppressWarnings("deprecation")

    public ScaleFadePageTransformer(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); mScreenXOffset = display.getWidth()/2; } @Override public void transformPage(View page, float position) { // Position of page relative to the current front-and-center position of the pager. // 0 is front and center. 1 is one full page position to the right, and -1 is one page position to the left. final float transformValue = Math.abs(Math.abs(position) - 1); page.setAlpha(transformValue); if (position > 0) { page.setScaleX(transformValue); page.setScaleY(transformValue); page.setPivotX(0.5f); final float translateValue = position * -mScreenXOffset; if (translateValue > -mScreenXOffset) { page.setTranslationX(translateValue); } else { page.setTranslationX(0); } } } }
  3. ESTRATÉGIA “Mentir” dizendo que existem muitos itens no adapter (ilusão

    de infinito....) Carregar e destruir itens a partir das posições virtuais, sobreescresvendo instantiateItem( ) e destroyItem( )
  4. public class CircularViewPager extends ViewPager { public CircularViewPager(Context context) {

    super(context); } public CircularViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override public void setAdapter(PagerAdapter adapter) { super.setAdapter(adapter); setCurrentItem(0); } @Override public void setCurrentItem(int item) { item = getOffsetAmount() + (item % getAdapter().getCount()); super.setCurrentItem(item); } private int getOffsetAmount() { if (getAdapter() instanceof InfinitePagerAdapter) { InfinitePagerAdapter adapter = (InfinitePagerAdapter) getAdapter(); return adapter.getRealCount() * 200; // BIG ENOUGH? } else { return 0; } } }
  5. public class CircularPagerAdapter extends PagerAdapter { private PagerAdapter adapter; public

    CircularPagerAdapter(PagerAdapter adapter) { this.adapter = adapter; } @Override public int getCount() { return Integer.MAX_VALUE; } public int getRealCount() { return adapter.getCount(); } @Override public Object instantiateItem(ViewGroup container, int position) { int virtualPosition = position % getRealCount(); return adapter.instantiateItem(container, virtualPosition); } @Override public void destroyItem(ViewGroup container, int position, Object object) { int virtualPosition = position % getRealCount(); adapter.destroyItem(container, virtualPosition, object); } [...] // Chamar outros métodos associados a esse adapter }