Browse Source

add top and sub news

Dola TENG 1 year ago
parent
commit
392691e82d

+ 0 - 1
.idea/misc.xml

@@ -1,4 +1,3 @@
-<?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
   <component name="ExternalStorageConfigurationManager" enabled="true" />
   <component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">

+ 3 - 0
app/build.gradle

@@ -1,6 +1,7 @@
 plugins {
     id 'com.android.application'
     id 'org.jetbrains.kotlin.android'
+    id 'kotlin-parcelize'
 }
 
 android {
@@ -45,6 +46,8 @@ dependencies {
     implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'
     implementation 'androidx.navigation:navigation-fragment-ktx:2.6.0'
     implementation 'androidx.navigation:navigation-ui-ktx:2.6.0'
+    implementation 'androidx.recyclerview:recyclerview:1.3.1' // or androidx.recyclerview:recyclerview:1.2.0
+    implementation 'com.github.bumptech.glide:glide:4.12.0'
     testImplementation 'junit:junit:4.13.2'
     androidTestImplementation 'androidx.test.ext:junit:1.1.5'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

+ 4 - 0
app/src/main/java/com/khmer9/boxingapplication/adapter/CardItem.kt

@@ -0,0 +1,4 @@
+package com.khmer9.boxingapplication.adapter
+
+data class CardItem(val imageResource: Int, val date: String, val title: String)
+

+ 36 - 0
app/src/main/java/com/khmer9/boxingapplication/adapter/SubTopNewCardAdapter.kt

@@ -0,0 +1,36 @@
+package com.khmer9.boxingapplication.adapter
+
+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 com.khmer9.boxingapplication.R
+
+class SubTopNewCardAdapter(private val cardList: List<CardItem>) : RecyclerView.Adapter<SubTopNewCardAdapter.ViewHolder>() {
+
+    class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
+
+    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
+        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.layout_sub_item_card, parent, false)
+        return ViewHolder(itemView)
+    }
+
+    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
+        val currentItem = cardList[position]
+
+        holder.imageView.setImageResource(currentItem.imageResource)
+        holder.dateTextView.text = currentItem.date
+        holder.titleTextView.text = currentItem.title
+    }
+
+    override fun getItemCount() = cardList.size
+
+    // Holds the views for adding it to image and text
+    class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
+        val imageView: ImageView = itemView.findViewById(R.id.imageView)
+        val dateTextView: TextView = itemView.findViewById(R.id.dateTextView)
+        val titleTextView: TextView = itemView.findViewById(R.id.titleTextView)
+    }
+}

+ 38 - 0
app/src/main/java/com/khmer9/boxingapplication/adapter/TopNewCardAdapter.kt

@@ -0,0 +1,38 @@
+package com.khmer9.boxingapplication.adapter
+
+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 com.khmer9.boxingapplication.R
+
+class TopNewCardAdapter(private val cardList: List<CardItem>) : RecyclerView.Adapter<TopNewCardAdapter.ViewHolder>() {
+
+    class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
+
+    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
+        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.layout_item_card, parent, false)
+        return ViewHolder(itemView)
+    }
+
+    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
+        val currentItem = cardList[position]
+
+        holder.imageView.setImageResource(currentItem.imageResource)
+        holder.dateTextView.text = currentItem.date
+        holder.titleTextView.text = currentItem.title
+    }
+
+    override fun getItemCount() = cardList.size
+
+    // Holds the views for adding it to image and text
+    class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
+        val imageView: ImageView = itemView.findViewById(R.id.imageView)
+        val dateTextView: TextView = itemView.findViewById(R.id.dateTextView)
+        val titleTextView: TextView = itemView.findViewById(R.id.titleTextView)
+    }
+}
+
+

+ 39 - 6
app/src/main/java/com/khmer9/boxingapplication/ui/home/HomeFragment.kt

@@ -4,14 +4,18 @@ import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
-import android.widget.TextView
 import androidx.fragment.app.Fragment
 import androidx.lifecycle.ViewModelProvider
+import androidx.recyclerview.widget.LinearLayoutManager
+import com.khmer9.boxingapplication.R
+import com.khmer9.boxingapplication.adapter.TopNewCardAdapter
+import com.khmer9.boxingapplication.adapter.CardItem
 import com.khmer9.boxingapplication.databinding.FragmentHomeBinding
 
 class HomeFragment : Fragment() {
 
     private var _binding: FragmentHomeBinding? = null
+    private lateinit var homeViewModel: HomeViewModel
 
     // This property is only valid between onCreateView and
     // onDestroyView.
@@ -22,16 +26,14 @@ class HomeFragment : Fragment() {
         container: ViewGroup?,
         savedInstanceState: Bundle?
     ): View {
-        val homeViewModel =
+        homeViewModel =
             ViewModelProvider(this).get(HomeViewModel::class.java)
 
         _binding = FragmentHomeBinding.inflate(inflater, container, false)
         val root: View = binding.root
+        setTopNews()
+        setSubTopNews()
 
-        val textView: TextView = binding.textHome
-        homeViewModel.text.observe(viewLifecycleOwner) {
-            textView.text = it
-        }
         return root
     }
 
@@ -39,4 +41,35 @@ class HomeFragment : Fragment() {
         super.onDestroyView()
         _binding = null
     }
+
+    private fun setTopNews() {
+        val recyclerView = binding.recyclerViewTopNews
+        val cardList = createCardList() // Create your card data list here
+        val cardAdapter = TopNewCardAdapter(cardList)
+
+        recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
+        recyclerView.adapter = cardAdapter
+    }
+
+    private fun setSubTopNews() {
+        val recyclerView = binding.recyclerViewSubTopNews
+        val cardList = createCardList() // Create your card data list here
+        val cardAdapter = TopNewCardAdapter(cardList)
+
+        recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
+        recyclerView.adapter = cardAdapter
+    }
+
+    private fun createCardList(): List<CardItem> {
+        // Create and return your list of CardItems
+        return listOf(
+            CardItem(R.drawable.ic_launcher_background, "Date 1", "Title 1"),
+            CardItem(R.drawable.ic_launcher_background, "Date 2", "Title 2"),
+            CardItem(R.drawable.ic_launcher_background, "Date 3", "Title 3"),
+            CardItem(R.drawable.ic_launcher_background, "Date 4", "Title 4"),
+            CardItem(R.drawable.ic_launcher_background, "Date 5", "Title 5"),
+            // Add more card items as needed
+        )
+    }
+
 }

+ 13 - 13
app/src/main/res/layout/fragment_home.xml

@@ -1,22 +1,22 @@
 <?xml version="1.0" encoding="utf-8"?>
-<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<androidx.appcompat.widget.LinearLayoutCompat 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="match_parent"
+    android:orientation="vertical"
     tools:context=".ui.home.HomeFragment">
 
-    <TextView
-        android:id="@+id/text_home"
+    <androidx.recyclerview.widget.RecyclerView
+        android:id="@+id/recyclerViewTopNews"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:layout_marginStart="8dp"
-        android:layout_marginTop="8dp"
-        android:layout_marginEnd="8dp"
-        android:textAlignment="center"
-        android:textSize="20sp"
-        app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toTopOf="parent" />
-</androidx.constraintlayout.widget.ConstraintLayout>
+        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
+
+    <androidx.recyclerview.widget.RecyclerView
+        android:id="@+id/recyclerViewSubTopNews"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
+
+</androidx.appcompat.widget.LinearLayoutCompat>

+ 43 - 0
app/src/main/res/layout/layout_item_card.xml

@@ -0,0 +1,43 @@
+<androidx.cardview.widget.CardView
+    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="wrap_content"
+    android:layout_height="wrap_content"
+    app:cardCornerRadius="0dp"
+    app:cardElevation="4dp"
+    >
+
+    <LinearLayout
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:orientation="vertical"
+        >
+
+        <ImageView
+            android:id="@+id/imageView"
+            android:layout_width="330dp"
+            android:layout_height="200dp"
+            android:src="@drawable/ic_launcher_background"
+            android:scaleType="centerCrop"
+            />
+
+        <TextView
+            android:id="@+id/dateTextView"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="Your Date"
+            android:textSize="12sp"
+            />
+
+        <TextView
+            android:id="@+id/titleTextView"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="Your Title"
+            android:textSize="16sp"
+            android:textStyle="bold"
+            />
+
+    </LinearLayout>
+</androidx.cardview.widget.CardView>

+ 43 - 0
app/src/main/res/layout/layout_sub_item_card.xml

@@ -0,0 +1,43 @@
+<androidx.cardview.widget.CardView
+    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="wrap_content"
+    android:layout_height="wrap_content"
+    app:cardCornerRadius="0dp"
+    app:cardElevation="4dp"
+    >
+
+    <LinearLayout
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:orientation="vertical"
+        >
+
+        <ImageView
+            android:id="@+id/imageView"
+            android:layout_width="180dp"
+            android:layout_height="180dp"
+            android:src="@drawable/ic_launcher_background"
+            android:scaleType="centerCrop"
+            />
+
+        <TextView
+            android:id="@+id/dateTextView"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="Your Date"
+            android:textSize="12sp"
+            />
+
+        <TextView
+            android:id="@+id/titleTextView"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="Your Title"
+            android:textSize="16sp"
+            android:textStyle="bold"
+            />
+
+    </LinearLayout>
+</androidx.cardview.widget.CardView>