android kotlin try with a small app dev

package com.calo.kotlinlanglearning

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

private lateinit var txt01: TextView;
private lateinit var recycler01: RecyclerView;
private lateinit var dataRepo:ArrayList<recDataSrc>;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
txt01= findViewById(R.id.txt01)
recycler01=findViewById(R.id.recycler01);
recycler01.layoutManager=LinearLayoutManager(this)
dataRepo=ArrayList()//init an ArrayList
for (i in 1..20){
dataRepo.add(recDataSrc("btn"+i, "txt$i"))
}
val a: (Int) -> Unit ={ p->onListItemClick(p)}//Unit corresponds to void
recycler01.adapter=recDataAdapter(dataRepo,a)

}

fun btn01_Click(view: View) {
if(txt01.text.equals(resources.getString(R.string.hello_world))) {//1. getString using the resource.getString method to look up strings resource file
txt01.setText(R.string.hello_world_CN);
}else{
txt01.setText(R.string.hello_world);
}
dataRepo.removeLast()
recycler01.adapter?.notifyDataSetChanged()//this is used to refresh the recyclerview after data change
}

private fun onListItemClick(position: Int){//2. method returns Unit which is void replacement
val cur=dataRepo[position]

if(txt01.text.equals(resources.getString(R.string.hello_world))) {//1. getString using the resource.getString method to look up strings resource file
txt01.setText(R.string.hello_world_CN);
}else{
txt01.setText(R.string.hello_world);
}
}



}

=============main activity above

package com.calo.kotlinlanglearning

import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.Toast

class recDataAdapter(private val mList: List<recDataSrc>, private val onItemClick: (position:Int)->Unit) : RecyclerView.Adapter<recDataAdapter.ViewHolder>() {


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.reclistitem, parent, false)
return ViewHolder(view,onItemClick)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

val ItemsViewModel = mList[position]

// holder.imageView.setImageResource(ItemsViewModel.image)

holder.btn.text=ItemsViewModel.btnText
holder.textView.text = ItemsViewModel.text

}

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

class ViewHolder(itemView: View,private val onItemClick: (position:Int)->Unit) : RecyclerView.ViewHolder(itemView),View.OnClickListener {
val textView: TextView = itemView.findViewById(R.id.textView)
val btn: Button=itemView.findViewById(R.id.recitembtn01)


init {
itemView.setOnClickListener(this)
btn.setOnClickListener(this)//4. the btn should bind to a listener currently this one
}

override fun onClick(p0: View?) {
val id=p0?.id
if(id==R.id.recitembtn01){
val a=5//3.this is hit when button in the item is clicked
}
val position = adapterPosition //5. this still gets the value when the button is clicked
onItemClick(position)
}


}


}

============== recyclerview adapter code above

package com.calo.kotlinlanglearning

data class recDataSrc(val btnText:String,val text:String)
============== recyclerview model code above

Summary:
kotlin is somehow briefer than java, and this recyclerview explore addreses how to use item click, as well as button
cclick event inside of a recycler view item


留下你的评论