Skip to content

Instantly share code, notes, and snippets.

@taktamur
Created November 13, 2018 07:04
Show Gist options
  • Save taktamur/2ed0e064a1e95c403c574887dbfb0298 to your computer and use it in GitHub Desktop.
Save taktamur/2ed0e064a1e95c403c574887dbfb0298 to your computer and use it in GitHub Desktop.
RecyclerView
class MyViewHolder(val binding: CellTextBinding) : RecyclerView.ViewHolder(binding.root)
class MyRecycleAdapter(context: Context, val list:List<String>) : RecyclerView.Adapter<MyViewHolder>() {
private val layoutInflater = LayoutInflater.from(context)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val binding:CellTextBinding = DataBindingUtil.inflate(layoutInflater,R.layout.cell_text, parent, false)
return MyViewHolder(binding)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.binding.text = list[position]
}
}
class RecyclerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recycler)
val data = (0..100).map{
"No."+ it
}
recycleView.layoutManager = LinearLayoutManager(this)
recycleView.adapter = MyRecycleAdapter(this,data)
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textView:TextView = view.textView
}
class MyRecycleAdapter(private val context: Context, val list:List<String>) : RecyclerView.Adapter<MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val layoutInflater = LayoutInflater.from(context)
val view = layoutInflater.inflate(R.layout.cell_text, parent, false)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.textView.text = list[position]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment