Recyclerview
Recyclerview with kotlin - Kotlin Android example
DTO
data class Subdata(val name: String, val imgurl: String) {}
ListItemsActivity.kt
class ListItemsActivity : AppCompatActivity() {
lateinit var recyclerView: RecyclerView
lateinit var listAdpter: ListAdpter
internal var adapterlists = ArrayList<Subdata>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_list_items)
//recycler recyclerView = findViewById(R.id.recyclerView) as RecyclerView
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.addOnItemTouchListener(RecyclerItemClickListener(this, recyclerView, object : RecyclerItemClickListener.OnItemClickListener {
override fun onItemClick(view: View, position: Int) {
}
override fun onLongItemClick(view: View?, position: Int) {
}
}))
listAdpter = ListAdpter(this@ListItemsActivity, adapterlists)
recyclerView.adapter = listAdpter
}
}
ListAdpter.kt
class ListAdpter(internal var context: Context, internal var list: ArrayList<Subdata>) : RecyclerView.Adapter<ListAdpter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.row_list_item, parent, false)
return MyViewHolder(itemView)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val subdata : Subdata= list[position]
holder.title.text = ""+subdata.name if(!TextUtils.isEmpty(subdata.imgurl))
{
Picasso.with(context)
.load(subdata.imgurl)
.placeholder(R.drawable.bg)
.into(holder.img);
}
else {
holder.img.setImageResource(R.drawable.bg)
}
}
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var title: TextView
var img: ImageView
init {
title = itemView.findViewById(R.id.title) as TextView
img = itemView.findViewById(R.id.img) as ImageView
val face = Typeface.createFromAsset(context.assets, "font.otf")
title.typeface = face
}
}
override fun getItemCount(): Int {
return list.size }
}
Comments
Post a Comment