実現したいこと
ListAdapterを使ったRecyclerViewで複数のViewHolderを使って、Viewの表示を行いたい。
詰まっている点
2種類(ヘッダー、フッター)のViewHolderがあるが、1つ目にlistに追加したデータ(ヘッダー用ビュー)分しか表示されない。ビルドは通っている。
環境
Android Studio 3.3.2
compileSdkVersion 28
targetSdkVersion 28
minSdkVersion 16
recyclerview-v7:28.0.0
コード
Adapter
1package sample.com.recyclerviewtest 2 3import android.content.Context 4import android.support.v7.recyclerview.extensions.ListAdapter 5import android.support.v7.util.DiffUtil 6import android.support.v7.widget.RecyclerView 7import android.view.LayoutInflater 8import android.view.View 9import android.view.ViewGroup 10import android.widget.TextView 11 12 13private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Any>() { 14 override fun areItemsTheSame(oldItem: Any, newItem: Any): Boolean = oldItem == newItem 15 override fun areContentsTheSame(oldItem: Any, newItem: Any): Boolean = oldItem == newItem 16} 17private val dataList: MutableList<Any> = mutableListOf() 18 19class MyAdapter( 20 context: Context, data: MutableList<String> 21) : ListAdapter<Any, RecyclerView.ViewHolder>(DIFF_CALLBACK) { 22 23 init { 24 dataList.add(HeaderData(title = "headerだよ")) 25 dataList.add(FooterData(text = "footerだよ")) 26 submitList(dataList.toList()) 27 } 28 29 private val layoutInflater = LayoutInflater.from(context) 30 31 override fun getItemCount(): Int = dataList.size 32 33 override fun getItemViewType(position: Int): Int { 34 return when (getItem(position)) { 35 is HeaderData -> R.layout.view_header 36 is FooterData -> R.layout.view_footer 37 else -> throw AssertionError() 38 } 39 } 40 41 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { 42 return when (viewType) { 43 R.layout.view_header -> { 44 HeaderViewHolder(layoutInflater.inflate(R.layout.view_header, parent, false)) 45 } 46 R.layout.view_footer -> { 47 FooterViewHolder(layoutInflater.inflate(R.layout.view_footer, parent, false)) 48 } 49 else -> { 50 throw IllegalArgumentException() 51 } 52 } 53 } 54 55 override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) { 56 val item = getItem(position) 57 when { 58 viewHolder is HeaderViewHolder && item is HeaderData -> { 59 viewHolder.bind(item) 60 } 61 viewHolder is FooterViewHolder && item is FooterData -> { 62 viewHolder.bind(item) 63 } 64 } 65 } 66 67 private class HeaderData( 68 val title: String 69 ) 70 71 private inner class HeaderViewHolder(view: View) : 72 RecyclerView.ViewHolder(view) { 73 val header: TextView = view.findViewById(R.id.header) 74 75 fun bind(data: HeaderData) { 76 header.text = data.title 77 } 78 } 79 80 private class FooterData( 81 val text: String 82 ) 83 84 private inner class FooterViewHolder(view: View) : 85 RecyclerView.ViewHolder(view) { 86 val footer: TextView = view.findViewById(R.id.footer) 87 88 fun bind(data: FooterData) { 89 footer.text = data.text 90 } 91 } 92}
view_header.xml
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/header" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> </android.support.constraint.ConstraintLayout> </layout>
view_footer.xml
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/footer" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> </android.support.constraint.ConstraintLayout> </layout>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。