Comments (7)
Thanks a ton!!! You saved my day
from quantitypickerview.
Hello @Bharath-D ,
Can you show me your mListAdapter.setOnItemChildClickListener
implementation?
At first I don't see how android:onClick="click"
would work.
Here's an example where the main idea is to provide a "callback" to the Adapter that later will use it inside view.setOnClickListener call:
class ProductListAdapter(private val onClick: ((View, Product) -> Unit)? = null,...){
...
override fun onBindViewHolder(...) {
...
holder.quantityPicker.setOnClickListener {
onClick?.invoke(it, items[holder.adapterPosition])
}
}
}
Hope it helped.
from quantitypickerview.
Sorry, I don't use android:onClick
I am using https://github.com/CymChad/BaseRecyclerViewAdapterHelper/ library to create adapter
``class HomeitemQuickAdapter(
layoutResId: Int, sectionHeadResId: Int, data: List<OfferProduct>?
) : BaseSectionQuickAdapter<SectionHeaderHelper, BaseViewHolder>(
sectionHeadResId,
layoutResId,
data as MutableList<SectionHeaderHelper>?
) {
private lateinit var listener:QuantityPickerView.QuantityPickerViewActionListener
init {
addChildClickViewIds(R.id.quantitypicker)
}
I added child using the above code and then called it in Activity using
mListAdapter.setOnItemChildClickListener { adapter, view, position ->
if(view.id == R.id.quantitypicker) {
showToast("hai")
}
All other children are working when clicked except this
from quantitypickerview.
Well actually I was reviewing my code and I think it could be related with how BaseRecyclerViewAdapterHelper handles view clicks. But first I'll explain my logic:
To perform clicks and events in this QuantityPickerView
the correct way to do so is by implementing:
holder.quantityPickerView.addActionListener(
onValueChanged = { view, value -> ... },
onToggleFinish = { isOpen -> ... },
beforeStartToggle = { willOpen -> ...}
)
Now if for some reason you need to intercept onTouchListener, you can but for things to work you have to forward the event:
holder.quantityPickerViewsetOnTouchListener { v, event ->
//do your logic and let the view to its logic too (or block it but if you do no ActionListener will be called)
return@setOnTouchListener v.onTouchEvent(event)
}
So, following my previous response logic, you should:
class ProductListAdapter(private val itemUpdate: ((Product, Int) -> Unit)? = null,...){
...
override fun onBindViewHolder(...) {
...
holder.quantityPicker.addActionListener(
onValueChanged = { view, value -> itemUpdate?.invoke(item, value)},
//other callbacks if needed
}
}
}
Every time the item gets updated it delegates to activity.
Now, let's see how the adapter's author handles click logic:
https://github.com/CymChad/BaseRecyclerViewAdapterHelper/blob/046c2be8c4ae5b5d39177e86d4ffc27bd803ae64/library/src/main/java/com/chad/library/adapter/base/BaseBinderAdapter.kt#L153
As you can see, it's just view.setOnClickListener
logic, which, with this view won't work.
My advice, do you really need that complex Adapter? I've another repo that uses a simple base adapter, maybe it's enough for you, just copy & paste the code needed. This way you gain power over your adapter and you're free to do your custom logic, instead of being tied to "SuperGenericAdapter".
If you don't want to move away from BaseRecyclerViewAdapterHelper, well for QuantityPickerView
ignore addChildClickViewIds(R.id.quantitypicker)
and create a private method that implements the code I shared above, it will work just fine.
I hope I've helped.
from quantitypickerview.
First of all thank you for your help. Your library is just awesome.
Following is my adapter
`package com.bigone.pvm_mart.ui.home.adapter
import android.graphics.Paint
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources
import androidx.recyclerview.widget.DiffUtil
import com.bigone.pvm_mart.R
import com.bigone.pvm_mart.service.model.OfferProduct
import com.bigone.pvm_mart.utlis.UiHelper.showToast
import com.chad.library.adapter.base.BaseSectionQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.github.guilhe.views.QuantityPickerView
import com.squareup.picasso.Picasso
class HomeitemQuickAdapter(
layoutResId: Int,
sectionHeadResId: Int,
data: List<OfferProduct>?
) :
BaseSectionQuickAdapter<SectionHeaderHelper, BaseViewHolder>(sectionHeadResId, layoutResId, data as MutableList<SectionHeaderHelper>?) {
override fun convert(holder: BaseViewHolder, item: SectionHeaderHelper) {
val items = item.obj as OfferProduct
items.let {
Picasso.get().load(it.image)
.placeholder(AppCompatResources.getDrawable(context, R.drawable.dummy_fruits)!!)
.error(AppCompatResources.getDrawable(context, R.drawable.dummy_fruits)!!)
.into(holder.getView(R.id.item_image) as ImageView)
holder.setText(R.id.item_name, it.name).setText(R.id.qty, it.unit_measure + it.unit)
.setText(R.id.offerPrice, "₹ " + it.special_price.toString())
// TODO: 7/21/2020 quantity picker
(holder.getView(R.id.picker) as QuantityPickerView)
}
}
//Set Header
override fun convertHeader(helper: BaseViewHolder, item: SectionHeaderHelper) {
if(item.isHeader) {
helper.setText(R.id.header, item.obj.toString())
}
}
//update items
fun updateItems(newItems: List<SectionHeaderHelper>) {
val diffResult = DiffUtil.calculateDiff(object : DiffUtil.Callback() {
override fun getOldListSize(): Int {
return data.size
}
override fun getNewListSize(): Int {
return newItems.size
}
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return data[oldItemPosition].isHeader == newItems?.get(newItemPosition)?.isHeader && data[oldItemPosition].obj == newItems[newItemPosition].obj
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return true
}
})
diffResult.dispatchUpdatesTo(this)
this.data.clear()
this.data.addAll(newItems)
}
fun addData(): Int {
var i:Int=0
return i
}
}`
Convert view is same as bind view holder. But I don't know how to implement your listener
Do you have any idea?
from quantitypickerview.
See if this helps :)
1 - Add a high-order function called itemUpdate
to work as a "callback".
2 - Add an addActionListener
to QuantityPickerView
inside convert function.
3 - In your Activity/Fragment use that callback.
Like this:
YourAdapter.kt
class HomeitemQuickAdapter(
...,
private val itemUpdate: ((Product, Int) -> Unit)? = null
) : BaseSectionQuickAdapter...) {
override fun convert(holder: BaseViewHolder, item: SectionHeaderHelper) {
val items = item.obj as OfferProduct
items.let {
...
(holder.getView(R.id.picker) as QuantityPickerView).addActionListener(
onValueChanged = { view, value -> itemUpdate?.invoke(item, value)}
)
}
}
...
}
YourActivity.kt
val adapter = HomeitemQuickAdapter(..., itemUpdate = { product, quantity -> //do your logic) }
from quantitypickerview.
Glad I could help 😉
from quantitypickerview.
Related Issues (6)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from quantitypickerview.