Explorar o código

EventFragment

Dola TENG hai 1 ano
pai
achega
3a1f2fb915

+ 35 - 0
app/src/main/java/com/khmer9/boxingapplication/adapter/EventAdapter.kt

@@ -0,0 +1,35 @@
+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 EventAdapter (private val cardList: List<CardItem>) : RecyclerView.Adapter<EventAdapter.ViewHolder>() {
+    class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
+
+    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EventAdapter.ViewHolder {
+        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.layout_event_card, parent, false)
+        return EventAdapter.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)
+    }
+}

+ 43 - 39
app/src/main/java/com/khmer9/boxingapplication/ui/event/EventFragment.kt

@@ -5,56 +5,60 @@ import androidx.fragment.app.Fragment
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import androidx.recyclerview.widget.LinearLayoutManager
 import com.khmer9.boxingapplication.R
+import com.khmer9.boxingapplication.adapter.CardItem
+import com.khmer9.boxingapplication.adapter.EventAdapter
+import com.khmer9.boxingapplication.adapter.TopNewsCardAdapter
+import com.khmer9.boxingapplication.databinding.FragmentEventBinding
+import com.khmer9.boxingapplication.databinding.FragmentHomeBinding
 
-// TODO: Rename parameter arguments, choose names that match
-// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
-private const val ARG_PARAM1 = "param1"
-private const val ARG_PARAM2 = "param2"
-
-/**
- * A simple [Fragment] subclass.
- * Use the [EventFragment.newInstance] factory method to
- * create an instance of this fragment.
- */
 class EventFragment : Fragment() {
-    // TODO: Rename and change types of parameters
-    private var param1: String? = null
-    private var param2: String? = null
+    private var _binding: FragmentEventBinding? = null
+
+    // This property is only valid between onCreateView and
+    // onDestroyView.
+    private val binding get() = _binding!!
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
-        arguments?.let {
-            param1 = it.getString(ARG_PARAM1)
-            param2 = it.getString(ARG_PARAM2)
-        }
     }
 
     override fun onCreateView(
-        inflater: LayoutInflater, container: ViewGroup?,
-        savedInstanceState: Bundle?
+        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
     ): View? {
-        // Inflate the layout for this fragment
-        return inflater.inflate(R.layout.fragment_event, container, false)
+        _binding = FragmentEventBinding.inflate(inflater, container, false)
+        val root: View = binding.root
+
+        setEvent()
+
+        return root
+    }
+
+    override fun onDestroyView() {
+        super.onDestroyView()
+        _binding = null
+    }
+
+    private fun setEvent() {
+        val recyclerView = binding.recyclerViewEvent
+        val cardList = createCardList() // Create your card data list here
+        val cardAdapter = EventAdapter(cardList)
+
+        recyclerView.layoutManager =
+            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
+        recyclerView.adapter = cardAdapter
     }
 
-    companion object {
-        /**
-         * Use this factory method to create a new instance of
-         * this fragment using the provided parameters.
-         *
-         * @param param1 Parameter 1.
-         * @param param2 Parameter 2.
-         * @return A new instance of fragment EventFragment.
-         */
-        // TODO: Rename and change types and number of parameters
-        @JvmStatic
-        fun newInstance(param1: String, param2: String) =
-            EventFragment().apply {
-                arguments = Bundle().apply {
-                    putString(ARG_PARAM1, param1)
-                    putString(ARG_PARAM2, param2)
-                }
-            }
+    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
+        )
     }
 }

+ 7 - 4
app/src/main/java/com/khmer9/boxingapplication/ui/home/HomeFragment.kt

@@ -50,7 +50,8 @@ class HomeFragment : Fragment() {
         val cardList = createCardList() // Create your card data list here
         val cardAdapter = TopNewsCardAdapter(cardList)
 
-        recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
+        recyclerView.layoutManager =
+            LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
         recyclerView.adapter = cardAdapter
     }
 
@@ -59,16 +60,18 @@ class HomeFragment : Fragment() {
         val cardList = createCardList() // Create your card data list here
         val cardAdapter = SubTopNewsCardAdapter(cardList)
 
-        recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
+        recyclerView.layoutManager =
+            LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
         recyclerView.adapter = cardAdapter
     }
 
-    private fun setNewsFeed(){
+    private fun setNewsFeed() {
         val recyclerView = binding.recyclerViewNewsFeed
         val cardList = createCardList() // Create your card data list here
         val cardAdapter = NewsFeedCardAdapter(cardList)
 
-        recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
+        recyclerView.layoutManager =
+            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
         recyclerView.adapter = cardAdapter
     }
 

+ 5 - 4
app/src/main/res/layout/fragment_event.xml

@@ -3,12 +3,13 @@
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
     tools:context=".ui.event.EventFragment">
 
-    <!-- TODO: Update blank fragment layout -->
-    <TextView
+    <androidx.recyclerview.widget.RecyclerView
+        android:id="@+id/recyclerViewEvent"
         android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:text="@string/hello_blank_fragment" />
+        android:layout_height="wrap_content"
+        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
 
 </FrameLayout>

+ 7 - 0
app/src/main/res/layout/fragment_home.xml

@@ -19,6 +19,13 @@
         android:layout_height="wrap_content"
         app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
 
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:padding="5dp"
+        android:text="News Feed"
+        android:textSize="12sp" />
+
     <androidx.recyclerview.widget.RecyclerView
         android:id="@+id/recyclerViewNewsFeed"
         android:layout_width="match_parent"

+ 45 - 0
app/src/main/res/layout/layout_event_card.xml

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

+ 4 - 0
app/src/main/res/values/strings.xml

@@ -11,6 +11,10 @@
     <string name="menu_gallery">Gallery</string>
     <string name="menu_slideshow">Slideshow</string>
     <string name="menu_event">Event</string>
+    <string name="menu_boxing">Boxing</string>
+    <string name="menu_video">Video</string>
+    <string name="menu_notification">Notifications</string>
+
     <!-- TODO: Remove or change this placeholder text -->
     <string name="hello_blank_fragment">Hello blank fragment</string>
 </resources>