ViewPager2 Preview

ViewPager2 Preview

ViewPager2 Preview in DroidKnight 2019 Seoul

A82a37a81ebdd732cb60c33d868382fa?s=128

omjoonkim

April 05, 2019
Tweet

Transcript

  1. 2.
  2. 6.
  3. 29.
  4. 37.
  5. 38.

    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 } }
  6. 39.

    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) }
  7. 40.

    '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); }
  8. 42.

    '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; } }
  9. 43.

    '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); ………………… }
  10. 44.

    '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); ………………… }
  11. 45.
  12. 48.

    2)PXUPVTFXJUI5BC-BZPVU TabLayoutMediator(tabLayout, viewPager) { tab, position -> /* implement code

    */ }.attach() package com.example.androidx.viewpager2.TabLayoutMediator;
  13. 49.
  14. 51.