Compare commits

...

2 Commits

Author SHA1 Message Date
9e54bbc978 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
2024-06-02 16:55:41 +02:00
a355301d68 added a RecyclerView to 'fragmnet_shoppinglist_shoplist'
added 'fragment_shoppinglist_shoplist_item'
added some colors to the colors.xml for the shoplist-item fragment
2024-06-02 14:18:15 +02:00
6 changed files with 141 additions and 14 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.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup 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. * A [Fragment] subclass to list all shops that contains assigned shoppinglists.
*/ */
class ShoplistFragment : Fragment() { class ShoplistFragment : Fragment() {
override fun onCreateView( private var _binding: FragmentShoppinglistShoplistBinding? = null
inflater: LayoutInflater, container: ViewGroup?, private val binding get() = _binding!!
savedInstanceState: Bundle?
): View? { private lateinit var adapter: ShoppinglistShoplistAdapter
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_shoppinglist_shoplist, container, false) 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

@@ -6,14 +6,14 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".ui.dashboard.tabfragment.shoppinglist.ShoplistFragment"> tools:context=".ui.dashboard.tabfragment.shoppinglist.ShoplistFragment">
<TextView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/shoplist"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:text="Einkaufszettel"
android:textAlignment="center"
android:textSize="32sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
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:layout_constraintVertical_bias="0.5" /> app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="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"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/shoplabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:textAlignment="center"
android:textColor="@color/cardview_textcolor"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
<ImageView
android:id="@+id/shopicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/shoppinglistCount"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5"
app:srcCompat="@android:drawable/toast_frame" />
<TextView
android:id="@+id/shoppinglistCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:textAlignment="center"
android:textColor="@color/cardview_textcolor"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -7,4 +7,7 @@
<color name="teal_700">#FF018786</color> <color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color> <color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color> <color name="white">#FFFFFFFF</color>
<color name="cardview_background">#444444</color>
<color name="cardview_textcolor">#FFFFFF</color>
</resources> </resources>