げんざいKotlin勉強中のものです。RecycViewを使いこなそうと勉強しているのですが、まだ構造が理解し切れていません。
僕の理想は下の写真のように画面に要素がいっぱい出て欲しいのです
どうなっているのかと言いますとこれは写真で見やすくするため少しずらしていますが、なぜか1つの要素が画面いっぱいになっています
この要素一つの高さを文字列の高さと同じ程度にしたいのですがかないません。
どなたか詳しい方コードを訂正して教えていただけると助かります><
下に今回使ったコードを載せておきます。
よろしくお願いします!
Kotlin
1package com.example.recyclerview 2 3import android.graphics.Color 4import androidx.appcompat.app.AppCompatActivity 5import android.os.Bundle 6import android.view.LayoutInflater 7import android.view.View 8import android.view.ViewGroup 9import android.widget.TextView 10import androidx.recyclerview.widget.LinearLayoutManager 11import androidx.recyclerview.widget.RecyclerView 12 13class MainActivity : AppCompatActivity() { 14 override fun onCreate(savedInstanceState: Bundle?) { 15 super.onCreate(savedInstanceState) 16 setContentView(R.layout.activity_main) 17// リサイクラーViewに設定していく。まずはrecyclervireをonCreateに引っ張ってくる。 18 val recyclerView = findViewById<RecyclerView>(R.id.recyclerView) 19// アダプターがクラスのままなので、インスタンス化する 20 val adapter = MyAdapter() 21// リサイクラーViewを作るにあたって必要なレイアウトマネージャー(どういう並べ方にするかを指定する) 22// コンストラクターの第一引数にcontextを入れる。contextはアクティビティの親クラスなのでthis 23 recyclerView.layoutManager = LinearLayoutManager(this) 24// アダプターをリサイクラーに設定する。 25 recyclerView.adapter = adapter 26 27// 要素がまだセットされていないので要素をセットする 28// mutableListOfで変更可能なリストが作れる 29 val textList = mutableListOf<String>() 30 for (i in 0..100) { 31 textList.add("要素$i") 32 } 33 adapter.textlist = textList 34// アダプターの中身を変えた時はお約束でこれを呼ぶ。呼ばないとリサイクラーViewに表示されているリスト内容が更新されない 35 adapter.notifyDataSetChanged() 36 } 37} 38 39private class MyAdapter : RecyclerView.Adapter<MyViewHolder>() { 40// リストの中に入れるものをこのクラスの中に保持する必要があるので作成初期値は空にしたいのでemptyList()にする。 41 var textlist: List<String> = emptyList() 42 43// 一行一行のレイアウト 44 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { 45// xmlでデザインされたものをViewに変換する。inflaterを作成する 46// contextはViewGroupが持っている 47 val inflater = LayoutInflater.from(parent.context) 48 49// inflaterを使ってxmlをViewに変換する 50// 2つ目の引数はレイアウトの親Viewにあたるインスタンスを指定する。(お作法でparent) 51// 3つめは今回falseを入れる(詳しい説明はなし) 52 val view = inflater.inflate(R.layout.activity_main, parent, false) 53 54// viewは作ったがこのメソッドはviewではなくviewholderを返して欲しい 55// なのでしたのMyViewHolderを使ってviewholderを作成する 56 return MyViewHolder(view) 57 } 58 59// viewにデータを設定するためのメソッド 60 override fun onBindViewHolder(holder: MyViewHolder, position: Int) { 61 holder.textView.text = textlist[position] 62// itemViewをするとtextViewの親viewを参照することができる 63 holder.itemView.setBackgroundColor( 64 if (position % 2 == 0) { 65 Color.WHITE 66 } else { 67 Color.LTGRAY 68 } 69 ) 70 } 71 72// リストの総数はいくつ必要か 73 override fun getItemCount(): Int { 74// これでListの中身の総数を設定できる 75 return textlist.size 76 } 77 78} 79 80private class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { 81 val textView = itemView.findViewById<TextView>(R.id.textView) 82}
ここからレイアウトのコード
layout/activity_main.xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <androidx.recyclerview.widget.RecyclerView 10 android:id="@+id/recyclerView" 11 android:layout_width="match_parent" 12 android:layout_height="match_parent" 13 app:layout_constraintBottom_toBottomOf="parent" 14 app:layout_constraintEnd_toEndOf="parent" 15 app:layout_constraintHorizontal_bias="0.0" 16 app:layout_constraintStart_toStartOf="parent" 17 app:layout_constraintTop_toTopOf="parent" 18 app:layout_constraintVertical_bias="0.0" /> 19 20 <TextView 21 android:id="@+id/textView" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="TextView" 25 app:layout_constraintStart_toStartOf="parent" 26 app:layout_constraintTop_toTopOf="parent" /> 27 28</androidx.constraintlayout.widget.ConstraintLayout>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/04/13 06:46