Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

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. 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Λੜ੒
  2. 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ʹରͯ͠ͲͷϨΠΞ΢τΛ࢖͏͔
  3. ෳ਺ϨΠΞ΢τ(ViewHolder) public static class ViewHolderOdd { TextView title; ImageView image;

    } public static class ViewHolderEven { TextView title; TextView detail; ImageView image; }
  4. ෳ਺ϨΠΞ΢τ(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; } ௕͍
  5. 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ͱಉ༷ͷޮՌ͕ಘΒΕΔ
  6. @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पΓͷίʔυ͕ݮ͖ͬͯͬ͢Γ
  7. ෳ਺ϨΠΞ΢τ(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; }