added a LayoutManager to fragment_shoppinglist_shoplist.xml

added a ShoppinglistShoplistAdapter.kt for the shoplist RecyclerView
added a data class for the ShoppinglistShoplistAdapter.kt
modified the layout of the fragment_shoppinglist_shoplist_item.xml
refactored the ShoplistFragment.kt
added samplecode for the RecyclerView to the ShoplistFragment.kt
This commit is contained in:
2024-06-02 16:55:41 +02:00
parent a355301d68
commit 9e54bbc978
5 changed files with 70 additions and 9 deletions

View File

@@ -0,0 +1,43 @@
package de.chrissthecoder.store.adapter
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import de.chrissthecoder.store.R
import de.chrissthecoder.store.dataclass.ShoppinglistShoplistItem
class ShoppinglistShoplistAdapter(val shoplist: List<ShoppinglistShoplistItem>) : RecyclerView.Adapter<ShoppinglistShoplistAdapter.ItemViewHolder>() {
inner class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val shoplabel: TextView
val shopicon: ImageView
val shoppinglistCount: TextView
init {
Log.d(this.javaClass.name,"Get necessary Views")
shoplabel = view.findViewById(R.id.shoplabel)
shopicon = view.findViewById(R.id.shopicon)
shoppinglistCount = view.findViewById(R.id.shoppinglistCount)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
Log.d(this.javaClass.name, "Create new views (invoked by the layout manager)")
val view = LayoutInflater.from(parent.context).inflate(R.layout.fragment_shoppinglist_shoplist_item, parent, false)
return ItemViewHolder(view)
}
override fun onBindViewHolder(holder: ShoppinglistShoplistAdapter.ItemViewHolder, position: Int) {
Log.d(this.javaClass.name,"Replace the contents of a view (invoked by the layout manager)")
holder.shoplabel.text = shoplist[position].shoplabel
holder.shoppinglistCount.text = "(" + shoplist[position].shoppinglistCount + ")"
}
override fun getItemCount(): Int {
Log.d(this.javaClass.name, "Return the size of your dataset (invoked by the layout manager)")
return shoplist.size
}
}

View File

@@ -0,0 +1,3 @@
package de.chrissthecoder.store.dataclass
data class ShoppinglistShoplistItem(val shoplabel: String, val iconID: Int, val shoppinglistCount: Int)

View File

@@ -5,18 +5,29 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import de.chrissthecoder.store.R
import de.chrissthecoder.store.adapter.ShoppinglistShoplistAdapter
import de.chrissthecoder.store.databinding.FragmentShoppinglistShoplistBinding
import de.chrissthecoder.store.dataclass.ShoppinglistShoplistItem
/**
* A [Fragment] subclass to list all shops that contains assigned shoppinglists.
*/
class ShoplistFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_shoppinglist_shoplist, container, false)
private var _binding: FragmentShoppinglistShoplistBinding? = null
private val binding get() = _binding!!
private lateinit var adapter: ShoppinglistShoplistAdapter
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentShoppinglistShoplistBinding.inflate(inflater, container, false)
val shoplist = ArrayList<ShoppinglistShoplistItem>()
val shoplistRecyclerView = binding.shoplist
adapter = ShoppinglistShoplistAdapter(shoplist)
shoplistRecyclerView.adapter = adapter
return binding.root
}
}

View File

@@ -14,5 +14,6 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -3,11 +3,14 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
app:cardBackgroundColor="@color/cardview_background"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent"