タイトルは釣りです。ごめんなさい。
昨日、ハマってしまったのでRecyclerViewについて整理したことをメモ。
RecyclerViewの最小構成
layoutファイル
activiity_main.xml 画面幅一杯のRecyclerViewを配置
xml version="1.0" encoding="utf-8"
<androidsupportconstraintConstraintLayout
xmlnsandroid="http://schemas.android.com/apk/res/android"
xmlnstools="http://schemas.android.com/tools"
xmlnsapp="http://schemas.android.com/apk/res-auto"
androidlayout_width="match_parent"
androidlayout_height="match_parent"
toolscontext=".MainActivity">
<androidsupportv7widgetRecyclerView
androidlayout_width="0dp"
androidlayout_height="0dp"
applayout_constraintEnd_toEndOf="parent"
applayout_constraintStart_toStartOf="parent"
applayout_constraintTop_toTopOf="parent"
applayout_constraintBottom_toBottomOf="parent"
androidid="@+id/recycler_view"/>
</androidsupportconstraintConstraintLayout>
layoutファイル RecyclerViewのitem
RecyclerViewのアイテム サンプルとしてImageViewとTextViewを配置
recycler_item.xml
xml version="1.0" encoding="utf-8"
<LinearLayout
xmlnsandroid="http://schemas.android.com/apk/res/android"
xmlnsapp="http://schemas.android.com/apk/res-auto"
androidorientation="vertical"
androidlayout_width="match_parent"
androidlayout_height="match_parent">
<ImageView
androidlayout_width="wrap_content"
androidlayout_height="wrap_content"
appsrcCompat="@drawable/glogo"
androidid="@+id/imageView"/>
<TextView
androidlayout_width="match_parent"
androidlayout_height="wrap_content" androidid="@+id/textView"/>
</LinearLayout>
Activity
Activity側の最小の実装
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recycler_view = findViewById<RecyclerView>(R.id.recycler_view)
recycler_view.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false)
class mViewHolder(item_view: View): RecyclerView.ViewHolder(item_view){
val textView = item_view.findViewById<TextView>(R.id.textView)
}
recycler_view.adapter = object : RecyclerView.Adapter<mViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, p1: Int): mViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.recycler_item, parent,false)
return mViewHolder(itemView)
}
override fun onBindViewHolder(holder: mViewHolder, p1: Int) {
holder.textView.setText( p1.toString())
}
override fun getItemCount(): Int {
return 3
}
}
}
}
ListViewと違うのは、layoutManagerをセットしないとadapterをセットしただけでは動作しません。
recycler_view.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false)
上記の実装ではLinearLayoutManager.HORIZONTALを指定したので、横スクロールします。
アイテムのサイズを調整する
私はアイテムサイズを変更の際、recycler_item.xmlをLinearLayoutで記述しており、何故か中のViewがloopしていると勘違いしていました。よって、中のView自体のサイズを変更すると以下のようなレイアウトになってしまいます。
ご覧のように画面幅一杯にitemが配置され、いくらviewサイズを小さくしてもpaddingが大きくなります。
itemのサイズを小さくするにはLinearLayoutのViewGroupがloopしているのでViewGroupのサイズを調整します。
xml version="1.0" encoding="utf-8"
<LinearLayout xmlnsandroid="http://schemas.android.com/apk/res/android"
xmlnsapp="http://schemas.android.com/apk/res-auto" androidorientation="vertical"
androidlayout_width="300dp"
androidlayout_height="match_parent">
<ImageView
androidlayout_width="wrap_content"
androidlayout_height="wrap_content" appsrcCompat="@drawable/glogo" androidid="@+id/imageView"/>
<TextView
androidlayout_width="match_parent"
androidlayout_height="wrap_content" androidid="@+id/textView" androidtextSize="36sp"/>
</LinearLayout>
少し頭を冷やせばすぐにitem自体がloopしていると気づくのですが、これに気づかずに長時間ハマってしまいまいた(^_^;)
アイテムのマージンを調整する
アイテムのマージンはRecyclerView.ItemDecoration
の実装をセットします。
val recycler_view = findViewById<RecyclerView>(R.id.recycler_view)
recycler_view.apply {
layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL, false)
addItemDecoration( object : RecyclerView.ItemDecoration(){
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
outRect.right = 20
}
})
}
このようにgetItemOffsetsにマージンの値を指定します。
リンク
リンク