android studio : kotlin RecyclerViewの使いかた メモ書き

Adapterを作ってそれをActivityで使えるようにする。
あとXMLにも書き忘れないように。

MyAdapter.kt

class MyAdapter(my_list: List<MyObject>private val context: Context): RecyclerView.Adapter<MyAdapter.MyViewHolder>(){
//MyObjectの変数はActivityで定義
private val my_list = mutableListOf<MyObject>()

class MyHolder(itemView: View):RecyclerView.ViewHolder(itemView){
//繰り返し表示したいButtonやTextViewなどのviewを定義
//今回のitemViewは下のmy_item.xml
val mybutton = itemView.my_button
val mytext = itemView.my_text
}
data class MyObject(
val button_string:String,
val text_string:String
)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val itemView = LayoutInflater.from(context).inflate(R.layout.my_item,parent,false)
return MyViewHolder(itemView)
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val item = my_list[position]
holder.my_button.setOnClickListener(){
holder.my_button.text = "dfsdfsd"
}
holder.my_text.text = "aaaaaa"
}

override fun getItemCount(): Int {
return my_list.size
}

fun getId(position:Int):Int{
return my_list[position].id
}
}

my_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView
android:id="@+id/my_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</RelativeLayout>

TestActivity

class TestActivity:AppCompatActivity() {
//上で定義しておいたMyObjectに変数を入れる
private var my_list = listOf(
MyAdapter.MyObject("nocnxond","adsfadsf"),
MyAdapter.MyObject("nod","nczx,mcsf"),
MyAdapter.MyObject("norond","hjlkfgjo"),
MyAdapter.MyObject("nocnwkdmsxond","adpocxf")
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_test)
val my_recyclerview = findViewById<RecyclerView>(R.id.my_recyclerview)
val layoutManager = LinearLayoutManager(applicationContext)
my_recyclerview.layoutManager = layoutManager
adapter = MyAdapter(my_list,this)
my_recyclerview.adapter = adapter
}
}

activity_test.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 横に並べたい場合はandroid:orientation="vertical"など -->
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/my_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/deadline_date"
app:layout_constraintBottom_toTopOf="@id/place"/>
</androidx.constraintlayout.widget.ConstraintLayout>

*****別のプログラムから引っ張ってきて変数を変えたりしたので変数名が正しくないかもしれない。*****

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA