Dola TENG 1 yıl önce
ebeveyn
işleme
874f473fea

+ 18 - 3
app/src/main/java/com/sambath/kunkhmer/adapter/LivesAdapter.kt

@@ -5,15 +5,21 @@ import android.view.View
 import android.view.ViewGroup
 import android.widget.ImageView
 import android.widget.TextView
+import androidx.appcompat.app.AppCompatActivity
 import androidx.recyclerview.widget.RecyclerView
 import com.sambath.kunkhmer.R
+import com.sambath.kunkhmer.screen.lives.LiveDetailFragment
 import com.squareup.picasso.Picasso
 
-class LivesAdapter(private val cardList: List<LivesCardItem>) : RecyclerView.Adapter<LivesAdapter.ViewHolder>() {
+class LivesAdapter(private val cardList: List<LivesCardItem>, private val listener: OnItemClickListener) : RecyclerView.Adapter<LivesAdapter.ViewHolder>() {
+
+    interface OnItemClickListener {
+        fun onItemClick(position: Int)
+    }
 
     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LivesAdapter.ViewHolder {
         val itemView = LayoutInflater.from(parent.context).inflate(R.layout.layout_lives_item_card, parent, false)
-        return LivesAdapter.ViewHolder(itemView)
+        return ViewHolder(itemView)
     }
 
     override fun onBindViewHolder(holder: LivesAdapter.ViewHolder, position: Int) {
@@ -26,9 +32,18 @@ class LivesAdapter(private val cardList: List<LivesCardItem>) : RecyclerView.Ada
 
     override fun getItemCount() = cardList.size
 
-    class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
+    inner class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
         val imageView: ImageView = itemView.findViewById(R.id.imageView)
         val subTitleTextView: TextView = itemView.findViewById(R.id.subTitleTextView)
         val titleTextView: TextView = itemView.findViewById(R.id.titleTextView)
+
+        init {
+            itemView.setOnClickListener {
+                val position = adapterPosition
+                if (position != RecyclerView.NO_POSITION) {
+                    listener.onItemClick(position)
+                }
+            }
+        }
     }
 }

+ 60 - 0
app/src/main/java/com/sambath/kunkhmer/screen/lives/LiveDetailFragment.kt

@@ -0,0 +1,60 @@
+package com.sambath.kunkhmer.screen.lives
+
+import android.os.Bundle
+import androidx.fragment.app.Fragment
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import com.sambath.kunkhmer.R
+
+// 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 [LiveDetailFragment.newInstance] factory method to
+ * create an instance of this fragment.
+ */
+class LiveDetailFragment : Fragment() {
+    // TODO: Rename and change types of parameters
+    private var param1: String? = null
+    private var param2: String? = null
+
+    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?
+    ): View? {
+        // Inflate the layout for this fragment
+        return inflater.inflate(R.layout.fragment_live_detail, container, false)
+    }
+
+    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 LiveDetailFragment.
+         */
+        // TODO: Rename and change types and number of parameters
+        @JvmStatic
+        fun newInstance(param1: String, param2: String) =
+            LiveDetailFragment().apply {
+                arguments = Bundle().apply {
+                    putString(ARG_PARAM1, param1)
+                    putString(ARG_PARAM2, param2)
+                }
+            }
+    }
+}

+ 25 - 3
app/src/main/java/com/sambath/kunkhmer/screen/lives/LivesFragment.kt

@@ -5,16 +5,20 @@ import androidx.fragment.app.Fragment
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import android.widget.Toast
+import androidx.appcompat.app.AppCompatActivity
 import androidx.recyclerview.widget.LinearLayoutManager
+import androidx.recyclerview.widget.RecyclerView
 import com.sambath.kunkhmer.R
 import com.sambath.kunkhmer.adapter.LivesAdapter
 import com.sambath.kunkhmer.adapter.LivesCardItem
 import kotlinx.android.synthetic.main.fragment_event.view.recyclerViewEvent
 import kotlinx.android.synthetic.main.fragment_lives.view.recyclerViewLives
 
-class LivesFragment : Fragment() {
+class LivesFragment : Fragment(), LivesAdapter.OnItemClickListener  {
     private var _root: View? = null
     private val binding get() = _root!!
+    private var cardList: List<LivesCardItem>? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -34,8 +38,8 @@ class LivesFragment : Fragment() {
 
     private fun setEvent() {
         val recyclerView = binding.recyclerViewLives
-        val cardList = createCardList() // Create your card data list here
-        val cardAdapter = LivesAdapter(cardList)
+        cardList = createCardList() // Create your card data list here
+        val cardAdapter = LivesAdapter(cardList!!, this)
 
         recyclerView.layoutManager =
             LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
@@ -58,4 +62,22 @@ class LivesFragment : Fragment() {
             // Add more card items as needed
         )
     }
+
+    override fun onItemClick(position: Int) {
+        val clickedItem = cardList!![position]
+        val liveDetailFragment = LiveDetailFragment()
+
+        // You can pass data to the LiveDetailFragment using arguments if needed
+        val bundle = Bundle()
+        bundle.putString("image_url", clickedItem.imageResource)
+        bundle.putString("title", clickedItem.title)
+        bundle.putString("description", clickedItem.title)
+        liveDetailFragment.arguments = bundle
+
+        // Replace the current fragment with the LiveDetailFragment
+        parentFragmentManager.beginTransaction()
+            .replace(R.id.nav_host_fragment_activity_main, liveDetailFragment)
+            .addToBackStack(null) // Add to back stack to handle the back navigation
+            .commit()
+    }
 }

+ 14 - 0
app/src/main/res/layout/fragment_live_detail.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".screen.lives.LiveDetailFragment">
+
+    <!-- TODO: Update blank fragment layout -->
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:text="@string/hello_blank_fragment" />
+
+</FrameLayout>