Trouble with updating my Recycler View with new items - android

So my main problem at the moment is trying to add data to my recycler view. Essentially I created a button that adds the string "hello" to a list and then tries to call mAdapter.notifyItemInserted(dataSet.size). It adds perfectly to the end of the list in the logs, but I just can't figure out how to update the UI for it.
The only items I can manage to load at the moment are the items fakeItems that are created when I start the app.
class MainActivity : AppCompatActivity() {
var dataSet = mutableListOf<String>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
insertFakeItems()
setUpRecycler()
}
private fun insertFakeItems() {
for (i in 0..5) {
dataSet.add("Item #$i")
}
}
private fun setUpRecycler() {
val mAdapter = MyAdapter(dataSet)
val mList = list
mList.layoutManager = LinearLayoutManager(this)
mList.adapter = mAdapter
mList.orientation =
DragDropSwipeRecyclerView.ListOrientation.VERTICAL_LIST_WITH_VERTICAL_DRAGGING
//mList.disableSwipeDirection(DragDropSwipeRecyclerView.ListOrientation.DirectionFlag.RIGHT)
// button
val onItemSwipeListener = object : OnItemSwipeListener<String> {
override fun onItemSwiped(
position: Int,
direction: OnItemSwipeListener.SwipeDirection,
item: String
): Boolean {
Log.d("Main", "Position = $position, Direction = $direction, Item = $item")
when (direction) {
OnItemSwipeListener.SwipeDirection.RIGHT_TO_LEFT -> {
Toast.makeText(applicationContext, "Item ${position+1} deleted", Toast.LENGTH_SHORT).show()
//todo: add deleted code here
}
OnItemSwipeListener.SwipeDirection.LEFT_TO_RIGHT -> {
Toast.makeText(applicationContext, "Item ${position+1} archived", Toast.LENGTH_SHORT).show()
//todo: add archived code here
}
}
return false
}
}
mList.swipeListener = onItemSwipeListener
fab_add.setOnClickListener {
Log.d("Main", "Button pressed")
dataSet.add(dataSet.size,"hello")
mAdapter.notifyItemInserted(dataSet.size)
println(dataSet)
}
}
}
And this is the adapter:
class MyAdapter(dataSet: MutableList<String>)
: DragDropSwipeAdapter<String, MyAdapter.ViewHolder>(dataSet) {
class ViewHolder(itemView: View) : DragDropSwipeAdapter.ViewHolder(itemView) {
val itemText: TextView = itemView.findViewById(R.id.item_text)
val dragIcon: ImageView = itemView.findViewById(R.id.drag_icon)
}
override fun getViewHolder(itemLayout: View) = MyAdapter.ViewHolder(itemLayout)
override fun onBindViewHolder(item: String, viewHolder: MyAdapter.ViewHolder, position: Int) {
viewHolder.itemText.text = item
}
override fun getViewToTouchToStartDraggingItem(item: String, viewHolder: MyAdapter.ViewHolder, position: Int): View? {
return viewHolder.dragIcon
}
override fun onDragFinished(item: String, viewHolder: ViewHolder) {
super.onDragFinished(item, viewHolder)
println("$dataSet")
}
}

The problem is that in Java or Kotlin you pass by value not by reference. So until you change the value of the dataSet you are using in RecyclerView, you won't get any effect.
Do something like this (:
class MyAdapter(dataSet: MutableList<String>)
: DragDropSwipeAdapter<String, MyAdapter.ViewHolder>(dataSet) {
private var dataSet: MutableList<String>
init {
this.dataSet = dataSet
}
class ViewHolder(itemView: View) : DragDropSwipeAdapter.ViewHolder(itemView) {
val itemText: TextView = itemView.findViewById(R.id.item_text)
val dragIcon: ImageView = itemView.findViewById(R.id.drag_icon)
}
override fun getViewHolder(itemLayout: View) = MyAdapter.ViewHolder(itemLayout)
override fun onBindViewHolder(item: String, viewHolder: MyAdapter.ViewHolder, position: Int) {
viewHolder.itemText.text = item
}
override fun getViewToTouchToStartDraggingItem(item: String, viewHolder: MyAdapter.ViewHolder, position: Int): View? {
return viewHolder.dragIcon
}
override fun onDragFinished(item: String, viewHolder: ViewHolder) {
super.onDragFinished(item, viewHolder)
println("$dataSet")
}
fun updateItem(item: String) {
this.dataSet.add(item)
notifyItemInserted(dataSet.size - 1)
}
}
In activity:
fab_add.setOnClickListener {
Log.d("Main", "Button pressed")
mAdapter.updateItem("hello")
println(dataSet)
}

You must call notifyDatasetChanged after changing recycleView items list.

Related

Carousel & Snap in Horizontal RecyclerView: returning the result in Kotlin?

I have been working on a simple app that has a Horizontal RecyclerView using a carousel that snaps when the rotations ends. After the rotation ends I need to return the final location from the adapter to the MainActivity.
Unfortunately I am struggling with this and I just can’t seem to get the location to be returned back to the MainActivity? At the moment I don’t receive anything back from the adapter ?
Thanks you for any help.
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)
)
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 :-( this
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)
}
}

Returning the location from the adapter back to the mainactivity in Kotlin?

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.

Onclick in recyclerView wrong positions

I have a recyclerView with 10 names and in this activity is possible search for name. With a click in a name the activity moves to another(with help Intents to move for DetailsActivity). It possible make this because in adapter, i have a function that give the position that the user have clicked. But when put a word in menu search this functional gives the position 1 (the real position of name, in exactally moment).
I can send in function in adpter for example, the id of name? What is the correct way to make this?
MainActivity.kt
class MainActivity : AppCompatActivity(), PeopleInStarWarsAdapter.OnNoteListener {
private lateinit var mainViewModel: MainViewModel
private var listDataPeople = ArrayList<DataPeople>()
private lateinit var dataPeopleAdapter: PeopleInStarWarsAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mainViewModel = ViewModelProvider
.AndroidViewModelFactory
.getInstance(application)
.create(MainViewModel::class.java)
//iniciar ViewModel
mainViewModel.init(this)
mainViewModel.getList().observe(this, Observer{ it ->
if(it != null){
listDataPeople = it
dataPeopleAdapter.submitList(listDataPeople)
}
else{
Toast.makeText(this, "Something is wrong!", Toast.LENGTH_SHORT).show()
}
})
initRecyclerView(listDataPeople)
}
private fun initRecyclerView(listDataPeople : ArrayList<DataPeople>) {
recycler_view.layoutManager = LinearLayoutManager(this)
dataPeopleAdapter = PeopleInStarWarsAdapter(listDataPeople, this)
recycler_view.adapter = dataPeopleAdapter
}
override fun onNoteClick(position: Int){
val intent = Intent(this, DetailsActivity::class.java)
intent.putExtra(BuildConfig.POSITION, position)
startActivity(intent)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main_menu, menu)
val manager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
val searchItem = menu?.findItem(R.id.search)
val searchView = searchItem?.actionView as SearchView
searchView.setSearchableInfo(manager.getSearchableInfo(componentName))
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String): Boolean {
searchView.clearFocus()
searchView.setQuery("", false)
searchItem.collapseActionView()
Log.v("Information", "Looking for $query")
mainViewModel.checkMatch(query, this#MainActivity)
return true
}
override fun onQueryTextChange(newText: String): Boolean {
Log.v("Information", "$newText")
return false
}
})
return true
}
}
PeopleInStarWarsAdapter.kt
class PeopleInStarWarsAdapter(listDataPeople: ArrayList<DataPeople>, onNoteListener: OnNoteListener) : RecyclerView.Adapter<RecyclerView.ViewHolder>(){
var listDataPeople = ArrayList<DataPeople>()
private var mOnNoteListener : OnNoteListener
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return ViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.layout_list_item, parent,
false)
)
}
override fun getItemCount(): Int {
return listDataPeople.size
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is ViewHolder -> {
holder.ViewHolder(listDataPeople[position], mOnNoteListener)
}
}
}
fun submitList(dataPeopleList: ArrayList<DataPeople>) {
this.listDataPeople = dataPeopleList
notifyDataSetChanged()
}
class ViewHolder constructor(itemView: View): RecyclerView.ViewHolder(itemView), View.OnClickListener {
private val textViewName : TextView = itemView.textView_name
var onNoteListener: OnNoteListener? = null
fun ViewHolder(dataPeople: DataPeople, onNoteListener: OnNoteListener) {
val name = dataPeople.name
this.textViewName.text = name
this.onNoteListener = onNoteListener
itemView.setOnClickListener(this)
}
override fun onClick(v: View?) {
onNoteListener?.onNoteClick(adapterPosition)
}
}
interface OnNoteListener {
fun onNoteClick(position: Int)
}
init {
this.listDataPeople = listDataPeople
mOnNoteListener = onNoteListener
}
}
You should send position to ViewHolder .
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is ViewHolder -> {
holder.ViewHolder(listDataPeople[position], mOnNoteListener,position)
}
}
}
Then pass this value into onNoteClick()
fun ViewHolder(dataPeople: DataPeople, onNoteListener: OnNoteListener,position: Int) {
val name = dataPeople.name
this.textViewName.text = name
this.onNoteListener = onNoteListener
itemView.setOnClickListener(this)
}
override fun onClick(v: View?) {
onNoteListener?.onNoteClick(position)
}

How add data from firebase to an array for my recyclerView

I think the main problem happened during adding foodInfo into foodLists, and also the part where I pass data to listAdapter. I did check with Log.d for foodData.foodName and it works fine, but when I try Log.d to get the foodList it doesn't work.
here are my code for the moment
EDITED SOLVED
My Adapter
class listAdapter(val food : ArrayList<Foods>) : RecyclerView.Adapter<listAdapter.ViewHolder>() {
val unfoldedIndexes = HashSet<Int>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.cell, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return food.count()
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(position)
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(position: Int) = with(itemView) {
// folding_cell.setBackgroundColor(resources.getColor(R.color.colorPrimary))
folding_cell.backSideColor = resources.getColor(R.color.White)
if (unfoldedIndexes.contains(position)) {
cell_title_view.visibility = View.GONE
cell_content_view.visibility = View.VISIBLE
} else {
cell_content_view.visibility = View.GONE
cell_title_view.visibility = View.VISIBLE
}
itemView.setOnClickListener {
// toggle clicked cell state
folding_cell.toggle(false)
// register in adapter that state for selected cell is toggled
registerToggle(position)
}
}
private fun registerToggle(position: Int) {
if (unfoldedIndexes.contains(position))
registerFold(position)
else
registerUnfold(position)
}
private fun registerFold(position: Int) {
unfoldedIndexes.remove(position)
}
private fun registerUnfold(position: Int) {
unfoldedIndexes.add(position)
}
}
}
My Activity for the recyclerView
class FoodListActivity : BaseActivity(1) {
private val TAG = "FoodListActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_food_list)
setupBottomNavigation()
Log.d(TAG,"onCreate")
getFromFirebase()
}
private fun getFromFirebase() {
val currentUserUID = FirebaseAuth.getInstance().uid.toString()
val ref = FirebaseDatabase.getInstance().reference
ref.child("users").child(currentUserUID).child("foods").addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
setHasFixedSize(true)
recyclerview.layoutManager = LinearLayoutManager(this#FoodListActivity)
p0.children.forEach {
Log.d("getFood", it.toString())
val foodData = it.getValue(Foods::class.java)
if (foodData != null) {
val foodList = ArrayList<Foods>()
val adapter = ListAdapter(foodList)
foodList.add(foodData)
recyclerview.adapter = adapter
}
}
}
})
}}

RecyclerView itemClickListener in Kotlin

I'm writing my first app in Kotlin after 3 years of experience with Android.
Just confused as to how to utilize itemClickListener with a RecyclerView in Kotlin.
I have tried the trait (edit: now interface) approach, very Java-like
public class MainActivity : ActionBarActivity() {
protected override fun onCreate(savedInstanceState: Bundle?) {
// set content view etc go above this line
class itemClickListener : ItemClickListener {
override fun onItemClick(view: View, position: Int) {
Toast.makeText(this#MainActivity, "TEST: " + position, Toast.LENGTH_SHORT).show()
}
}
val adapter = DrawerAdapter(itemClickListener())
mRecyclerView.setAdapter(adapter)
}
trait ItemClickListener {
fun onItemClick(view: View, position: Int)
}
}
That seemed very redundant so I tried the inner class approach:
inner class ItemClickListener {
fun onItemClick(view: View, position: Int) {
startActivityFromFragmentForResult<SelectExerciseActivity>(SELECT_EXERCISES)
}
}
And then just setting the adapter's click listener like this:
val adapter = WorkoutsAdapter(ItemClickListener())
But I'm still not satisfied with this because I think there might be a better, cleaner way. I'm trying to essentially achieve something like this:
RecyclerView onClick
Any suggestions?
Ended up going with a variation of the approved answer
Defined the function in the activity:
val itemOnClick: (View, Int, Int) -> Unit = { view, position, type ->
Log.d(TAG, "test")
}
Passed the function itself on to the adapter like this:
class ExercisesAdapter(val itemClickListener: (View, Int, Int) -> Unit) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
// other stuff up here
val vhExercise = ExerciseVH(view) // view holder
// on to the view holder through the extension function
vhExercise.onClick(itemClickListener)
}
}
Extension function by Loop in the approved answer below.
fun <T : RecyclerView.ViewHolder> T.onClick(event: (view: View, position: Int, type: Int) -> Unit): T {
itemView.setOnClickListener {
event.invoke(it, getAdapterPosition(), getItemViewType())
}
return this
}
My solution is like a combination of the previous ones with a super clean call from the activity.
ContactAdapter:
class ContactAdapter #Inject constructor() : RecyclerView.Adapter<ContactAdapter.ViewHolder>() {
var onItemClick: ((Contact) -> Unit)? = null
var contacts: List<Contact> = emptyList()
...
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val contact = contacts[position]
holder.email.text = contact.email
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val email: TextView = itemView.email
init {
itemView.setOnClickListener {
onItemClick?.invoke(contacts[adapterPosition])
}
}
}
}
ContactActivity:
override fun setupRecyclerAdapter() {
recyclerView.adapter = contactAdapter
recyclerView.layoutManager = LinearLayoutManager(this)
contactAdapter.onItemClick = { contact ->
// do something with your item
Log.d("TAG", contact.email)
}
}
I have a little bit different approach. You can create an extension for your ViewHolder
fun <T : RecyclerView.ViewHolder> T.listen(event: (position: Int, type: Int) -> Unit): T {
itemView.setOnClickListener {
event.invoke(getAdapterPosition(), getItemViewType())
}
return this
}
Then use it in adapter like this
class MyAdapter : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
val items: MutableList<String> = arrayListOf()
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyViewHolder? {
val inflater = LayoutInflater.from(parent!!.getContext())
val view = inflater.inflate(R.layout.item_view, parent, false)
return MyViewHolder(view).listen { pos, type ->
val item = items.get(pos)
//TODO do other stuff here
}
}
override fun onBindViewHolder(holder: MyViewHolder?, position: Int) {
}
override fun getItemCount(): Int {
return items.size()
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
}
}
I am working with my colleagues on library providing such extensions.
In case anyone is looking for a more no-frills answer, I tried the following - which is very similar to the solution from AfzalivE:
In my Adapter class I passed the clickListener as a parameter. On onBindViewHolder, I've used setOnClickListener to call clickListener and handle click event.
MyAdapter.kt:
class MyAdapter constructor(objects: ArrayList<MyObject>, val clickListener: (MyObject) -> Unit) : RecyclerView.Adapter<MyAdapter.Holder>() {
private var mObjects : ArrayList<MyObject> = ArrayList<MyObject>()
init {
mObjects = objects
}
override fun onBindViewHolder(holder: Holder?, position: Int) {
var item : MyObject = objects[position]
// Calling the clickListener sent by the constructor
holder?.containerView?.setOnClickListener { clickListener(item) }
}
// More code (ViewHolder definitions and stuff)...
}
Note: I needed a reference from my list item's container (the root view), which in this case is containerView
Then I passed my object as parameter without need for searching it on a list again and handle it directly on my Activity class, in the moment I set the adapter:
MyActivity.kt:
myRecyclerView?.adapter = MyAdapter(mObjects) {
Log.e("Activity", "Clicked on item ${it.itemName}")
}
Update
If you need to get the position of the clicked item, just define it as parameter on the callback and then send it back later. Notice the val clickListener: (MyObject, Int) -> Unit below:
MyAdapter.kt
class MyAdapter constructor(objects: ArrayList<MyObject>, val clickListener: (MyObject, Int) -> Unit) : RecyclerView.Adapter<MyAdapter.Holder>() {
// Rest of the code...
Then on onBindViewHolder() you pass the position when calling the callback method:
override fun onBindViewHolder(holder: Holder?, position: Int) {
var item : MyObject = objects[position]
// Calling the clickListener sent by the constructor
holder?.containerView?.setOnClickListener { clickListener(item, position) }
}
And on MyActivity.kt, you'll have to change the way you set the adapter so you can get the position. Like this:
myRecyclerView?.adapter = MyAdapter(mObjects) { itemDto: MyObject, position: Int ->
Log.e("MyActivity", "Clicked on item ${itemDto.someItemPropertyLikeName} at position $position")
}
Sorry for the delay, Got an awesome answer from this link and it was in Java..
Did some Homework and converted it to Kotlin..
Now it is working Properly.. Here is the Code,
Create a class named RecyclerItemClickListenr,
class RecyclerItemClickListenr(context: Context, recyclerView: RecyclerView, private val mListener: OnItemClickListener?) : RecyclerView.OnItemTouchListener {
private val mGestureDetector: GestureDetector
interface OnItemClickListener {
fun onItemClick(view: View, position: Int)
fun onItemLongClick(view: View?, position: Int)
}
init {
mGestureDetector = GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
override fun onSingleTapUp(e: MotionEvent): Boolean {
return true
}
override fun onLongPress(e: MotionEvent) {
val childView = recyclerView.findChildViewUnder(e.x, e.y)
if (childView != null && mListener != null) {
mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView))
}
}
})
}
override fun onInterceptTouchEvent(view: RecyclerView, e: MotionEvent): Boolean {
val childView = view.findChildViewUnder(e.x, e.y)
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView))
}
return false
}
override fun onTouchEvent(view: RecyclerView, motionEvent: MotionEvent) {}
override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}}
and access it from Activity/Fragment as
recyclerView.addOnItemTouchListener(RecyclerItemClickListenr(this, recyclerView, object : RecyclerItemClickListenr.OnItemClickListener {
override fun onItemClick(view: View, position: Int) {
//do your work here..
}
override fun onItemLongClick(view: View?, position: Int) {
TODO("do nothing")
}
}))
Add ClickListener code on onBindViewHolder fun
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.vieww.textView.setText(arr.get(position))
holder.vieww.setOnClickListener {(holder.vieww.textView.setTextColor(Color.GREEN))} // click event
}
You can easily achieve this by using an interface
class ExercisesAdapter(private val itemClickListener: ItemClickListener) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
interface ItemClickListener {
fun onItemClick(position: Int)
fun onLongClick(position: Int)
}
inner class MyViewHolder(view:View): RecyclerView.ViewHolder(view){
init {
view.setOnClickListener {
if (bindingAdapterPosition >= 0) {
itemClickListener.onItemClick(bindingAdapterPosition)
}
}
view.setOnLongClickListener{
if (bindingAdapterPosition >= 0) {
itemClickListener.onLongClick(bindingAdapterPosition)
}
return#setOnLongClickListener true
}
}
}
}
From your MainActivity
public class MainActivity : AppCompatActivity(), ExercisesAdapter.ItemClickListener {
protected override fun onCreate(savedInstanceState: Bundle?) {
// set content view etc go above this line
mAdapter = ExercisesAdapter(this)
}
override fun onItemClick(position: Int) {
Toast.makeText(this#MainActivity, "TEST: " + position, Toast.LENGTH_SHORT).show()
}
override fun onLongClick(position: Int) {
//do long click here
}
}
Slightly different, based on denwehrle
To use on a fragment, inside OnCreateView
adapter.onItemClick = {it ->
//do something
}
Add in the adapter class:
var onItemClick: ((Contact)->Unit) ?= null
...
inner class contactViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val myItemView: TextView = itemView.findViewById(R.id.textView)
init{
itemView.setOnClickListener {
onItemClick?.invoke(contact[adapterPosition])
}
}
}
You don't need to write extension function to ViewHolder or something like this.
Best practice; use Higher-Order Function
MainRecyclerAdapter
class MainRecyclerAdapter(val news: JSONArray, private val itemClickListener: (Int) -> Unit) : RecyclerView.Adapter<MainRecyclerAdapter.ViewHolder>() {}
Just add a Higher-order func. like itemClickListener and then go to the ViewHolder class. Write this function to your bind function as parameter and set this to itemView Like that :
MainRecyclerAdapter.ViewHolder
class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
fun bind(newsItem: JSONObject,itemClickListener:(Int)->Unit) {
//Some Stuff here..
itemView.setOnClickListener { itemClickListener(adapterPosition) }
}
}
Use this method onBindViewHolder
OnBindViewHolder
override fun onBindViewHolder(holder: MainRecyclerAdapter.ViewHolder, position: Int) {
holder.bind(news.getJSONObject(position),itemClickListener)
}
And now you can write your onClick function in any activity or fragments.. Just give as parameter.
Activity or Fragment
val itemOnClick: (Int) -> Unit = { position ->
newsRecyclerView.adapter!!.notifyDataSetChanged()
Toast.makeText(this.context,"$position. item clicked.",Toast.LENGTH_SHORT).show()
}
newsRecyclerView.adapter = MainRecyclerAdapter(news,itemClickListener = itemOnClick)
Updated in 05 - 2019
I think the most elegant solution is to give this responsibility to recyclerView and not to view or even adapt it.
for that we need:
1: Create RecyclerItemClickListener file
class RecyclerItemClickListener(
private val mRecycler: RecyclerView,
private val clickListener: ((position: Int, view: View) -> Unit)? = null,
private val longClickListener: ((position: Int, view: View) -> Unit)? = null
) : RecyclerView.OnChildAttachStateChangeListener {
override fun onChildViewDetachedFromWindow(view: View) {
view.setOnClickListener(null)
view.setOnLongClickListener(null)
}
override fun onChildViewAttachedToWindow(view: View) {
view.setOnClickListener { v -> setOnChildAttachedToWindow(v) }
}
private fun setOnChildAttachedToWindow(v: View?) {
if (v != null) {
val position = mRecycler.getChildLayoutPosition(v)
if (position >= 0) {
clickListener?.invoke(position, v)
longClickListener?.invoke(position, v)
}
}
}
}
2: Create/Add extensions for RecyclerView:
import android.support.v7.widget.RecyclerView
import com.app.manager.internal.common.RecyclerItemClickListener
#JvmOverloads
fun RecyclerView.affectOnItemClicks(onClick: ((position: Int, view: View) -> Unit)? = null, onLongClick: ((position: Int, view: View) -> Unit)? = null) {
this.addOnChildAttachStateChangeListener(RecyclerItemClickListener(this, onClick, onLongClick))
}
3: And finally the use (i suppose you use kotlinx)
import kotlinx.android.synthetic.main.{your_layout_name}.*
class FragmentName : Fragment() {
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
recycler.affectOnItemClick { position, view -> /*todo*/ }
}
}
3 easy steps:
1. pass in argument of your adapter as follow:
class ListAdapter(private val mListener: (ListItemDataClass) -> Unit)
2. in onBindViewHolder function, use like this
override fun onBindViewHolder(holder: YourViewHolder, position: Int) {
val item = getItem(position)
holder.itemView.setOnClickListener {
item?.let { it1 -> mListener.invoke(it1) }
}
}
3. and in your activity, use like this
val adapter = ListAdapter {
Toast.makeText(this, it.title, Toast.LENGTH_SHORT).show()
}
Here is a simple approach without using an interface, just in your adapter create an init block within viewholder class. Like below
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
init {
itemView.setOnClickListener{
//your code here---
}
}
}
Adapter constructor declaration
class YourAdapter(private val mListener: (ItemObject) -> Unit) : RecyclerView.Adapter<ViewHolder>()
Adapter::onBindViewHolder
holder.itemView.setOnClickListener {
mListener.invoke(item) // <- item instance of ItemObject
}
How to Use
mTypesWasteAdapter = YourAdapter({ it.something()})
Basically, you will receive the ItemObject as it in the lambda argument.
Finally, here is a nice working solution:
MyRecyclerAdapter.kt
class MyRecyclerAdapter(val context: Context, val items : ArrayList<Item>, val clickListener: (Int) -> Unit) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, p1: Int): RecyclerView.ViewHolder {
return MyViewHolder(LayoutInflater.from(context).inflate(R.layout.my_item, parent, false))
}
override fun getItemCount(): Int {
return items.size
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
(holder as MyViewHolder).clickableView.setOnClickListener {
clickListener(position)
}
}
}
class MyViewHolder (view: View) : RecyclerView.ViewHolder(view) {
val clickableView = view.clickable_view
}
MainActivity.kt
fun appClickListener(position: Int) {
// You got the position of ArrayList
}
my_recyclerview.adapter = MyRecyclerAdapter(this, myList, clickListener = {
appClickListener(it)
})
My simple solution using higher-order function and let scoping function to set listener only if itemAction has been set
// Adapter
private var itemAction: ((Item) -> Unit)? = null
fun setItemAction(action: (Item) -> Unit) {
this.itemAction = action
}
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun setItem(item: Item) {
// ...
itemAction?.let {
itemView.setOnClickListener { it(item) }
}
}
}
and in activity/fragment
adapter.setItemAction { // <- it is Item
// do something with it
}
You could try something like:
public class MainActivity : ActionBarActivity() {
protected override fun onCreate(savedInstanceState: Bundle?) {
[...]
val adapter = DrawAdapter(::onItemClick)
[...]
}
}
fun onItemClick(view: View, position: Int) {
//Do work
}
and SAM convertion just works like in Java 8, so just use a lambda:
public class MainActivity : ActionBarActivity() {
protected override fun onCreate(savedInstanceState: Bundle?) {
[...]
val adapter = DrawAdapter({view, position -> /*Do work*/ })
[...]
}
}
In RecyclerView you can put click on inflated view inside ViewHolder class and call it from onBindViewHolder callback method for example:
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val view = view
val tv_message = view.tv_message
val tv_address = view.tv_address
fun bind(listViewItem: ListViewItem) {
view.setOnClickListener(View.OnClickListener {
Toast.makeText(
view.context,
"Name: " + listViewItem.name + "/n Address: " + listViewItem.address,
Toast.LENGTH_LONG).show()
})
}
}
}
You can call from adapter onBindViewHolder() method:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val listViewItem: ListViewItem = mListViewItems[position]
holder.tv_message.text = listViewItem.name
holder.tv_address.text = listViewItem.address
holder.bind(mListViewItems[position]);
}
//Step 1 make an interface like
interface RecyclerViewClickListener {
fun onItemClick(position: String)
fun onLongClick(position: Int)
}
Step 2 Inside Adapter class pass one more argument as an interface like
class ModelAdapter(var item_list: ArrayList<UploadDocument>,var mItemClickListener:RecyclerViewClickListener) : RecyclerView.Adapter<ModelAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ModelAdapter.ViewHolder {
// create a new view
val view = LayoutInflater.from(parent.context).inflate(R.layout.upload_document_row_item, null)
// create ViewHolder
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ModelAdapter.ViewHolder, position: Int) {
holder.txtRegistrationDoc?.setText(item_list[position].getdocName())
holder.txtCertificate?.setText(item_list[position].getcertificateName())
holder.txtFileSize?.setText(item_list[position].getfileSize())
holder.txtCreatedOn?.setText(item_list[position].getcreatedOn())
holder.txtModifiedOn?.setText(item_list[position].getModifiedDate())
//holder.chkSelected.isChecked = item_list[position].isSelected()
holder.chkSelected.tag = item_list[position].getdocName()
holder. chkSelected!!.setOnCheckedChangeListener { buttonView, isChecked ->
if(isChecked)
{
System.out.println("position>>>"+buttonView.tag.toString())
mItemClickListener.onItemClick(buttonView.tag.toString())
}
}
//(context as UploadDocumentActivity::class.java).onClickCalled("your argument here")
/* holder.btn_delete.setOnClickListener(object : View.OnClickListener() {
override fun onClick(v: View) {
deleteItemFromList(v, position)
}
})*/
}
override fun getItemCount(): Int {
return item_list.size
}
/*// confirmation dialog box to delete an unit
private fun deleteItemFromList(v: View, position: Int) {
val builder = AlertDialog.Builder(v.getContext())
//builder.setTitle("Dlete ");
builder.setMessage("Delete Item ?")
.setCancelable(false)
.setPositiveButton("CONFIRM",
DialogInterface.OnClickListener { dialog, id ->
item_list.remove(position)
notifyDataSetChanged()
})
.setNegativeButton("CANCEL", DialogInterface.OnClickListener { dialog, id -> })
builder.show()
}*/
class ViewHolder(
itemLayoutView: View) : RecyclerView.ViewHolder(itemLayoutView) {
var item_name: TextView
var txtRegistrationDoc: TextViewNormal?=null
var txtCertificate: TextViewNormal?=null
var txtFileSize: TextViewNormal?=null
var txtCreatedOn: TextViewNormal?=null
var txtModifiedOn: TextViewNormal?=null
var chkSelected: CheckBox
init {
item_name = itemLayoutView.findViewById(R.id.txt_Name)
txtRegistrationDoc = itemLayoutView.findViewById(R.id.txtRegistrationDoc)
txtCertificate = itemLayoutView.findViewById(R.id.txtCertificate)
txtFileSize = itemLayoutView.findViewById(R.id.txtFileSize)
txtCreatedOn = itemLayoutView.findViewById(R.id.txtCreatedOn)
txtModifiedOn = itemLayoutView.findViewById(R.id.txtModifiedOn)
//btn_delete = itemLayoutView.findViewById(R.id.btn_delete_unit)
chkSelected = itemLayoutView.findViewById(R.id.chk_selected)
}
}
}
//Step 3 Inside your activity/ Frgament
recyclerView?.adapter = ModelAdapter(documentList,object : `in`.mobilepedia.com.gicgwaliarincubationcentre.RecyclerViewClickListener
{
override fun onItemClick(position: String) {
System.out.println("Position>>>>>"+position)
}
override fun onLongClick(position: Int) {
}
})
Kotlin
Make your adapter constructor like this
class ViewAdapter(
private val context: Context,
private val mListener: (DataClass) -> Unit
) :
RecyclerView.Adapter<WeekRecyclerViewAdapter.ViewHolder>() {
// Your adapter code goes here
}
In your onBindViewHolder,
holder.binding.parentLayout.setOnClickListener {
mListener.invoke(items[position]) // <- item instance of ItemObject
}
In your Fragment, implement like below
class YourFragment : Fragment(), (DataClass) -> Unit {
override fun invoke(p1: DataClass) {
//You will get the selected item here
}
Here is my MainActivity.kt class that uses recyclerview to populate location data. It has a simple on item click listener interface that you can implement.
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var viewAdapter: RecyclerView.Adapter<*>
private lateinit var viewManager: RecyclerView.LayoutManager
private var locationArrayList = arrayListOf<Location>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//create locations
var ny = Location("New York")
var la = Location("Los Angeles")
locationArrayList.addAll(listOf(ny, la))
viewManager = LinearLayoutManager(this)
viewAdapter = LocationsAdapter(locationArrayList)
recyclerView = findViewById<RecyclerView>(R.id.recyclerView).apply {
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
setHasFixedSize(true)
// use a linear layout manager
layoutManager = viewManager
// specify an viewAdapter
adapter = viewAdapter
}
//recycler view click listener implement
recyclerView.addOnItemClickListener(object: OnItemClickListener {
override fun onItemClicked(position: Int, view: View) {
// Your logic
Toast.makeText(this#MainActivity, locationArrayList[position].locationName, Toast.LENGTH_SHORT).show()
}
})
}
//on item click interface
interface OnItemClickListener {
fun onItemClicked(position: Int, view: View)
}
fun RecyclerView.addOnItemClickListener(onClickListener: OnItemClickListener) {
this.addOnChildAttachStateChangeListener(object: RecyclerView.OnChildAttachStateChangeListener {
override fun onChildViewDetachedFromWindow(view: View?) {
view?.setOnClickListener(null)
}
override fun onChildViewAttachedToWindow(view: View?) {
view?.setOnClickListener({
val holder = getChildViewHolder(view)
onClickListener.onItemClicked(holder.adapterPosition, view)
})
}
})
}
//end of interface
}
If someone interested in the old way implementation..
I posted full example which also reduces your adapter code as well. It uses the old pattern of getting callback..
Project level gradle
buildscript {
ext.kotlin_version = '1.3.10'
repositories {
google()
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"//newly added
classpath 'com.google.gms:google-services:4.1.0' // google-services plugin
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
mavenCentral()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
App level Gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'org.jetbrains.kotlin.android.extensions'//it is used for #Percelize
android {
compileSdkVersion 28
dataBinding {
enabled = true
}
androidExtensions {
experimental = true
}
defaultConfig {
applicationId 'broadpeak.firebase.learning'
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
/*kapt {
generateStubs = true
}*/
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-core:16.0.5'
implementation 'com.google.firebase:firebase-firestore:17.1.3'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.firebaseui:firebase-ui-auth:4.1.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
////kapt "com.android.databinding:compiler:$android_plugin_version"\ // not required above 3.2.0
///kapt "com.android.databinding:compiler:3.1.4"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'
SubjectListActivity.class
class SubjectListActivity : BaseActivity() {
var subjects = mutableListOf<SubjectBO>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.question_list_activity)
recycler_view.itemAnimator = DefaultItemAnimator()
recycler_view.setHasFixedSize(true)
recycler_view.layoutManager = LinearLayoutManager(this#SubjectListActivity)
db.collection("tagCollection").get().addOnSuccessListener { querySnapshot ->
if (querySnapshot.isEmpty()) {
Log.d(TAG, "onSuccess: LIST EMPTY")
} else {
// Convert the whole Query Snapshot to a list
// of objects directly! No need to fetch each document.
subjects = querySnapshot.toObjects(SubjectBO::class.java)
if(subjects.size > 0){
recycler_view.adapter = SubjectAdapter(subjects, object : OnRecyclerItemClickListener {
override fun onItemClicked(view: View?, position: Int) {
var intent = Intent(this#SubjectListActivity,McqActivity::class.java)
intent.putExtra("keyTagBO",subjects.get(position))
startActivity(intent)
}
});
}
}
}.addOnFailureListener { exception ->
exception.printStackTrace()
}
}
SubjectAdapter.class
class SubjectAdapter(items: List<SubjectBO>, onRecyclerItemClickListener: OnRecyclerItemClickListener)
: BaseAdapter<SubjectBO, SubjectViewHolder>(items, onRecyclerItemClickListener) {
override fun onCreateViewHolder(parent: ViewGroup, p1: Int): SubjectViewHolder {
return SubjectViewHolder(parent, R.layout.item_subject, onRecyclerItemClickListener)
}
}
SubjectViewHolder.class
class SubjectViewHolder(parent: ViewGroup, itemLayoutId: Int, onRecyclerItemClickListener:
OnRecyclerItemClickListener) : BaseViewHolder<SubjectBO>(parent, itemLayoutId, onRecyclerItemClickListener) {
override fun bindData(data: SubjectBO) {
itemView.tvTitle.setText(data.tagName)
}
}
BaseAdapter.class
abstract class BaseAdapter<T, U : BaseViewHolder<T>>
(var items: List<T>, var onRecyclerItemClickListener: OnRecyclerItemClickListener)
: RecyclerView.Adapter<U>() {
override fun getItemCount(): Int {
return items.size
}
override fun onBindViewHolder(holder: U, pos: Int) {
holder.bindData(items.get(pos))
}
}
BaseViewHolder.class
abstract class BaseViewHolder<T : BaseModel>(parent: ViewGroup, #LayoutRes itemLayoutId: Int,
var onRecyclerItemClickListener: OnRecyclerItemClickListener) :
RecyclerView.ViewHolder(LayoutInflater.from(parent.context).inflate(itemLayoutId, parent,
false)), View.OnClickListener {
override fun onClick(v: View?) {
onRecyclerItemClickListener.onItemClicked(v, adapterPosition)
}
abstract fun bindData(data: T)
init {
itemView.setOnClickListener(this)
}
}
OnRecyclerItemClickListener.class
interface OnRecyclerItemClickListener{
fun onItemClicked(view: View?, position: Int)
}
Two ways you can have an access to recyclerview in kotlin is first you can directly declare OnClickListener in adapter and make redirection inside it and second way is you can redirect your onclick to fragment/activity where you have set adapter of recycler
1.
class CartAdapter(context: Context) : RecyclerView.Adapter<CartAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.layout_cart, parent, false));
}
override fun getItemCount(): Int {
return 10;
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.itemView.rtbProductRating.setOnClickListener{
var iNavigation= Intent(context,MainActivity::class.java)
iNavigation.flags= Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT
context.startActivity(iNavigation)
// directly redirect your activity from adapter
}
}
class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView)
}
Second way you can have is redirect your adapter click to fragment/activity and then redirect your activity from there instead of redirecting from adapter
class CartAdapter(context: Context, onClickListener: View.OnClickListener) : RecyclerView.Adapter<CartAdapter.ViewHolder>() {
var context = context
var onClickListener = onClickListener
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.layout_cart, parent, false));
}
override fun getItemCount(): Int {
return 10;
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
//set your position to the view
holder.itemView.rtbProductRating.tag = position
//redirect click to the fragment
holder.itemView.rtbProductRating.setOnClickListener {
onClickListener.onClick(holder.itemView.rtbProductRating)
}
// holder.itemView.tv_waybill_count.text = holder.itemView.context.getString(R.string.waybills,5)
}
class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView)
}
Your fragment will look like:
class CartFragment: BaseFragment(),View.OnClickListener {
override val layout= R.layout.frg_cart
override fun onClick(v: View?) {
var position=v?.tag as Int
if(position==0){
var iNavigation= Intent(this,MainActivity::class.java)
iNavigation.flag=Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT
startActivity(iNavigation)
}
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
listener()
}
private fun listener() {
cart_rv.adapter=CartAdapter(activity,this)
}
}
RecyclerItemClickListener
package com.mypackage.custom
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
#Suppress("DEPRECATION")
class RecyclerItemClickListener(context: Context, private val mListener: OnItemClickListener?) : RecyclerView.OnItemTouchListener {
private var mGestureDetector: GestureDetector = GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
override fun onSingleTapUp(e: MotionEvent): Boolean {
return true
}
})
interface OnItemClickListener {
fun onItemClick(view: View, position: Int)
}
override fun onInterceptTouchEvent(view: RecyclerView, e: MotionEvent): Boolean {
val childView = view.findChildViewUnder(e.x, e.y)
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildPosition(childView))
return true
}
return false
}
override fun onTouchEvent(view: RecyclerView, motionEvent: MotionEvent) {}
override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
}
For Activity:
recyclerView!!.addOnItemTouchListener(
RecyclerItemClickListener(this!!, object : RecyclerItemClickListener.OnItemClickListener {
override fun onItemClick(view: View, position: Int) {
//Write your code here
}
})
For Fragment:
recyclerView!!.addOnItemTouchListener(
RecyclerItemClickListener(this!!.activity!!, object : RecyclerItemClickListener.OnItemClickListener {
override fun onItemClick(view: View, position: Int) {
//Write your code here
}
})
OH what the heck someone might like this
We all place edit and trashcan images in out recyclerview and would like something to happen when they are clicked. Here is our Kotlin example
This is in a card view that is inflated in the Adapter
<RelativeLayout
android:id="#+id/editCLICK"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="370dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingTop="12dp">
<ImageView
android:id="#+id/ivEdit"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#color/color_Transparent"
android:src="#drawable/ic_edit"
android:tint="#color/color_lightBlue" />
</RelativeLayout>
then in the Adapter we do some binding
override fun onBindViewHolder(holder: ParentViewHolder, position: Int) {
val items = parentList[position]
holder.item.text = items.dept
holder.editCLICK.setOnClickListener {
val i = Intent(context, EnterParentActivity::class.java)
i.putExtra("FROM", "U")
i.putExtra("MainActId",items.idD)
i.putExtra("PFK",items.fkD)
i.putExtra("ET",items.dept)
i.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(i)
}
}
inner class ParentViewHolder(view: View):RecyclerView.ViewHolder(view){
var item: TextView = view.findViewById(R.id.tvDept) as TextView
var editCLICK: RelativeLayout = view.findViewById(R.id.editCLICK) as RelativeLayout
}
Simple quick and reliable enjoy
I came up with this solution to open an activity when row is clicked, using a companion object and interface. The activity is opened from main activity since I had to save list state before leaving.
Adapter
class MyAdapter(
val dataList: List<objects.ListObject>, val listener: ItemClickListener
) : RecyclerView.Adapter<MyAdapter.ListViewHolder>()
{
companion object {
var mClickListener: ItemClickListener? = null
}
interface ItemClickListener
{
fun clickRow(position: Int)
}
override fun onBindViewHolder(holder: MyAdapter.ListViewHolder, position: Int)
{
holder.bindData(
...
)
mClickListener = listener
holder.itemView.setOnClickListener { view ->
mClickListener?.clickRow(position)
}
}
...
}
Main activity
val context = this
private lateinit var mMyAdapter: MyAdapter
fun initList()
{
mMyAdapter =
MyAdapter(dataList, object : MyAdapter.ItemClickListener
{
override fun clickRow(position: Int)
{
openActivityListItems(position)
}
}
)
}
fun openActivityListItems(position : Int)
{
recyclerViewState = mListView.getLayoutManager()?.onSaveInstanceState()
val intent = Intent(context, ListItems::class.java)
intent.putExtra("Parameter1", dataList[position].Parameter1)
intent.putExtra("Parameter2", dataList[position].Parameter2)
context.startActivity(intent)
}

Categories

Resources