RecyclerView with various elements in the same line - android

I would like to make a RecyclerView having differents elements per line depending on a boolean in the adapter.
In my app, you have some banknote and coins. I have to put all in a RecyclerView. First of all I want to show the banknote (2 per 2) and after the coins (4 per 4)
like this :
This is how I want the result look like :
This is my Adapter
class MoneyAdapter(private val money_list: ArrayList<Money>) : RecyclerView.Adapter<MoneyAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MoneyAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.item_money, parent, false)
return ViewHolder(v)
}
override fun onBindViewHolder(holder: MoneyAdapter.ViewHolder, position: Int) {
money_map.put(money_list[position].value, 0)
holder.bindItems(money_list[position])
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(money: Money) {
// if (money.isCoin) I would like to put it on a 4 element per line
}
}
}
override fun getItemCount(): Int {
return money_list.size
}
}
This is where I declar emy RecyclerView :
recycler_money.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
recycler_money.adapter = MoneyAdapter(getMoneyList(PrefsModel.currency)
I was searching a lot of examples but it's always with differents col/row using StaggeredGridLayoutManager and I want only change the number of elements by line (2 or 4) directly on the Adapter.

I found the solution thanks to #Gautam :
val l = GridLayoutManager(context, 4)
l.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
if (getMoneyList(PrefsModel.currency)[position].type == MONEY_PIECE)
return 1
else
return 2
}
}
recycler_money.layoutManager = l

Related

How to get the total count in cart using Recyclerview Adapter

Hi I am using Recyclerview with Adapter in my Android application. In my app I have product listing screen, where user can set the quantity in every list item. I am using two imageviews for increment and decrement the quantity. Now issue is , if I make 2 quantity for first product and 4 for second product, total count should be 6 for my cart screen, but I am not able to get total count. Following is my code for adapter, can anyone help me ?
class ProductAdapter(private val cellClickListener: CellClickListener) : RecyclerView.Adapter<ProductViewHolder>() {
var productsList = mutableListOf<Product>()
var cartList = mutableListOf<Product>()
/* fun setMovieList(movies: List<MobileList>) {
this.movies = movies.toMutableList()
notifyDataSetChanged()
}*/
fun addData(data:List<Product>){
productsList.addAll(data)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = AdapterLayoutBinding.inflate(inflater, parent, false)
return ProductViewHolder(binding)
}
override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
val products = productsList[position]
holder.binding.name.text = products.name
holder.binding.price.text = products.price
Glide.with(holder.itemView.context).load(products.image_url).into(holder.binding.imageview)
var cartCount : Int=0
holder.binding.cartPlus.setOnClickListener {
cartCount++
holder.binding.cartValue.text = cartCount.toString()
cartList.add(products)
//println(cartList)
}
holder.binding.cartMinus.setOnClickListener {
cartCount--
holder.binding.cartValue.text = cartCount.toString()
cartList.remove(products)
// println(cartList)
}
fun updatedData( updatedcartList:List<Product>){
cartList.addAll(updatedcartList)
}
holder.itemView.setOnClickListener {
cellClickListener.onCellClickListener(products,cartCount)
}
}
override fun getItemCount(): Int {
return productsList.size
}
}
class ProductViewHolder(val binding: AdapterLayoutBinding) : RecyclerView.ViewHolder(binding.root) {
}
interface CellClickListener {
fun onCellClickListener(data: Product,count:Int)
}
use cartlist.size() for total count.
and for using in activity define this in Adapter class:
class ProductAdapter(private val..
{
...
fun getCartSize():Int {
return cartlist.size()
}
...
}
and in the activity you can use :
adapter.getCartsize()
since you are just passing the list into the recycleview and want to use it in fragment or activity. I would suggest using the list to get the total count for your items instead. You can use sumOf to your list.
you can use it like this before you pass to recyclver view.
val totalCount = list.sumOf { it.quantity }
or
txtView.text = list.sumOf {it.quantity}.toString()

How to add onClickListener to the items of the recyclerView in Kotlin?

I am using recyclerView to show data from firebase database and I want to handle clicks,
Now the important part is that I want to know the number that was clicked in order to test google play in app billing before showing the next activity
I mean user should click item number one then pay to see information number 1 and so on
Any help, please ?
//my adapter
class MyAdapter(
private val arrayList: ArrayList<Long>
) :
RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view =
LayoutInflater.from(parent.context)
.inflate(R.layout.layout_item, parent, false)
return MyViewHolder(view)
}
override fun getItemCount() = arrayList.size
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.number.text = arrayList[position].toString()
}
class MyViewHolder(view: View) :
RecyclerView.ViewHolder(view) {
val number = view.findViewById<View>(R.id.singleNumberId) as TextView
}
}
Here is a small example I have of registering a click for a RecyclerView adapter item:
class PatientListAdapter : ListAdapter<Patient, PatientListAdapter.PatientViewHolder>(co.za.abcdefgh.viewmodels.PatientListViewModel.DiffItemCallback) {
// this property will be used to set the onclick callback for the entire adpater
var onPatientSelectedCallback: PatientSelectedCallback? = null
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): PatientViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_patient, parent, false) as View
return PatientViewHolder(view)
}
override fun onBindViewHolder(holder: PatientViewHolder, position: Int) {
holder.view.item_patient_name.text = getItem(position).toString()
holder.view.item_patient_folderNumber.text = getItem(position).folderNumber
// lets set our on click for each viewholder here
holder.view.item_patient_info_card.setOnClickListener {
// the secret sauce .... getItem(holder.adapterPosition)
onPatientSelectedCallback?.onPatientSelected(getItem(holder.adapterPosition))
}
}
class PatientViewHolder(val view: View) : RecyclerView.ViewHolder(view)
// interface which defines a method signature that will called when a item in the adpater is selected
interface PatientSelectedCallback {
fun onPatientSelected(patient: Patient)
}
}
and then wherever you use the adapter after instantiating simply do:
val viewAdapter = PatientListAdapter()
viewAdapter.onPatientSelectedCallback =
object : PatientListAdapter.PatientSelectedCallback {
override fun onPatientSelected(patient: Patient) {
// do something with the chosen item
patientViewModel.setPatient(patient)
}
}

Android: itemdecoration in recyclerview to be text and only if a certain condition is met

I have a recyclerview that shows items that were bought and to be bought. The items are sorted by purchase date which can be in the past and future.
I want to add a separator between the last "past" item and the first "future" one. I know I should add a decorator like the following:
DividerItemDecoration decoration = new DividerItemDecoration(Objects.requireNonNull(getActivity()), VERTICAL);
rvItems.addItemDecoration(decoration);
The decorator is a thin horizontal line that is shown between all items. How can I make the decorator a text, something like "↓ past items ↑ future items" and to be visible only between the relevant items?
Thanks
In such a case I think it is preferable to have a recycler view adapter that supports multiple types of items.
The separators that you call, will be sections
Example:
sealed class Item {
data class Section(val sectionText): Item
data class Purchases(.......): Item
}
Adapter:
class MyAdapter(private val items: List<Item>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
companion object {
const val TYPE_PURCHASES = 0
const val TYPE_SECTION = 1
}
override fun getItemViewType(position: Int): Int {
val type = when (items[position]) {
is Items.Section -> TYPE_SECTION
// other types...
else -> TYPE_PURCHASES
}
return type
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val viewHolder: RecyclerView.ViewHolder = when (viewType) {
TYPE_PURCHASES -> {}
// other view holders...
else -> {}
}
return viewHolder
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
when(holder) {
is SectionViewHolder -> bindSectionViewHolder(holder)
is PurchasesViewHolder -> bindSectionViewHolder(holder)
}
}
override fun getItemCount() = items.size
}
Then create the corresponding ViewHolders for each type and instantiate them at onCreateViewHolder.
And when you pass the list of Items in the adapter you should have logic that adds either a Section or an Item to the list.
Similar example from one of my projects:
.subscribeBy(onSuccess = {
val list = mutableListOf<IViewModel>()
val previousBookingDate = Calendar.getInstance()
val bookingDate = Calendar.getInstance()
it.data?.fold(Date(0)) { previousHeader, bookable ->
previousBookingDate.time = previousHeader
bookingDate.time = bookable.getStartDate()
val timeIgnoringComparator = DateUtils.TimeIgnoringComparator()
if (timeIgnoringComparator.compare(previousBookingDate, bookingDate) != 0) {
list.add(SectionViewModel(bookable.getStartDate(), isDefaultSearch || isDefaultCenterSelected()))
}
list.add(BookableItemViewModel(bookable))
bookable.getStartDate()
}
searchResultsLiveData.value = list
}, onError = {
isError.setValue(Event(Pair(null, Runnable { performSearch() })))
})
I suggest taking a look at this library to quickly make lists with different item types.

(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?) {
...
}

Android - how to implement multiple ViewHolder one for header layout another for model layout in Firestore RecyclerView adapter

I'm trying to make 2 view holder one will be at position 0 always which will be a header view for selecting image, another for displaying the model,
I can do it for normal recyclerview adapter but in firestore recycler adapter i can't do it,
what I try it increase the item count by 1 but I got
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
This is my adapter class code:
val TYPE_SELECT_IMAGE = 0
val TYPE_ITEMS = 1
class UsersImageAdapter(options: FirestoreRecyclerOptions<UserImage>)
: FirestoreRecyclerAdapter<UserImage, RecyclerView.ViewHolder>(options) , AnkoLogger{
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int, model: UserImage) {
when(holder){
is SelectImageViewHolder -> {
holder.bindModel()
}
is UserImageViewHolder -> {
holder.bindImages(model)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
TYPE_SELECT_IMAGE -> {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_add_image, parent, false)
SelectImageViewHolder(view)
}
TYPE_ITEMS -> {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_image,parent,false)
UserImageViewHolder(view)
}
else -> throw RuntimeException()
}
}
override fun getItemViewType(position: Int): Int {
return if (isPositionHeader(position)) {
TYPE_SELECT_IMAGE
} else {
TYPE_ITEMS
}
}
fun isPositionHeader(position: Int): Boolean {
return position == 0
}
override fun getItemCount(): Int {
return super.getItemCount() + 1
}
}
and this is two view holder one for header view another for displaying model
class SelectImageViewHolder(val view: View) : RecyclerView.ViewHolder(view) , AnkoLogger{
fun bindModel() {
view.setOnClickListener {
info {
"clicked !"
}
}
}
}
class UserImageViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
fun bindImages(userImage: UserImage) {
val imageView = view.findViewById<ImageView>(R.id.image_user_post)
Glide.with(view.context).load(userImage.imageUrl).into(imageView)
}
}
my goal is to keep the SelectImageViewHolder in position 0 always
FirebaseUI maintainer here. onBindViewHolder(RecyclerView.ViewHolder, Int, Model) makes a getItem(position) call internally to seamlessly populate your model. Since you're manually injecting an item, the RecyclerView is going to think there are items in the database and will blow up as you saw. To fix that, you'll want to override the native onBindViewHolder(RecyclerView.ViewHolder, Int) method without calling super:
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when(holder) {
is SelectImageViewHolder -> holder.bindModel()
is UserImageViewHolder -> {
// Manually get the model item
holder.bindImages(getItem(position - 1))
}
}
}

Categories

Resources