Hello stackoverflow community, i am working on recycler adaptor within fragment, on which i have created an interface on click listener for which i have a callback in fragment. the problem i am facing is its working some times but most of the time its not generating the callback. let me know what i am doing wrong in this..
is it that my View is not getting the click listener, as i have added ripple effect which is not shown on view?
or i need to put the listener in the activity instead of fragment, which i did but no result.
the code to my adatper class
class UserMoneyRequestsAdaptor(
postItems: ArrayList<UserMoneyRequest>?, recItemClick: RecItemClick
) : RecyclerView.Adapter<BaseViewHolder>() {
private var isLoaderVisible = false
var mPostItems: ArrayList<UserMoneyRequest>? = postItems
var recItemClick: RecItemClick? = null
init {
this.recItemClick = recItemClick
}
interface RecItemClick {
fun onAcceptReqest(position: Int, moneyRequestModel: UserMoneyRequest)
fun onCancelReqest(position: Int, moneyRequestModel: UserMoneyRequest)
}
companion object {
private const val VIEW_TYPE_LOADING = 0
private const val VIEW_TYPE_NORMAL = 1
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
return when (viewType) {
VIEW_TYPE_NORMAL -> ViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.user_request_list_item, parent, false)
)
VIEW_TYPE_LOADING -> ProgressHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_loading, parent, false)
)
else -> ViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.user_request_list_item, parent, false)
)
}
}
override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
holder.onBind(position)
holder.setIsRecyclable(false)
}
override fun getItemViewType(position: Int): Int {
return if (isLoaderVisible) {
if (position == mPostItems!!.size - 1) VIEW_TYPE_LOADING else VIEW_TYPE_NORMAL
} else {
VIEW_TYPE_NORMAL
}
}
override fun getItemCount(): Int {
return if (mPostItems == null) 0 else mPostItems!!.size
}
fun addItems(postItems: ArrayList<UserMoneyRequest>) {
mPostItems!!.addAll(postItems!!)
notifyDataSetChanged()
}
fun addLoading() {
isLoaderVisible = true
mPostItems!!.add(
UserMoneyRequest(
"-1", "", "", "", "", "", "", "", "", "", "", UserAccount(
"",
"",
"",
"",
Currency("", "", ""),
User("", "")
)
)
)
notifyItemInserted(mPostItems!!.size - 1)
}
fun addData(listItems: ArrayList<UserMoneyRequest>) {
val size = mPostItems!!.size
mPostItems!!.addAll(listItems)
val sizeNew = this.mPostItems!!.size
notifyItemRangeChanged(size, sizeNew)
}
fun addAll(list: java.util.ArrayList<UserMoneyRequest>) {
mPostItems!!.addAll(list)
notifyDataSetChanged()
}
fun updateItem(pos: Int, paymentLinkListModel: UserMoneyRequest) {
notifyItemChanged(pos)
}
fun addOneItem(postItem: UserMoneyRequest) {
mPostItems!!.add(0, postItem)
notifyDataSetChanged()
}
fun removeOneItem(position: Int) {
mPostItems!!.removeAt(position)
notifyDataSetChanged()
}
fun getlist(positionIndex: Int): UserMoneyRequest {
return mPostItems!![positionIndex]
}
fun removeLoading() {
isLoaderVisible = false
val position = mPostItems!!.size - 1
val item: UserMoneyRequest = getItem(position)
if (item.id == "-1") {
mPostItems!!.removeAt(position)
notifyItemRemoved(position)
}
}
fun clear() {
mPostItems!!.clear()
notifyDataSetChanged()
}
fun getItem(position: Int): UserMoneyRequest {
return mPostItems!![position]
}
inner class ViewHolder internal constructor(itemView: View?) :
BaseViewHolder(itemView) {
private val tvStatus = itemView!!.findViewById(R.id.requestStatus) as TextView
val tvAmount = itemView!!.findViewById(R.id.requestAmount) as TextView
private val tvDate = itemView!!.findViewById(R.id.requestDateAndTime) as TextView
private val requestedFrom = itemView!!.findViewById(R.id.requestedFromId) as TextView
private val requestAccept = itemView!!.findViewById(R.id.requestAcceptStatus) as TextView
private val requestDecline = itemView!!.findViewById(R.id.requestDecline) as TextView
private val requestNotes = itemView!!.findViewById(R.id.requestNotes) as TextView
val context: Context = itemView!!.context
override fun clear() {}
override fun onBind(position: Int) {
super.onBind(position)
val moneyRequest = mPostItems!![position]
tvAmount.text = (moneyRequest.amount.toDouble() / 100).toString() + moneyRequest.user_account.currency.code
tvDate.text = getMonthDateTime(moneyRequest.created_at)
requestedFrom.text = moneyRequest.user_account.user!!.email
requestNotes.text = "Notes: " + moneyRequest.notes
setStatus(context, moneyRequest)
//first on clicklistener
requestAccept.setOnClickListener {
recItemClick!!.onAcceptReqest(position, moneyRequest)
}
//second on clicklistener
requestDecline.setOnClickListener {
recItemClick!!.onCancelReqest(position, moneyRequest)
}
}
@RequiresApi(Build.VERSION_CODES.O)
private fun setStatus(context: Context, moneyRequest: UserMoneyRequest) {
when (moneyRequest.status) {
"0" -> {
tvStatus.text = moneyRequest.statusStr
tvStatus.setBackgroundResource(R.drawable.request_pending_text_bg)
requestAccept.visibility = View.VISIBLE
requestAccept.isClickable = true
requestDecline.visibility = View.VISIBLE
requestDecline.isClickable = true
}
"1" -> {
tvStatus.text = moneyRequest.statusStr
tvStatus.setBackgroundResource(R.drawable.request_active_background_text)
}
"2" -> {
tvStatus.text = moneyRequest.statusStr
tvStatus.setTextColor(
ContextCompat.getColor(
context,
R.color.color_status_rejected
)
)
tvStatus.setBackgroundResource(R.drawable.request_canceled_text_background)
}
"3" -> {
tvStatus.text = moneyRequest.statusStr
tvStatus.setBackgroundResource(R.drawable.request_rejected_text_background)
}
"4" -> {
tvStatus.text = moneyRequest.statusStr
tvStatus.setBackgroundResource(R.drawable.request_completed_text_background)
}
}
}
}
inner class ProgressHolder internal constructor(itemView: View?) :
BaseViewHolder(itemView) {
override fun clear() {}
}
}
my root element of the recycler view
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:id="@+id/relPayoutRequestItem"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/detailsLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginBottom="@dimen/_4sdp"
android:layout_toStartOf="@+id/amountLayout"
android:orientation="vertical"
android:padding="@dimen/_6sdp">
<TextView
android:id="@+id/requestedFromId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:fontFamily="@font/spartanmbbold"
android:lineSpacingExtra="@dimen/_4sdp"
android:lines="1"
android:text="@string/dummy_text_transfers"
android:textColor="@color/text_color"
android:textSize="@dimen/_10sdp"
android:textStyle="bold" />
<TextView
android:id="@+id/requestNotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_2sdp"
android:fontFamily="@font/spartanmb_semibold"
android:lineSpacingExtra="@dimen/_4sdp"
android:text="Notes: NIL"
android:textColor="@color/text_color"
android:textSize="@dimen/_10sdp"
android:textStyle="normal" />
</LinearLayout>
<LinearLayout
android:id="@+id/amountLayout"
android:layout_marginTop="@dimen/_14sdp"
android:layout_marginBottom="@dimen/_4sdp"
android:layout_marginLeft="@dimen/_10sdp"
android:layout_marginRight="@dimen/_10sdp"
android:layout_alignParentEnd="true"
android:orientation="vertical"
android:layout_width="@dimen/_68sdp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/requestAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/spartan_mb_medium"
android:textStyle="bold"
android:lines="1"
android:ellipsize="end"
android:textSize="@dimen/_10sdp"
android:layout_gravity="center_vertical|end"
android:textColor="@color/text_color"
android:lineSpacingExtra="@dimen/_4sdp"
android:text="@string/dummy_transaction_amount"/>
</LinearLayout>
<RelativeLayout
android:id="@+id/bottomlayout"
android:layout_below="@+id/detailsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/requestDateAndTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="@dimen/_6sdp"
android:layout_marginTop="@dimen/_12sdp"
android:ellipsize="end"
android:fontFamily="@font/spartan_mb_medium"
android:lineSpacingExtra="@dimen/_6sdp"
android:lines="1"
android:text="@string/dummy_transfer_time"
android:textColor="@color/transaction_time_text_color"
android:textSize="@dimen/_8sdp"
android:textStyle="normal" />
<TextView
android:layout_centerVertical="true"
android:id="@+id/requestStatus"
android:layout_toStartOf="@+id/acceptRipple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/spartanmbbold"
android:textStyle="bold"
android:layout_marginEnd="@dimen/_4sdp"
android:lines="1"
android:gravity="center"
android:layout_gravity="end"
android:textSize="@dimen/_8sdp"
android:textColor="@color/white"
android:lineSpacingExtra="@dimen/_6sdp"
android:text="@string/dummy_payment_method"/>
<!-- first view for clicklistener-->
<com.balysv.materialripple.MaterialRippleLayout
android:id="@+id/acceptRipple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginTop="@dimen/_6sdp"
android:layout_marginEnd="@dimen/_4sdp"
app:mrl_rippleAlpha="0.2"
android:layout_toStartOf="@+id/requestRippleDecline"
app:mrl_rippleColor="@color/ripple_black"
app:mrl_rippleHover="true"
app:mrl_rippleOverlay="true"
app:mrl_rippleRoundedCorners="@dimen/_12sdp">
<TextView
android:id="@+id/requestAcceptStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/spartanmbbold"
android:textStyle="bold"
android:visibility="gone"
android:lines="1"
android:gravity="center"
android:layout_gravity="end"
android:background="@drawable/user_request_active_background_text"
android:textSize="@dimen/_8sdp"
android:textColor="@color/white"
android:lineSpacingExtra="@dimen/_6sdp"
android:text="@string/accept"/>
</com.balysv.materialripple.MaterialRippleLayout>
<!-- second view for clicklistener-->
<com.balysv.materialripple.MaterialRippleLayout
android:id="@+id/requestRippleDecline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginTop="@dimen/_6sdp"
android:layout_marginEnd="@dimen/_10sdp"
app:mrl_rippleAlpha="0.2"
app:mrl_rippleColor="@color/ripple_black"
app:mrl_rippleHover="true"
app:mrl_rippleOverlay="true"
app:mrl_rippleRoundedCorners="@dimen/_12sdp">
<TextView
android:id="@+id/requestDecline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/spartanmbbold"
android:textStyle="bold"
android:lines="1"
android:background="@drawable/user_request_rejected_text_background"
android:gravity="center"
android:visibility="gone"
android:layout_gravity="end"
android:textSize="@dimen/_8sdp"
android:textColor="@color/white"
android:lineSpacingExtra="@dimen/_6sdp"
android:text="@string/decline"/>
</com.balysv.materialripple.MaterialRippleLayout>
</RelativeLayout>
<View
android:layout_marginStart="@dimen/_20sdp"
android:layout_marginEnd="@dimen/_15sdp"
android:layout_marginTop="@dimen/_10sdp"
android:layout_below="@+id/bottomlayout"
android:layout_width="match_parent"
android:layout_height="@dimen/_1sdp"
android:background="@color/settings_screen_seperator" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
the problem was within the view, i was using nested scroll view which was not generating the click listener