Como faço para os cards aparecem um me baixo do outro sem substituir o anterior? E também quando eu saio da tela ou do app os cards somem.
Esse código em composto pelo HomeFragment.kt:
class BudgetsFragment : Fragment() {
private lateinit var adapter: CardAdapter
private lateinit var recyclerView: RecyclerView
private var cardList: MutableList<CardItem> = mutableListOf()
private val retrofit = Retrofit.Builder()
.baseUrl("https://647fc34cf061e6ec4d485612.mockapi.io/")
.addConverterFactory(GsonConverterFactory.create())
.build()
private val apiService: CardService = retrofit.create(CardService::class.java)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_budgets, container, false)
recyclerView = rootView.findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(context)
val itemId = arguments?.getInt("itemId", -1) ?: -1
if (!::adapter.isInitialized) {
adapter = CardAdapter(cardList)
recyclerView.adapter = adapter
}
apiService.getCards().enqueue(object : Callback<List<CardItem>> {
override fun onResponse(call: Call<List<CardItem>>, response: Response<List<CardItem>>) {
if (response.isSuccessful) {
val cards = response.body() ?: emptyList()
val filteredList = cards.filter { it.id == itemId }
if (filteredList.isEmpty()) {
Toast.makeText(context, "Seu orçamento está vazio.", Toast.LENGTH_SHORT).show()
}
cardList.clear()
cardList.addAll(filteredList)
adapter.notifyDataSetChanged()
}
}
override fun onFailure(call: Call<List<CardItem>>, t: Throwable) {}
})
return rootView
}
}
Esse código em composto pelo CardAdapter.kt:
class CardAdapter(
private var cardList: List<CardItem>
) : RecyclerView.Adapter<CardAdapter.CardViewHolder>() {
class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textViewCardTitle: TextView = itemView.findViewById(R.id.textViewCardTitle)
val imageViewCardItem: ImageView = itemView.findViewById(R.id.imageViewCardItem)
val imageButtonDeleteCard: ImageButton = itemView.findViewById(R.id.imageButtonDeleteCard)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_card_test, parent, false)
return CardViewHolder(itemView)
}
override fun onBindViewHolder(holder: CardViewHolder, position: Int) {
val currentItem = cardList[position]
holder.textViewCardTitle.text = currentItem.nome
Picasso.get().load(currentItem.imagem).into(holder.imageViewCardItem)
holder.imageButtonDeleteCard.setOnClickListener {
removeCard(position)
}
}
private fun removeCard(position: Int) {
val updatedList = cardList.toMutableList()
updatedList.removeAt(position)
cardList = updatedList.toList()
notifyItemRemoved(position)
}
override fun getItemCount(): Int {
return cardList.size
}
}
Esse código em composto pelo DetailsFragment.kt que envia o card até BudgetsFragment.kt:
val buttonDetails = view.findViewById<AppCompatButton>(R.id.buttonDetails)
buttonDetails.setOnClickListener {
val bundle = Bundle()
bundle.putInt("itemId", detailsItem.id)
val navController = findNavController()
navController.navigate(R.id.nav_budgets, bundle)
}