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

ViewPager2 Preview

ViewPager2 Preview

ViewPager2 Preview in DroidKnight 2019 Seoul

omjoonkim

April 05, 2019
Tweet

More Decks by omjoonkim

Other Decks in Programming

Transcript

  1. 6TJOHXJUI7JFX viewPager.adapter = object : RecyclerView.Adapter<CardViewHolder>() { override fun onCreateViewHolder(

    parent: ViewGroup, viewType: Int ): CardViewHolder { return CardViewHolder(CardView(layoutInflater, parent)) } override fun onBindViewHolder(holder: CardViewHolder, position: Int) { holder.bind(cards[position]) } override fun getItemCount(): Int { return cards.size } }
  2. 6TJOHXJUI'SBHNFOU viewPager.adapter = object : FragmentStateAdapter(supportFragmentManager,lifecycle) { override fun getItem(position:

    Int): PageFragment { val itemId = items.itemId(position) val itemText = items.getItemById(itemId) return PageFragment.create(itemText) } override fun getItemCount(): Int = items.size override fun getItemId(position: Int): Long = items.itemId(position) override fun containsItem(itemId: Long): Boolean = items.contains(itemId) }
  3. 'SBHNFOU4UBUF"EBQUFS public abstract class FragmentStateAdapter extends RecyclerView.Adapter<FragmentViewHolder> implements StatefulAdapter public

    FragmentStateAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) { mFragmentManager = fragmentManager; mLifecycle = lifecycle; super.setHasStableIds(true); }
  4. 'SBHNFOU7JFX)PMEFS public final class FragmentViewHolder extends ViewHolder { private FragmentViewHolder(FrameLayout

    container) { super(container); } static FragmentViewHolder create(ViewGroup parent) { FrameLayout container = new FrameLayout(parent.getContext()); container.setLayoutParams( new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); container.setId(ViewCompat.generateViewId()); container.setSaveEnabled(false); return new FragmentViewHolder(container); } FrameLayout getContainer() { return (FrameLayout) itemView; } }
  5. 'SBHNFOU4UBUF"EBQUFSCJOE @Override public final void onBindViewHolder(final @NonNull FragmentViewHolder holder, int

    position) { final long itemId = holder.getItemId(); final int viewHolderId = holder.getContainer().getId(); final Long boundItemId = itemForViewHolder(viewHolderId); // item currently bound to the VH if (boundItemId != null && boundItemId != itemId) { removeFragment(boundItemId); mItemIdToViewHolder.remove(boundItemId); } mItemIdToViewHolder.put(itemId, viewHolderId); // this might overwrite an existing entry ensureFragment(position); ………………… }
  6. 'SBHNFOU4UBUF"EBQUFSCJOE @Override public final void onBindViewHolder(final @NonNull FragmentViewHolder holder, int

    position) { final long itemId = holder.getItemId(); final int viewHolderId = holder.getContainer().getId(); final Long boundItemId = itemForViewHolder(viewHolderId); // item currently bound to the VH if (boundItemId != null && boundItemId != itemId) { removeFragment(boundItemId); mItemIdToViewHolder.remove(boundItemId); } mItemIdToViewHolder.put(itemId, viewHolderId); // this might overwrite an existing entry ensureFragment(position); ………………… }
  7. 2)PXUPVTFXJUI5BC-BZPVU TabLayoutMediator(tabLayout, viewPager) { tab, position -> /* implement code

    */ }.attach() package com.example.androidx.viewpager2.TabLayoutMediator;