Second Activity
class BookDescription : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_book_description)
var books = intent.getParcelableExtra("String") as Book
Glide.with(this).load(books.imageUrl).into(bookImg1)
nameTxt1.text = books.name
autorTxt1.text = books.name
} }
Adapter class
class Adapter(private val context: Context) :
RecyclerView.Adapter<Adapter.Holder>() {
private var datalist = mutableListOf<Book>()
fun setListdata(data: MutableList<Book>){
datalist = data
}
inner class Holder(itemView : View) : RecyclerView.ViewHolder(itemView){
fun bindView(book: Book){
Glide.with(context).load(book.imageUrl).into(itemView.bookImg)
itemView.nameTxt.text = book.name
itemView.autorTxt.text= book.writer
itemView.bookImg.setOnClickListener(
View.OnClickListener {
val intent = Intent(context, BookDescription::class.java)
intent.putExtra("jbg", book)
context.startActivity(intent)
}
)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
val view = LayoutInflater.from(context).inflate(R.layout.book_format, parent,
false )
return Holder(view)
}
override fun onBindViewHolder(holder: Holder, position: Int) {
val book = datalist[position]
holder.bindView(book)
}
override fun getItemCount(): Int {
return if (datalist.size> 0){
datalist.size
}else{
0
}
} }
Main Activity
class MainActivity : AppCompatActivity() {
private lateinit var adapter : Adapter
private val viewModel by lazy { ViewModelProviders.of(this).get(MainViewModel::class.java)}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
adapter = Adapter(this)
recycle.layoutManager = GridLayoutManager(this,2)
recycle.adapter = adapter
observerData()
}
fun observerData(){
viewModel.fetchUserData().observe(this,Observer{
adapter.setListdata(it)
adapter.notifyDataSetChanged()
})
}
}
Class Book
#Parcelize data class Book(val imageUrl:String= "URL IMmage",
val name:String = "Naziv knjige",
val writer:String= "Pisac knjige") :Parcelable!
*[Logcat]**(https://i.stack.imgur.com/Wnt2O.jpg)
You need to make sure that your Book class implements Parcelable, and then make sure that the key used to put the extra matches the key used to retrieve it:
Adapter
val intent = Intent(context, BookDescription::class.java)
intent.putParcelable("UNIQUE_KEY", book)
context.startActivity(intent)
BookDescription
var books = intent.getParcelableExtra("UNIQUE_KEY") as Book
Related
I'm new to Kotlin. I have a MainActivity and a list view inside.
When an item is clicked in the list view, I want to update a textView inside MainActivity.
However because data should be passed from ListView to MainActivity, I did something weird:
MainActivity.kt:
data class Country(val imgRes:Int, val name:String)
class MainActivity : AppCompatActivity() {
private lateinit var binding : ActivityMainBinding
private val data = arrayListOf<Country>()
val imgRes = intArrayOf()
val data1 = arrayOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
for(i in imgRes.indices) {
val country = Country(imgRes[i], data1[i])
data.add(country)
}
val adapter = RecyclerAdapter(data, object:RecyclerAdapter.OnItemClickListener {
override fun onItemClick(v: View, pos: Int) {
binding.textView.text = (v as TextView).text
}
})
binding.recycler1.adapter = adapter
binding.recycler1.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
}
}
ListView.kt:
class RecyclerAdapter(private val dataSet:List<Country>, private val listener:OnItemClickListener) : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
public interface OnItemClickListener {
fun onItemClick(v:View, pos:Int)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = RowBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
}
override fun getItemCount(): Int {
return dataSet.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(position)
}
inner class ViewHolder(val binding: RowBinding) : RecyclerView.ViewHolder(binding.root) {
val rowImageView = binding.rowImageView
val rowTextView = binding.rowTextView
init {
binding.root.setOnClickListener {
listener.onItemClick(rowTextView, adapterPosition)
}
}
fun bind(pos: Int) {
with(binding) {
rowImageView.setImageResource(dataSet[pos].imgRes)
rowTextView.text = dataSet[pos].name
}
}
}
}
I declared interface(OnItemClickListener) at ListView.kt and redefine it at MainActivity.kt.
It works fine but is there any better way to do this?
You can use higher order functions instead of interface.
Define this function top of your Adapter:
var onClick: ((text: String) -> Unit)? = null
in ViewHolder:
binding.root.setOnClickListener {
onClick?.invoke(dataSet[position].text)
}
in MainActivity:
adapter.onClick = { name ->
binding.textView.text = name
}
You can also find more information about higher orders in here
<include android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/yourlayout" />
Try this in your Adapter:
var itemClickListener: ((position: Int, name: String) -> Unit)? = null
Bindviewholder:
itemClickListner.invoke(1,"anyvalue")
in Activity:
adapter.itemClickListener = {
position, name ->
Toast.makeText(requireContext(),"position is $position name is $name ",Toast.LENGTH_SHORT).show()
}
in Adapter:
private var mListener: OnItemClickListener,
interface OnItemClickListener {
fun onClick(position: Int, routerArray: ArrayList<Router>)
}
binding.yourView.setOnClickListener {
mListener.onClick(adapterPosition, routerArray)
}
in Activity:
override fun onClick(position: Int, yourArray: ArrayList<Model>) {
binding.textView.text = yourArray[position].text
}
im facing errors in recyclerView here is //
data class categories(
val Name: String,
val Image:Int
)
here is model /////
object CategoriesModel {
fun getImages(): ArrayList<categories> {
val ImagesList = ArrayList<categories>()
val categoryFirst = categories(
"Memmals",R.drawable.memmals
)
ImagesList.add(categoryFirst)
val categorySecond = categories(
"Memmals",R.drawable.fish
)
ImagesList.add(categorySecond)
return ImagesList
}
}
//// adapter///
class CategoriesAdapter(private val itemList: ArrayList) :
RecyclerView.Adapter<CategoriesAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoriesAdapter.ViewHolder {
val view =LayoutInflater.from(parent.context)
.inflate(R.layout.memmals, parent,false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: CategoriesAdapter.ViewHolder, position: Int) {
holder.bind(categories("Memmals",2))
}
override fun getItemCount(): Int {
return itemList.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(category: categories) {
val textViewName = itemView.findViewById<TextView>(R.id.txtCategory)
textViewName.text = category.Name
val txtImage=itemView.findViewById<View>(R.id.image)
txtImage.id=category.Image
}
}
}
/////main activity///
class MainActivity : AppCompatActivity() {
private lateinit var mAdapter: CategoriesAdapter
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding= ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.recyclerView.setOnClickListener{
val intent=Intent(this,Level1::class.java)
startActivity(intent)
}
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
recyclerView.layoutManager = GridLayoutManager(this,2)
val itemList = ArrayList<CategoriesModel>()
// from here
itemList.add(categories("Memmals",R.drawable.memmals))
itemList.add(categories("Fishes",R.drawable.fish))
itemList.add(categories("Arthropods",R.drawable.dear))
itemList.add(categories("Yalk",R.drawable.yalk))
//to here errors are there
//Adapter setting
mAdapter = CategoriesAdapter(itemList)
recyclerView.adapter = mAdapter
}
}
/// here is the error///
MainActivity.kt: (34, 22): Type mismatch: inferred type is categories but CategoriesModel was expected
MainActivity.kt
val itemList = ArrayList<CategoriesModel>()
should be changed to
val itemList = ArrayList<categories>()
because you are using CategoriesModel object and expected is categories.
I am fetching JSON data from API and passing it in recycler view but if I want to fetch new data and display it in recycler view then I have to clear the list and then add new data in that list and notify the adapter that the data is changed but it is not updated what should I do?
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var recipeViewModel: RecipeViewModel
private lateinit var mainBinding: ActivityMainBinding
private lateinit var recipeAdapter: RecipeAdapter
private lateinit var recipeItemList: ArrayList<Hit>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(mainBinding.root)
recipeViewModel =
ViewModelProvider(
this,
ViewModelProvider.AndroidViewModelFactory
.getInstance(application)
)[RecipeViewModel::class.java]
recipeItemList = arrayListOf()
mainBinding.recyclerView.layoutManager = LinearLayoutManager(this)
mainBinding.recyclerView.hasFixedSize()
recipeAdapter = RecipeAdapter(this)
mainBinding.recyclerView.adapter = recipeAdapter
recipeViewModel.recipeLiveData.observe(this, Observer { recipeItems ->
recipeItemList.addAll(recipeItems.hits)
recipeAdapter.updateRecipes(recipeItemList)
Log.d("RESPONSE", recipeItems.toString())
Log.d("List size", recipeAdapter.itemCount.toString())
})
searchRecipeName()
}
private fun searchRecipeName() {
mainBinding.searchRecipeFabBtn.setOnClickListener {
val view = layoutInflater.inflate(R.layout.recipe_search_layout, null)
val searchRecipeET = view.findViewById<EditText>(R.id.searchRecipeET)
val searchRecipeBtn = view.findViewById<Button>(R.id.searchRecipeBtn)
val bottomSheetDialog = BottomSheetDialog(this)
bottomSheetDialog.apply {
this.setContentView(view)
this.show()
}
searchRecipeBtn.setOnClickListener {
val recipeName = searchRecipeET.text.toString()
searchRecipeName(recipeName, searchRecipeET, bottomSheetDialog)
}
}
}
private fun searchRecipeName(
recipeName: String,
searchRecipeET: EditText,
bottomSheetDialog: BottomSheetDialog
) {
if (recipeName.isEmpty()) {
searchRecipeET.error = "Please enter recipe name"
} else {
recipeViewModel.getRecipes(recipeName)
bottomSheetDialog.dismiss()
}
}
}
RecipeAdapter.kt
class RecipeAdapter(val context: Context) : RecyclerView.Adapter<RecipeAdapter.RecipeViewHolder>() {
private val recipesList: ArrayList<Hit> = arrayListOf()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecipeViewHolder {
val layoutInflater = LayoutInflater.from(context)
val view = layoutInflater.inflate(R.layout.recipe_items_layout, null, false)
return RecipeViewHolder(view)
}
override fun onBindViewHolder(holder: RecipeViewHolder, position: Int) {
val currentItem = recipesList[position]
holder.recipeImageView.load(currentItem.recipe.image)
holder.recipeNameText.text = currentItem.recipe.label
}
override fun getItemCount(): Int {
return recipesList.size
}
class RecipeViewHolder(itemView: View) :RecyclerView.ViewHolder(itemView) {
val recipeImageView: ImageView = itemView.findViewById(R.id.recipeImageView)
val recipeNameText: TextView = itemView.findViewById(R.id.recipeNameText)
}
fun updateRecipes(newRecipesList: ArrayList<Hit>){
recipesList.clear()
Log.d("RECIPE SIZE", "${recipesList.size}")
recipesList.addAll(newRecipesList)
notifyDataSetChanged()
}
}
This may be helpful.
Be careful of this :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(mainBinding.root)
recipeViewModel =
ViewModelProvider(
this,
ViewModelProvider.AndroidViewModelFactory
.getInstance(application)
)[RecipeViewModel::class.java]
recipeItemList = arrayListOf()
mainBinding.recyclerView.layoutManager = LinearLayoutManager(this)
mainBinding.recyclerView.hasFixedSize()
recipeAdapter = RecipeAdapter(this)
mainBinding.recyclerView.adapter = recipeAdapter
recipeViewModel.recipeLiveData.observe(this, Observer { recipeItems ->
// You're adding items here but never clear the list
// list will be bigger every time you'll be notified
// recipeItemList.addAll(recipeItems.hits
// recipeAdapter.updateRecipes(recipeItemList)
// Do this instead
recipeItemList = recipeItems.hits
recipeAdapter.updateRecipes(recipeItemList)
Log.d("RESPONSE", recipeItems.toString())
Log.d("List size", recipeAdapter.itemCount.toString())
})
searchRecipeName()
}
Also, here: It's a little better to do this (https://stackoverflow.com/a/10298038/4221943)
fun updateRecipes(newRecipesList: ArrayList<Hit>){
recipesList = newRecipesList
Log.d("RECIPE SIZE", "${recipesList.size}")
notifyDataSetChanged()
}
BTW it will always be more efficient to use the more specific change events if you can. Rely on notifyDataSetChanged() as a last resort. It is also good practice to use notifyItemInserted(mItems.size() - 1) for "easier" solution.
You could convert the RecyclerView.Adapter into a ListAdapter:
class RecipeAdapter(val context: Context) : ListAdapter<Hit, RecipeAdapter.RecipeViewHolder>(RecipeDiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecipeViewHolder {
val layoutInflater = LayoutInflater.from(context)
val view = layoutInflater.inflate(R.layout.recipe_items_layout, null, false)
return RecipeViewHolder(view)
}
override fun onBindViewHolder(holder: RecipeViewHolder, position: Int) {
val currentItem = getItem(position)
holder.recipeImageView.load(currentItem.recipe.image)
holder.recipeNameText.text = currentItem.recipe.label
}
class RecipeViewHolder(itemView: View) :RecyclerView.ViewHolder(itemView) {
val recipeImageView: ImageView = itemView.findViewById(R.id.recipeImageView)
val recipeNameText: TextView = itemView.findViewById(R.id.recipeNameText)
}
}
class RecipeDiffCallback : DiffUtil.ItemCallback<Hit>() {
// Change this condition based on the attribute of `Hit` that will change
override fun areItemsTheSame(oldItem: Hit, newItem: Hit): Boolean = oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: Hit, newItem: Hit): Boolean = oldItem == newItem
}
Then update its content with the submitList method.
Every item not satisfying the RecipeDiffCallback conditions will be automatically updated:
recipeViewModel.recipeLiveData.observe(this, Observer { recipeItems ->
recipeAdapter.submitList(recipeItems.hits)
})
My target is to sort in alphabetical order data from an ArrayList with encrypted data and put correct order into a RecyclerView.
I'm able to show decrypted data into RecyclerView, but in the same order witch I use to add item into the ArrayList (see code in adapter).
I tried to sort with sortby{}without success (see code in activity). The result is an partial order and onItemClick not return correct position.
My activity that extend adapter:
class HomeActivity : AppCompatActivity(),DocumentAdapter.OnItemClickListener {
companion object{
lateinit var dbHandler: DBHandler
}
private var documentslist = ArrayList<PassItem>()
private lateinit var adapter : RecyclerView.Adapter<*>
private lateinit var rv : RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
dbHandler = DBHandler(this, null, null,1)
viewDocuments()
val fab = findViewById<FloatingActionButton>(R.id.fabAdd)
fab.setOnClickListener {
val i = Intent(this, AddDocument :: class.java)
startActivity(i)
}
}
#SuppressLint("WrongConstant")
private fun viewDocuments(){
documentslist = dbHandler.getDocuments(this)
//documentslist.sortBy { it.PassItemApp.toString() } ---> this sort encrypted data then not in alphabetic order and onItemClick return uncorrect position
adapter = DocumentAdapter(this,documentslist,this)
rv = findViewById(R.id.recyclerView)
rv.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL,false)
rv.adapter = adapter
}
override fun onItemClick(position: Int) {
val intent = Intent(this#HomeActivity,DocActivity::class.java)
intent.putExtra("clickedPosition", position)
startActivity(intent)
}
override fun onResume() {
viewDocuments()
super.onResume()
}
and my adapter:
class DocumentAdapter(val mCtx: Context, private val documents: ArrayList<PassItem>, val listener: OnItemClickListener) : RecyclerView.Adapter<DocumentAdapter.ViewHolder>(){
interface OnItemClickListener{
fun onItemClick(position : Int)
}
inner class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView), View.OnClickListener{
val txtDocApp : TextView = itemView.findViewById(R.id.txtDocApp)
init {
itemView.setOnClickListener(this)
}
override fun onClick(v: View?) {
val position : Int = adapterPosition
if (position != RecyclerView.NO_POSITION){
listener.onItemClick(position)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DocumentAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.item_pass_layout,parent,false)
return ViewHolder(v)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val document : PassItem = documents[position]
//Decryption of item showed in Recyclerview
val initialisationVector = document.PassItemIvApp
val bytesIV = Base64.decode(initialisationVector, Base64.NO_WRAP)
val oisIV = ObjectInputStream(ByteArrayInputStream(bytesIV))
val initializationVectorIV = oisIV.readObject() as ByteArray
val encryptedText = document.PassItemApp
val bytesText = Base64.decode(encryptedText, Base64.NO_WRAP)
val oisText = ObjectInputStream(ByteArrayInputStream(bytesText))
val textByteArray = oisText.readObject() as ByteArray
val key = document.PassItemKeyApp
val keyText = Base64.decode(key, Base64.NO_WRAP)
val oisKey = ObjectInputStream(ByteArrayInputStream(keyText))
val keyToDecrypt = oisKey.readObject() as SecretKey
val aesDecrypt = Decrypted(keyToDecrypt,initializationVectorIV)
val decryptedByteArray = aesDecrypt.decrypt(textByteArray)
val textAfterDecryption = decryptedByteArray.toString(charset("UTF_8"))
//Data in Recyclerview
holder.txtDocApp.text = textAfterDecryption
}
override fun getItemCount(): Int {
return documents.size
}
}
I have a RecyclerView and am trying to pass the location from my adapter back to the mainactivity without success so far .
I think the code in the adapter is passing the data back is working, but unfortunately I can’t work out how to set the initialisation in the onCreate in the MainActivity to work? I’m not certain how to call this using “lazy” (ie “private val itemAdapter by lazy” in the mainactivity”)?
I am getting frustrated solving this so any help appreciated.
MainActivity.kt
class MainActivity : AppCompatActivity(), ItemAdapter.OnItemClickListener {
private val itemAdapter by lazy {
ItemAdapter { position: Int, item: Item ->
Toast.makeText(this#MainActivity, "Pos ${position}", Toast.LENGTH_LONG).show()
item_list.smoothScrollToPosition(position)
} }
private val possibleItems = listOf(
Item("Plane 1", R.drawable.ic_airplane),
Item("Car 2", R.drawable.ic_car),
Item("Meals 3", R.drawable.ic_food),
Item("Fuel 4", R.drawable.ic_gas),
Item("Home 5 ", R.drawable.ic_home)
)
fun onImageClick(imageData: String?) {
// handle image data
}
override fun onCreate(savedInstanceState: Bundle?) {
var position: Int
val thisitem: Item
var thisunit: Unit
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// var itemAdapter = ItemAdapter(possibleItems)
// var thisitemAdapter = ItemAdapter((position, thisitem) -> thisunit)
// Toast.makeText(this#MainActivity, "Pos ${position}", Toast.LENGTH_LONG).show()
// item_list.smoothScrollToPosition(position) //CAN'T GET THIS TO COMPILE
item_list.initialize(itemAdapter)
itemAdapter.setOnItemClickListener(this) // Added this
ItemSnapHelper().attachToRecyclerView(item_list)
item_list.setViewsToChangeColor(listOf(R.id.list_item_background, R.id.list_item_text))
item_list.scrollToPosition(Int.MAX_VALUE/2)
itemAdapter.setItems(possibleItems)
}
override
public fun getAdapterPosition(position : Int ){
Toast.makeText(this,"this is toast message" + position,Toast.LENGTH_SHORT).show()
}
private fun getLargeListOfItems(): List<Item> {
val items = mutableListOf<Item>()
(0..40).map { items.add(possibleItems.random()) }
return items
}
}
data class Item(
val title: String,
#DrawableRes val icon: Int
)
ItemAdapter.kt
class ItemAdapter(val itemClick: (position:Int,item:Item) -> Unit) : RecyclerView.Adapter<ItemViewHolder>() {
private var items: List<Item> = listOf()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder =
ItemViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false))
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
val pos = position % items.size
holder.bind(items[pos]) // bind the actual item
Log.d("xx position= ", pos.toString())
}
override fun getItemCount(): Int = Int.MAX_VALUE
fun setItems(newItems: List<Item>) {
items = newItems
notifyDataSetChanged()
}
lateinit var listener: OnItemClickListener
public interface OnItemClickListener {
fun getAdapterPosition(position : Int )
}
public fun setOnItemClickListener(listener: OnItemClickListener) {
this.listener= listener
}
}
class ItemViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
fun bind(item: Item) {
view.list_item_text.text = "${item.title}"
view.list_item_icon.setImageResource(item.icon)
}
}
you have a listener so you can write like this
class MainActivity : AppCompatActivity(), ItemAdapter.OnItemClickListener {
private lateinit var itemAdapter: ItemAdapter
private val possibleItems = listOf(
Item("Plane 1", R.drawable.ic_airplane),
Item("Car 2", R.drawable.ic_car),
Item("Meals 3", R.drawable.ic_food),
Item("Fuel 4", R.drawable.ic_gas),
Item("Home 5 ", R.drawable.ic_home)
)
fun onImageClick(imageData: String?) {
// handle image data
}
override fun onCreate(savedInstanceState: Bundle?) {
var position: Int
val thisitem: Item
var thisunit: Unit
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
itemAdapter = ItemAdapter()
itemAdapter.listener = this
item_list.initialize(itemAdapter)
ItemSnapHelper().attachToRecyclerView(item_list)
item_list.setViewsToChangeColor(listOf(R.id.list_item_background, R.id.list_item_text))
item_list.scrollToPosition(Int.MAX_VALUE/2)
itemAdapter.setItems(possibleItems)
}
override
public fun getAdapterPosition(position : Int ){
Toast.makeText(this,"this is toast message" + position,Toast.LENGTH_SHORT).show()
}
private fun getLargeListOfItems(): List<Item> {
val items = mutableListOf<Item>()
(0..40).map { items.add(possibleItems.random()) }
return items
}
}
ItemAdapter.kt
class ItemAdapter : RecyclerView.Adapter<ItemViewHolder>() {
private var items: List<ClipData.Item> = listOf()
var listener: OnItemClickListener? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder = ItemViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false))
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
holder.bind(items[position], position)
}
override fun getItemCount(): Int = Int.MAX_VALUE
fun setItems(newItems: List<ClipData.Item>) {
items = newItems
notifyDataSetChanged()
}
}
class ItemViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
fun bind(item: ClipData.Item, position: Int) {
view.list_item_text.text = "${item.title}"
view.list_item_icon.setImageResource(item.icon)
view.list_item_text.setOnClickListener {
listener?.getAdapterPosition(position)
}
}
}
interface OnItemClickListener {
fun getAdapterPosition(position: Int)
}
Instead of creating an interface you can also pass a lambda function as a callback to the Adapter.
Basic structure will be like this:
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
...
val itemAdapter = ItemAdapter { position ->
// do whatever you wish with this position
}
recyclerView.adapter = itemAdapter
...
}
}
class ItemAdapter(private val onClick: (Int) -> Unit) {
...
inner class ItemViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
fun bind(item: ClipData.Item, position: Int) {
...
view.setOnClickListener {
onClick(position)
}
}
}
}
If you don't want to create ItemViewHolder as an inner class, you can accept the onClick lambda in its constructor and pass it there from onCreateViewHolder.