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

Adapter and Custom Layout

Tatsuya Arai
February 18, 2015

Adapter and Custom Layout

Tatsuya Arai

February 18, 2015
Tweet

More Decks by Tatsuya Arai

Other Decks in Programming

Transcript

  1. "EBQUFSBOE $VTUPN-BZPVU QPUBUPUJQT

  2. @cutmail • Fablic, Inc. • Co-Founder • Mobile Application Engineer

  3. Adapterͷߴ଎Խ

  4. Adapterͷߴ଎Խ • ViewHolder

  5. ViewHolder • Adapterͷߴ଎Խख๏ • Adapter#getView()ͰfindViewById()ͷճ਺ΛݮΒ͢ (ViewΛ࢖͍·Θ͢)

  6. public class ItemAdapter extends ArrayAdapter<Item> { private LayoutInflater inflater; private

    List<Item> items; @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { convertView = inflater.inflate(R.layout.list_item, parent, false); viewHolder = new ViewHolder(); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } Item item = getItem(position); viewHolder.titleView.setText(item.getItemName()); return convertView; } public static class ViewHolder { ImageView thumbnail; TextView titleView; public ViewHolder(View view) { this.thumbnail = (ImageView) view.findViewById(R.id.thumbnail); this.titleView = (TextView) view.findViewById(R.id.title); } } } 7JFXͷࢀরΛอ࣋ ॳճͷΈ7JFX)PMEFSΛੜ੒
  7. ෳ਺ͷҟͳΔϨΠΞ΢τ 5JUMF 5JUMF 5JUMF 5JUMF %BUF %BUF %BUF

  8. ViewType @Override public int getViewTypeCount() { return 2; } @Override

    public int getItemViewType(int position) { if (position % 1 == 0) { return VIEW_TYPE_ODD; } else { return VIEW_TYPE_EVEN; } } ϨΠΞ΢τͷछྨ QPTJUJPOʹରͯ͠ͲͷϨΠΞ΢τΛ࢖͏͔
  9. ෳ਺ϨΠΞ΢τ(ViewHolder) public static class ViewHolderOdd { TextView title; ImageView image;

    } public static class ViewHolderEven { TextView title; TextView detail; ImageView image; }
  10. ෳ਺ϨΠΞ΢τ(ViewHolder൛) @Override public View getView(int position, View convertView, ViewGroup parent)

    { ViewHolder1 viewHolder1; ViewHolder2 viewHolder2; Item item = getItem(position); switch (getItemViewType(position)) { case VIEW_TYPE_ODD: if (convertView == null) { convertView = inflater.inflate(R.layout.list_item_1, parent, false); viewHolder1 = new ViewHolder(convertView); convertView.setTag(viewHolder1); } else { viewHolder1 = 1(ViewHolder) convertView.getTag(); } holder.title.setText(item.getName()); Picasso.with(getContext()) .load(item.getImageUrl) .placeholder(R.drawable.icon_user) .error(R.drawable.icon_user) .resizeDimen(R.dimen.list_item_user_icon_size, R.dimen.list_item_user_icon_size) .centerCrop() .into(holder.imageView); break; case VIEW_TYPE_EVEN: ɾɾɾ ɾɾɾ return convertView; } ௕͍
  11. AdapterͷංେԽ • ViewHolder͸Adapter͕ංେ͠΍͍͢ • ෳ਺ϨΠΞ΢τରԠ͢Δͱ͞ΒʹංେԽ • Adapterͷ໾ׂ͸Viewͱදࣔσʔλͷbinding • Viewͷදࣔॲཧ͸֤ViewΫϥε͕ॲཧ͢Δ΂͖

  12. Adapterͷߴ଎Խ • ViewHolder • Custom Layout

  13. Custom Layoutͷಋೖ

  14. LayoutΛܧঝͯ͠Custom LayoutΛ࡞੒ public class NormalLayout extends LinearLayout { TextView titleView;

    ImageView imageView; @Override protected void onFinishInflate() { super.onFinishInflate(); titleView = (TextView) findViewById(R.id.title); imageView = (ImageView) findViewById(R.id.image); } public void setItem(Item item) { titleView.setText(item.getItemName()); Picasso.with(getContext()) .load(item.getImageUrl) .placeholder(R.drawable.icon_user) .error(R.drawable.icon_user) .resizeDimen(R.dimen.icon_size, R.dimen.user_icon_size) .centerCrop() .into(holder.imageView); } } PO'JOJTI*OqBUF ͕ݺ͹ΕͨޙʹpOE7JFX#Z*E ͢Δ͜ͱͰ 7JFX)PMEFSͱಉ༷ͷޮՌ͕ಘΒΕΔ
  15. @Override public View getView(int position, View convertView, ViewGroup parent) {

    NormalLayout normalLayout; SpecialLayout specialLayout; Item item = getItem(position); switch (getItemViewType(position)) { case VIEW_TYPE_ODD: if (convertView != null && (convertView instanceof NormalLayout)) { normalLayout = (NormalLayout) convertView; } else { normalLayout = (NormalLayout) inflater.inflate(R.layout.list_item_odd, parent, false); } normalLayout.setItem(item); return normalLayout; case VIEW_TYPE_EVEN: ɾɾɾ return specialLayout; } return null; } ෳ਺ϨΠΞ΢τ(Custom Layout൛) -BZPVUपΓͷίʔυ͕ݮ͖ͬͯͬ͢Γ
  16. ෳ਺ϨΠΞ΢τ(ViewHolder൛) @Override public View getView(int position, View convertView, ViewGroup parent)

    { ViewHolder1 viewHolder1; ViewHolder2 viewHolder2; Item item = getItem(position); switch (getItemViewType(position)) { case VIEW_TYPE_ODD: if (convertView == null) { convertView = inflater.inflate(R.layout.list_item_1, parent, false); viewHolder1 = new ViewHolder(convertView); convertView.setTag(viewHolder1); } else { viewHolder1 = 1(ViewHolder) convertView.getTag(); } holder.title.setText(item.getName()); Picasso.with(getContext()) .load(item.getImageUrl) .placeholder(R.drawable.icon_user) .error(R.drawable.icon_user) .resizeDimen(R.dimen.list_item_user_icon_size, R.dimen.list_item_user_icon_size) .centerCrop() .into(holder.imageView); break; case VIEW_TYPE_EVEN: ɾɾɾ ɾɾɾ return convertView; }
  17. ϦετͷදࣔཁૉΛमਖ਼Λ͢Δࡍ͸ AdapterͰ͸ͳ͘ɺ Custom LayoutΫϥεΛमਖ਼͢Ε͹OK

  18. ଞͷAdapterͰCustom Layout Λ࢖͏͜ͱ΋Մೳ

  19. (SJE7JFX΋ಉ༷ʹରԠՄೳ

  20. ·ͱΊ • Custom LayoutΛ࢖͏͜ͱͰAdapterͷංେԽ͕๷ ͛Δ

  21. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ