2017年8月10日木曜日

Kotlin メモ : ArrayAdapter

Java class MyAdapter extends ArrayAdapter<MyData> { private final LayoutInflater inflater; MyAdapter(Context context, List<MyData> objects) { super(context, 0, objects); inflater = LayoutInflater.from(context); } ... @NonNull @Override public View getView(int position, View convertView, @NonNull ViewGroup parent) { final MyViewHolder holder; if (convertView == null) { holder = MyViewHolder.create(inflater, parent); convertView = holder.view; convertView.setTag(holder); } else { holder = (MyViewHolder) convertView.getTag(); } final MyData data = getItem(position); assert data != null; holder.bind(data); return convertView; } } Kotlin class MyAdapter(context: Context, objects: List<MyData>) : ArrayAdapter<MyData>(context, 0, objects) { private val inflater = LayoutInflater.from(context) ... override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { val view: View = convertView ?: MyViewHolder.create(inflater, parent) .also { it.view.tag = it } .view getItem(position)?.let { (view.tag as MyViewHolder).bind(it) } return view } }

0 件のコメント:

コメントを投稿