Created
November 13, 2018 07:04
-
-
Save taktamur/2ed0e064a1e95c403c574887dbfb0298 to your computer and use it in GitHub Desktop.
RecyclerView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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