Good morning, I hope you understand the question I need to show a CardView after executing an action in another CardView , for example: the functionality I need is that when I click the show button located in CardView 1 (position 0) it shows me the C ardView 2 (position 1) how could I do something like that? I was trying to validate by position, also using for or while but I did not get a result, without further ado I will show what I have for the moment.
From input it is valid that it only shows the CardView that is in position 0, because I use a database, in reality I validate it by the id but let's take it as if it were position 0 of the RecyclerView so as not to think too much about it:
if (CierreCaja.lIdTipoPago != 1){
cierreCajaViewHolder.cvCierre.setVisibility(View.INVISIBLE);
}
When I click the show button it should show the following CardView but I don't know how to validate to access that position dynamically
cierreCajaViewHolder.aceptar.setOnClickListener(view -> {
cierreCajaViewHolder.cvCierre.setVisibility(View.VISIBLE);
}
cvCierre is the CardView and at a glance this does not work for me because although I am assigning the CardView to be shown at no time am I saying which CardView to show
I also tried to show the CardView by assigned position that shows the current position + 1 but it doesn't work either, which is logical because setVisibility
a position cannot be passed as a parameter, however that is what I am looking for to show me the following CardView :
cierreCajaViewHolder.cvCierre.setVisibility(position+1);
And that would be all I hope my question has been understood and I hope someone can help me, thank you very much in advance!
To do what you want it can be very simple and the best option is to create an adapter where you can add new
CardView
, you must detect the position of the element you click on using:by getting the position you can tell the Adapter to add the next item, adding 1 to get the next index and notify the Adapter which item to add using notifyItemInserted() :
In this way, by clicking on an element you can get the next one to be inserted.
Check out the full example:
https://github.com/Jorgesys/Adapter-Add-Remove-Elements