Commit d95f8def authored by Timothé KOBAK's avatar Timothé KOBAK

grid view implemented

parent b2bc3455
......@@ -5,7 +5,9 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.GridView
import android.widget.ListView
import android.widget.ToggleButton
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.example.elbuenopeso.databinding.FragmentMarketBinding
......@@ -15,25 +17,39 @@ import com.example.elbuenopeso.R
class MarketFragment : Fragment() {
private var _binding: FragmentMarketBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
private lateinit var listView: ListView
private lateinit var gridView: GridView
private lateinit var toggleButton: ToggleButton
private lateinit var adAdapter: AdAdapter
private var isGridViewEnabled = false
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val marketViewModel =
ViewModelProvider(this)[MarketViewModel::class.java]
_binding = FragmentMarketBinding.inflate(inflater, container, false)
val root: View = binding.root
val listView: ListView = binding.listView
// seed market items
var annonces: List<AdModel> = listOf<AdModel>(
listView = binding.listView
gridView = binding.gridView
toggleButton = binding.toggleButton
val annonces: List<AdModel> = listOf(
AdModel("Poutre", "1 rue Jean-Pierre", R.drawable.pichu, 25.0),
AdModel("Briques", "2 rue Jean-Michel", R.drawable.pichu, 16.0),
AdModel("Poutre", "1 rue Jean-Pierre", R.drawable.pichu, 25.0),
AdModel("Briques", "2 rue Jean-Michel", R.drawable.pichu, 16.0),
AdModel("Mur en bois", "4 rue Alain-Juju", R.drawable.pichu, 7.2),
AdModel("Maison de pierre", "7 rue Joris Belhomme", R.drawable.pichu, 3.99),
AdModel("Téléphone de Timothé", "8 rue de Timothé", R.drawable.pichu, 0.85),
AdModel("Oridnateur", "10 rue Jean-Charles", R.drawable.pichu, 104.98),
AdModel("Charnières", "22 rue Jeanne-Marie", R.drawable.pichu, 2648.97),
AdModel("Porte en marbre", "1 rue Jean-Pierre", R.drawable.pichu, 480.0),
AdModel("Cheminée", "33 rue du Gouvernement", R.drawable.pichu, 800.0),
AdModel("Poutre", "1 rue Jean-Pierre", R.drawable.pichu, 25.0),
AdModel("Briques", "2 rue Jean-Michel", R.drawable.pichu, 16.0),
AdModel("Mur en bois", "4 rue Alain-Juju", R.drawable.pichu, 7.2),
......@@ -43,17 +59,32 @@ class MarketFragment : Fragment() {
AdModel("Charnières", "22 rue Jeanne-Marie", R.drawable.pichu, 2648.97),
AdModel("Porte en marbre", "1 rue Jean-Pierre", R.drawable.pichu, 480.0),
AdModel("Cheminée", "33 rue du Gouvernement", R.drawable.pichu, 800.0),
// Add more AdModel items as needed
)
var adAdapter: AdAdapter = AdAdapter(requireContext(), annonces)
adAdapter = AdAdapter(requireContext(), annonces)
listView.adapter = adAdapter
/*marketViewModel.text.observe(viewLifecycleOwner) {
textView.text = it
}*/
toggleButton.setOnCheckedChangeListener { _, isChecked ->
isGridViewEnabled = isChecked
updateView()
}
return root
}
private fun updateView() {
if (isGridViewEnabled) {
listView.visibility = View.GONE
gridView.visibility = View.VISIBLE
gridView.adapter = adAdapter
} else {
gridView.visibility = View.GONE
listView.visibility = View.VISIBLE
listView.adapter = adAdapter
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
......
......@@ -6,12 +6,37 @@
android:layout_height="match_parent"
tools:context=".ui.market.MarketFragment">
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toggle View"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/listView"
android:layout_width="409dp"
android:layout_height="681dp"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toBottomOf="@id/toggleButton" />
<GridView
android:id="@+id/gridView"
android:layout_width="0dp"
android:layout_height="0dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:gravity="center"
android:horizontalSpacing="8dp"
android:verticalSpacing="8dp"
android:padding="8dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toggleButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?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"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/itemListViewTitleView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20sp"
app:layout_constraintStart_toEndOf="@+id/itemListViewImageView"
app:layout_constraintTop_toTopOf="parent" />
android:orientation="vertical"
android:padding="8dp">
<ImageView
android:id="@+id/itemListViewImageView"
android:layout_width="117dp"
android:layout_height="94dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@drawable/pichu" />
android:layout_gravity="center_horizontal"
android:src="@drawable/pichu" />
<TextView
android:id="@+id/itemListViewTextView"
android:layout_width="wrap_content"
android:id="@+id/itemListViewTitleView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="@+id/itemListViewImageView"
app:layout_constraintStart_toEndOf="@+id/itemListViewImageView" />
android:textSize="20sp"
android:textStyle="bold"
android:maxLines="2"
android:ellipsize="end"
android:paddingTop="8dp"
android:paddingBottom="4dp" />
<TextView
android:id="@+id/itemListViewPrixView"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:paddingBottom="4dp" />
<TextView
android:id="@+id/itemListViewTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/itemListViewTextView"
app:layout_constraintStart_toEndOf="@+id/itemListViewImageView"
app:layout_constraintTop_toBottomOf="@+id/itemListViewTitleView" />
android:textSize="14sp"
android:maxLines="3"
android:ellipsize="end" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
</LinearLayout>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment