RecyclerViewのheightをアイテム数に応じて動的に変更したいのですが、アイテムの高さ取得などからわからないです。どのように書けばいいのか教えて頂きたいです。
XML
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#e0e0e0"> 6<androidx.recyclerview.widget.RecyclerView 7 android:id="@+id/trainerListRecycerView" 8 android:layout_width="match_parent" 9 android:layout_height="500dp" 10 app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" 11 app:layout_constraintEnd_toEndOf="parent" 12 app:layout_constraintHorizontal_bias="0.0" 13 app:layout_constraintStart_toStartOf="parent" 14 app:layout_constraintTop_toTopOf="parent" /> 15</androidx.constraintlayout.widget.ConstraintLayout> 16
Kotlin
1//フラグメント 2class TrainerListFragment : Fragment() { 3 4 private lateinit var trainerListViewModel: TrainerListViewModel 5 private lateinit var recyclerView: RecyclerView 6 private lateinit var trainerList: ArrayList<TrainerListAdapter.TrainerListItem> 7 private lateinit var trainerListAdapter: TrainerListAdapter 8 9 override fun onCreateView( 10 inflater: LayoutInflater, container: ViewGroup?, 11 savedInstanceState: Bundle? 12 ): View? { 13 trainerListViewModel = 14 ViewModelProvider(this).get(TrainerListViewModel::class.java) 15 val root = inflater.inflate(R.layout.list_trainer, container, false) 16 17 18 19 val testTrainer1 = TrainerListAdapter.TrainerListItem(R.drawable.ic_home, "トレーナー1", 1, 1, "カテゴリ1", "カテゴリ2") 20 val testTrainer2 = TrainerListAdapter.TrainerListItem(R.drawable.ic_calendar, "トレーナー2", 2, 2, "カテゴリ3", "カテゴリ4") 21 val testTrainer3 = TrainerListAdapter.TrainerListItem(R.drawable.ic_search, "トレーナー3", 2, 2, "カテゴリ5", "カテゴリ6") 22 23 trainerList = arrayListOf(testTrainer1, testTrainer2, testTrainer3) 24 25 recyclerView = root.findViewById(R.id.trainerListRecycerView) 26 trainerListAdapter = 27 TrainerListAdapter( 28 trainerList 29 ) 30 recyclerView.adapter = trainerListAdapter 31 recyclerView.isNestedScrollingEnabled = false 32 recyclerView.layoutManager = GridLayoutManager(activity,2) 33 return root 34 } 35 36}
Kotlin
1//アダプター 2class TrainerListAdapter(private val trainerList: ArrayList<TrainerListItem>): RecyclerView.Adapter<TrainerListAdapter.ViewHolder>() { 3 4 data class TrainerListItem( 5 val image: Int, 6 val name: String, 7 val subscribedNumber: Int, 8 val rank: Int, 9 val category1: String, 10 val category2: String 11 ) 12 13 // Viewの初期化 14 class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { 15 val image: ImageView = view.findViewById(R.id.trainerListItemImage) 16 val name: TextView = view.findViewById(R.id.trainerListItemName) 17 val subscribedNumber: TextView = view.findViewById(R.id.trainerListSubscribedNumber) 18 val rank: TextView = view.findViewById(R.id.trainerListRankNumber) 19 val category1: TextView = view.findViewById(R.id.trainerListItemCategory1) 20 val category2: TextView = view.findViewById(R.id.trainerListItemCategory2) 21 } 22 23 // レイアウトの設定 24 override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder { 25 val view = LayoutInflater.from(viewGroup.context).inflate(R.layout.item_trainer, viewGroup, false) 26 return ViewHolder( 27 view 28 ) 29 } 30 31 // Viewの設定 32 override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { 33 val trainer = trainerList[position] 34 35 viewHolder.image.setBackgroundResource(trainer.image) 36 viewHolder.name.text = trainer.name 37 viewHolder.subscribedNumber.text = trainer.subscribedNumber.toString() 38 viewHolder.rank.text = trainer.rank.toString() 39 viewHolder.category1.text = trainer.category1 40 viewHolder.category2.text = trainer.category2 41 42 43 viewHolder.itemView.setOnClickListener { v -> //ここにアイテムをクリックした際の挙動を記載 44 // activityを取得 45 val activity = v!!.context as TopActivity 46 // DetailFragmentを取得 47 val detail = DetailFragment() 48 49 activity.supportFragmentManager.beginTransaction().setCustomAnimations(R.anim.slide_in_right,R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right).replace(R.id.trainer_replace, detail).addToBackStack(null).commit() 50 51 52 } 53 } 54 55 // 表示数を返す 56 override fun getItemCount() = trainerList.size 57}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。