LoginSignup
13
14

More than 5 years have passed since last update.

RecyclerView.Adapterのライフサイクル

Posted at

はじめに

RecyclerViewのAdapterのライフサイクルを調べてみました。
僕のメモ書き程度に残してますので参考までに確認してください。

ログ

以下にAdapterを回した時のログを記載します。

D/HomeStarHorseRecyclerAdapter: HomeStarHorseRecyclerAdapter getItemCount
D/HomeStarHorseRecyclerAdapter: HomeStarHorseRecyclerAdapter getItemCount
D/HomeStarHorseRecyclerAdapter: HomeStarHorseRecyclerAdapter getItemCount
D/HomeStarHorseRecyclerAdapter: HomeStarHorseRecyclerAdapter getItemViewType position:0
D/HomeStarHorseRecyclerAdapter: HomeStarHorseRecyclerAdapter onCreateViewHolder parent:android.support.v7.widget.RecyclerView{e836243 VFED.... ......ID 0,42-720,1130 #7f0d0091 app:id/my_recycler_view2} viewType:0
D/HomeStarHorseRecyclerAdapter: HomeStarHorseRecyclerAdapter onBindViewHolder vh:ViewHolder{20c8d53e position=0 id=-1, oldPos=-1, pLpos:-1 no parent} position:0
D/HomeStarHorseRecyclerAdapter: HomeStarHorseRecyclerAdapter getItemCount
D/HomeStarHorseRecyclerAdapter: HomeStarHorseRecyclerAdapter getItemViewType position:1
D/HomeStarHorseRecyclerAdapter: HomeStarHorseRecyclerAdapter onCreateViewHolder parent:android.support.v7.widget.RecyclerView{e836243 VFED.... ......ID 0,42-720,1130 #7f0d0091 app:id/my_recycler_view2} viewType:0
D/HomeStarHorseRecyclerAdapter: HomeStarHorseRecyclerAdapter onBindViewHolder vh:ViewHolder{30e274b5 position=1 id=-1, oldPos=-1, pLpos:-1 no parent} position:1
D/HomeStarHorseRecyclerAdapter: HomeStarHorseRecyclerAdapter getItemCount
D/HomeStarHorseRecyclerAdapter: HomeStarHorseRecyclerAdapter getItemViewType position:2
D/HomeStarHorseRecyclerAdapter: HomeStarHorseRecyclerAdapter onCreateViewHolder parent:android.support.v7.widget.RecyclerView{e836243 VFED.... ......ID 0,42-720,1130 #7f0d0091 app:id/my_recycler_view2} viewType:0
D/HomeStarHorseRecyclerAdapter: HomeStarHorseRecyclerAdapter onBindViewHolder vh:ViewHolder{13697d8 position=2 id=-1, oldPos=-1, pLpos:-1 no parent} position:2

処理の流れ

ListViewのAdapterとほぼ変わりませんね。
違う部分としては「ViewType」部分ぐらいかなぁって思ってます。
(すべてのメソッドをオーバーライドしてないので適当)

No メソッド 処理内容
1 getItemCount RecyclerViewで表示するアイテムの個数
2 getItemViewType アイテムのViewTypeを設定する。引数に「position」があるので個数で判断する
3 onCreateViewHolder Layoutを設定する
4 onBindViewHolder Layoutの画像や文字を設定する

所感

RecyclerViewをカスタムしていくを考えるとNo2の「getItemViewType」をうまく作っていく必要があると感じました。

13
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14