How to send RecyclerView data to a BottomSheetDialog - android

I am developing a Recyclerview that pressing a button should open a BottomSheetDialog. I can open BottomSheet but I can't pass data to it. I tried using an interface earlier but it didn't work.
class MyAdapter(private val listaItens: List<Itens>, private val context: Context,
private val fragmentManager: FragmentManager) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val POST_TXT = 0
private val POST_IMG = 1
//some code ...
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
when (viewType) {
POST_TXT -> {
val item = LayoutInflater.from(parent.context).inflate(R.layout.text, parent, false)
return ViewHolderTexto(item)
}
POST_IMG -> {
val item = LayoutInflater.from(parent.context).inflate(R.layout.image, parent, false)
return ViewHolderImage(item)
}
//some code ...
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val item = listaItens[position]
val usuarioLogado = UsuarioFirebase.getDadosUsuarioLogado()
when (holder.itemViewType) {
POST_TXT -> {
val viewHolderTexto = holder as ViewHolderTexto
viewHolderTexto.setIsRecyclable(false)
//some code ...
}
POST_IMG -> {
val viewHolderImage = holder as ViewHolderImage
viewHolderImage.setIsRecyclable(false)
//some code ...
holder.imageComentarioPostagemImage.setOnClickListener {
val comentariosBottomSheet = ComentariosBottomSheet()//open bottom sheet
comentariosBottomSheet.show(fragmentManager, comentariosBottomSheet.tag)
}
//some code ...
}
}
}
How to send RecyclerView data to a BottomSheetDialog?

Why don't you simply pass the data by constructor param of the Bottom sheet
val comentariosBottomSheet = ComentariosBottomSheet(data)//open bottom sheet
comentariosBottomSheet.show(fragmentManager, comentariosBottomSheet.tag)

How about his approach:
function/interface passed into the adapter
Note function is passed as last parameter
class MyAdapter( .... private val onClickCallBack:(item :Itens) ... {
....
}
Register clicklistener on the holder
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int){
val item = listaItens[position]
val usuarioLogado = UsuarioFirebase.getDadosUsuarioLogado()
....
holder.itemView.setOnClickListener{
onClickCallBack(item)
}
Add callback to the adapter where you want to show the bottom (sheet ex Activity)
As noted above the function was passed as last parameter to the Aconstructor so we can call it after like this:
val myAdapter:MyAdapter = MyAdapter(){
//ShowBottomScree
// 'it' is refenced as the passed data from adapter.
}

Related

RecyclerViewAdapter in Android Kotlin

I have an array that save different sensors data (in non-activity class) and I want the RecyclerView to be updated based on data of that array. Is it possible to automatically change the presented data?
I used setOnClickListener but nothing updated. The RecyclerView just display the default data. Also, I used text view however the data is updated with each click not continuously.
Thanks for your help.
In fragment:
override fun onClick(v: View?) {
when (v?.id) {
R.id.buttonStart-> {
start(v)
}
R.id.buttonStop-> {
stop(v)
}
}
}
fun start (_v: View) {
listListSensors.adapter = SensorsRecyclerViewAdapter(model.listSensors(), mListener!!)
}
In the other class:
fun listSensors(): Sensors {
return currentSensor
}
In recycle view:
class SensorsRecyclerViewAdapter (items: Sensors, listener: ListSensorsFragment.OnListFragmentInteractionListener)
: RecyclerView.Adapter<SensorsRecyclerViewAdapter.SensorsViewHolder>() {
private var mValues: Sensors = items
private var mListener: ListSensorsFragment.OnListFragmentInteractionListener = listener
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SensorsViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.recyclerviewlistsensors_item, parent, false)
return SensorsViewHolder(itemView)
}
override fun onBindViewHolder(holder: SensorsViewHolder, position: Int) {
holder.mItem = mValues
holder.listSensorsLightView.text = mValues.getLight()
holder.listSensorsTemperatureView.text = mValues.getTemperature()
holder.listSensorsGyroscopeView.text = mValues.getGyroscope()
holder.listSensorsAccelerometerView.text = mValues.getAccelerometer()
holder.listSensorsGravityView.text = mValues.getGravity()
holder.mView.setOnClickListener { mListener.onListFragmentInteraction(holder.mItem) }
}
in your Adapter
var itemClickListener: ((position: Int, name: String) -> Unit)? = null
fun setData(data:List<AnyList>){
this.list.clear()
this.list.addAll(data)
notifyDataSetChanged()
}
//bindviewholder
itemClickListner.invoke(1,"anyvalue")
// in fragment
adapter.itemClickListener = {
position, name ->
Toast.makeText(requireContext(),"position is $position name is $name ",Toast.LENGTH_SHORT).show()
}
// call setData() any place in fragment

Kotlin : How to click on item in a Recyclerview Inside another Recyclerview

After seen all the similiar questions, I don't find a solution of my problem, which the description as follows:
In my android app, I use Kotlin as language. In my app, I have a lot of category and each category have a list of products. In my home activity, I create a vertical RecyclerView named "productListOfCategory". In the "productListOfCategory" adapter,a textView to display a category name and a recyclerView to display all related product list. The following code is the description of "ProductListOfProductTypeAdapter" adapter : (ProductType = category)
class ProductListOfProductTypeAdapter(private val context: Context, private val ProductTypeIDWithProduct: Map<String, Array<ProductData>>, private val productTypeDetail: Array<ProductTypeData>)
: RecyclerView.Adapter<ProductListOfProductTypeAdapter.ViewHolder>(),ProductOfProductTypeAdapter.OnItemClickListener{
override fun onItemClick(view: View, viewModel: ProductData) {
val intent = Intent(context,ProductDetail::class.java)
context.startActivity(intent)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_list_product_product_type, parent, false)
return ViewHolder(view)
}
override fun getItemCount() = ProductTypeIDWithProduct.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val itemType = ProductTypeIDWithProduct.keys.elementAt(position)
holder.productTypeName.text = getName(itemType)
val arrayProductOfProductType = ProductTypeIDWithProduct[itemType] as ArrayList<ProductData>
holder.productListOfProductType.apply {
holder.productListOfProductType.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
holder.productListOfProductType.adapter = ProductOfProductTypeAdapter(arrayProductOfProductType,context,this#ProductListOfProductTypeAdapter)
}
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val productTypeName: TextView = itemView.textViewProductTypeName
val productListOfProductType: RecyclerView = itemView.recyclerViewProductOfProductType
}
"ProductOfProductTypeAdapter" is the second adapter which the code as the following :
class ProductOfProductTypeAdapter(private val products: ArrayList<ProductData>, private val context: Context,private val mListener: OnItemClickListener)
: RecyclerView.Adapter<ProductOfProductTypeAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_product_featured, parent, false)
return ViewHolder(view)
}
override fun getItemCount() = products.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val productItem = products[position] as Map<String, Map<String, String>>
holder.productName.text = productItem["name"]?.get("En").toString()
holder.productWeight.text = productItem["weight"].toString().plus(context.resources.getString(R.string.ml))
holder.productPrice.text = "$".plus(productItem["price"].toString()).plus("/")
holder.productRating.text = productItem["reviewsValue"].toString()
holder.itemView.setOnClickListener {
mListener.onItemClick(holder.itemView, products[position])
notifyDataSetChanged()
}
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val productName: TextView = itemView.itemProdFeaturedName
val productPrice: TextView = itemView.itemProdFeaturedPrice
val productImage: ImageView = itemView.itemProdFeaturedImage
val productWeight: TextView = itemView.txtViewWeight
val productRating: TextView = itemView.itemProdFeaturedRate
}
interface OnItemClickListener {
fun onItemClick(view: View,viewModel: Map<String, Map<String, String>>)
}
My problem is How to click on product and dispaly the product detail activity.I try as the following but still not get what I want.
Shouldn't your OnItemClickListener be like the following?
interface OnItemClickListener {
fun onItemClick(view: View, product: ProductData)
}
And you need to change the way you start your ProductDetail activity, and put your product ID or something to identify the product selected in the extra data of the intent. For example:
val intent = Intent(context,ProductDetail::class.java)
intent.putExtra("PRODUCT_ID", product.id)
context.startActivity(intent)

Editing list item on list view

Imagine we have a simple list of items. Each item contains only a short title.
To handle the list we are using RecyclerView with ListAdapter and ViewHolders.
Each item/view is not editable unless we click it.
In this scenario I am using one view model for list and one for item under edit.
Unfortunately all my attempts failed.
I have tried to use two different view holders but the list was flickering, after all inflating view (in this case binding) is heavy.
Another shot I was giving to use the same view holder but with two various bind methods - one binding plain item, second binding with viewmodel instead of data object but it failed as well - suddenly a few rows were editable.
Has anyone solved it ?
class MistakesAdapter(private val editViewModel: MistakeEditViewModel) :
ListAdapter<Mistake, RecyclerView.ViewHolder>(MistakesDiffCallback()) {
companion object{
const val ITEM_PLAIN_VIEW_TYPE = 0
const val ITEM_EDITABLE_VIEW_TYPE = 1
}
private var itemPositionUnderEdit = -1
private val listener = object: MistakeItemListener{
override fun onClick(view: View, position: Int) {
Timber.d("OnClick : edit - $itemPositionUnderEdit, clickPos - $position")
editViewModel.onEditMistake(getItem(position))
itemPositionUnderEdit = position
notifyItemChanged(itemPositionUnderEdit)
}
}
override fun getItemViewType(position: Int) =
when (position) {
itemPositionUnderEdit -> ITEM_EDITABLE_VIEW_TYPE
else -> ITEM_PLAIN_VIEW_TYPE
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
when (viewType) {
ITEM_EDITABLE_VIEW_TYPE -> EditableMistakeViewHolder.from(parent)
else -> MistakeViewHolder.from(parent)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is EditableMistakeViewHolder -> holder.bind(editViewModel, listener)
is MistakeViewHolder -> holder.bind(getItem(position), listener)
else -> throw ClassCastException("Unknown view holder type")
}
}
class MistakeViewHolder private constructor(private val binding: ListItemMistakesBinding) :
RecyclerView.ViewHolder(binding.root) {
companion object {
fun from(viewGroup: ViewGroup): MistakeViewHolder {
val inflater = LayoutInflater.from(viewGroup.context)
val binding = ListItemMistakesBinding.inflate(inflater, viewGroup, false)
return MistakeViewHolder(binding)
}
}
fun bind(item: Mistake, listener: MistakeItemListener) {
binding.apply {
mistake = item
inputType = InputType.TYPE_NULL
this.listener = listener
position = adapterPosition
executePendingBindings()
}
}
}
class EditableMistakeViewHolder private constructor(private val binding: ListItemMistakesBinding)
: RecyclerView.ViewHolder(binding.root) {
companion object{
fun from(viewGroup: ViewGroup): EditableMistakeViewHolder {
val inflater = LayoutInflater.from(viewGroup.context)
val binding = ListItemMistakesBinding.inflate(inflater, viewGroup, false)
return EditableMistakeViewHolder(binding)
}
}
fun bind(viewModel: MistakeEditViewModel, listener: MistakeItemListener){
binding.apply {
this.viewModel = viewModel
inputType = InputType.TYPE_CLASS_TEXT
this.listener = listener
position = adapterPosition
root.setBackgroundColor(Color.GRAY)
}
}
}
}
class MistakeEditViewModel(private val repository: MistakesRepository) : ViewModel() {
#VisibleForTesting
var mistakeUnderEdit: Mistake? = null
//two-way binding
val mistakeName = MutableLiveData<String>()
fun onEditMistake(mistake: Mistake) {
mistakeUnderEdit = mistake
mistakeName.value = mistake.name
}
}
By changing my approach to the problem I solved it.
I make all list items editable but at the same time I am following focus.
To cut the long story short, I invoke item view model methods with help of OnFocusChangeListener and TextWatcher on my editTexts.

(Kotlin) Position of RecyclerView returns -1 when trying to click on an item

I'm new to Android development (and Kotlin).
I'm trying to implement a RecyclerView (which works fine) and when I click on a specific row it opens a new activity (Intent).
However, whenever I've press/click on one of the rows, I'm only able to get the value "-1" returned.
I've tried a number of different approaches (you should see the number of tabs in my browser).
This seems like it should be a fairly straightforward occurrence for something as common as a RecyclerView, but for whatever reason I'm unable to get it working.
Here is my RecyclerView Adapter file:
class PNHLePlayerAdapter (val players : ArrayList<PNHLePlayer>, val context: Context) : RecyclerView.Adapter<ViewHolder>() {
var onItemClick: ((Int)->Unit) = {}
// Gets the number of items in the list
override fun getItemCount(): Int {
return players.size
}
// Inflates the item views
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val itemView = LayoutInflater.from(context).inflate(
R.layout.pnhle_list_item,
parent,
false
)
val viewHolder = ViewHolder(itemView)
itemView.setOnClickListener {
onItemClick(viewHolder.adapterPosition)
}
return ViewHolder(itemView)
}
// Binds each item in the ArrayList to a view
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.tvPlayerName?.text = players[position].Name
holder.tvPlayerRank?.text = position.toString()
holder.tvPNHLe?.text = players[position].PNHLe.toString()
holder.tvTeam?.text = players[position].Team
holder.ivLeague?.setImageResource(leagueImageID)
}
}
class ViewHolder (view: View) : RecyclerView.ViewHolder(view) {
val linLayout = view.hor1LinearLayout
val ivTeam = view.teamImageView
val tvPlayerName = view.playerNameTextView
val tvPlayerRank = view.rankNumTextView
val tvPNHLe = view.pnhleTextView
val tvTeam = view.teamTextView
val ivLeague = view.leagueImageView
}
As you can see, there is a class property "onItemClick" which uses a lambda as the click callback.
I setOnClickListener in the onCreateViewHolder method after the view is inflated.
Next, in my Activity I add the list to my Adapter and set the call back.
However, every time I 'Toast' the position it is displayed as '-1'.
val adapter = PNHLePlayerAdapter(list, this)
adapter.onItemClick = { position ->
Toast.makeText(this, position.toString(),Toast.LENGTH_SHORT).show()
var intent = Intent(this, PlayerCardActivity::class.java)
//startActivity(intent)
}
rv_player_list.adapter = adapter
Perhaps I'm not thinking about this properly, but shouldn't the position represent the row number of the item out of the RecyclerView???
Ideally, I need to use the position so that I can obtain the correct item from the 'list' (ArrayList) so that I can pass information to my next Activity using the Intent
I found the issue.
Change this line in onCreateViewHolder:
return ViewHolder(itemView)
to this one:
return viewHolder
I would reorganize the adapter like this:
class PNHLePlayerAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<Adapter.ViewHolder>() {
interface AdapterListener {
fun onItemSelected(position: Int?)
}
var players: List<Player> = listOf()
set(value) {
field = value
this.notifyDataSetChanged()
}
var listener: AdapterListener? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_car_selector, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(position)
}
override fun getItemCount(): Int {
return brands.size
}
inner class ViewHolder(view: View): androidx.recyclerview.widget.RecyclerView.ViewHolder(view) {
private var position: Int? = null
private val baseView: LinearLayout? = view.findViewById(R.id.baseView) as LinearLayout?
...
init {
baseView?.setOnClickListener {
listener?.onManufacturerSelected(position)
}
}
fun bind(position: Int) {
this.position = position
...
}
}
}
And from your activity/fragment set the listener as adapter.listener = this, and implement the onItemSelected(position: Int?)
override fun onItemSelected(position: Int?) {
...
}

Can't assign new value to a simple Singleton using an onClickeListener for a RecyclerView

I am trying to build a Slack-like chat app following a tutorial in a course I am taking online.
In the tutorial the instructor is using a ListView and the OnItemClickListener method, but I am trying to do it with recycler view, and I am stuck with the onClickListener in the adapter.
I have tried to find answers in other questions but couldn't find one that solves my problem. The closest ones were this and this
My two problems are:
1. The app's main activity has on the top of the screen a title that states what channel is currently active. I have created a singleton that holds the "current channel" and the title's text is being pulled from that singleton.
I am having a hard time changing the value of that singleton on click.
The main activity also has all the channels in a list view in a drawer.
I am trying to close the drawer when a channel is clicked but that isn't happening either.
This is my current adapter:
class ChannelsAdapter(val context: Context, val channels: ArrayList<Channel>) :
RecyclerView.Adapter<ChannelsAdapter.Holder>() {
inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val singleChannel = itemView.findViewById<TextView>(R.id.single_channel)
val mainLayout = LayoutInflater.from(context).inflate(R.layout.activity_main, null)
fun bindText(textVar: String, context: Context) {
singleChannel.text = textVar
}
}
override fun onBindViewHolder(holder: Holder, position: Int) {
holder.bindText(channels[position].toString(), context)
holder.itemView.setOnClickListener {
ChannelName.activeChannel = channels[position]
holder.mainLayout.drawer_layout.closeDrawer(GravityCompat.START)
}
}
override fun getItemCount(): Int {
return channels.count()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChannelsAdapter.Holder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.channel_list_layout, parent, false)
return Holder(view)
}
}
This is the singleton
object ChannelName {
var activeChannel : Channel? = null
}
You can rewrite the setter for activeChanell variable and call a listener that has been added before to notify your Activity:
object ChannelName {
private val listeners = ArrayList<(Channel?) -> Unit>()
fun addChannelNameChangedListener(listener: (Channel?) -> Unit) {
listeners.add(listener)
}
fun removeChannelNameChangedListener(listener: (Channel?) -> Unit) {
listeners.remove(listener)
}
var activeChannel: Channel? = null
set(value) {
field = value
listeners.forEach { it.invoke(value) }
}
}
And inside the Activity add a listener like this:
ChannelName.addChannelNameChangedListener {
// Do your operation
}
The alternative solution is to use Observable utils like LiveData, so you shouldn't worry about the Android life cycle any more:
object ChannelName {
val activeChannel: MutableLiveData<ChannelName> = MutableLiveData()
}
To change the value inside your adapter simply call:
ChannelName.activeChannel.value = channels[position]
And inside your activity Observe to the variable by calling:
ChannelName.activeChannel.observe(this, Observer {
// Do your operation
})
class ChannelsAdapter(val context: Context, val channels: ArrayList<Channel>) :
RecyclerView.Adapter<ChannelsAdapter.Holder>() {
private var itemClickListener: OnItemClickListener? = null
fun setItemClickListener(itemClickListener: OnItemClickListener) {
this.itemClickListener = itemClickListener
}
interface OnItemClickListener {
fun onItemClick(position: Int)
}
inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
val singleChannel = itemView.findViewById<TextView>(R.id.single_channel)
val mainLayout = LayoutInflater.from(context).inflate(R.layout.activity_main, null)
fun bindText(textVar: String, context: Context) {
singleChannel.text = textVar
}
override fun onClick(v: View?) {
val position = adapterPosition
itemClickListener?.let {
if (position != RecyclerView.NO_POSITION) {
it.onItemClick(position)
}
}
}
}
override fun onBindViewHolder(holder: Holder, position: Int) {
holder.bindText(channels[position].toString(), context)
}
override fun getItemCount(): Int {
return channels.count()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChannelsAdapter.Holder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.channel_list_layout, parent, false)
return Holder(view)
}
}
This way you can setItemClickListener to the adapter in your activity and get callback from your recyclerView.
You should not set listener in onBind() method since it will be called more than your items' count.

Categories

Resources