Explorar o código

add top and sub news newfeeds

Dola TENG hai 1 ano
pai
achega
7e1a7e236b

+ 36 - 0
app/src/main/java/com/khmer9/boxingapplication/adapter/NewsFeedCardAdapter.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 NewsFeedCardAdapter(private val cardList: List<CardItem>) : RecyclerView.Adapter<NewsFeedCardAdapter.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_news_feed_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)
+    }
+}

+ 1 - 1
app/src/main/java/com/khmer9/boxingapplication/adapter/SubTopNewCardAdapter.kt → app/src/main/java/com/khmer9/boxingapplication/adapter/SubTopNewsCardAdapter.kt

@@ -8,7 +8,7 @@ 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 SubTopNewsCardAdapter(private val cardList: List<CardItem>) : RecyclerView.Adapter<SubTopNewsCardAdapter.ViewHolder>() {
 
     class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
 

+ 1 - 1
app/src/main/java/com/khmer9/boxingapplication/adapter/TopNewCardAdapter.kt → app/src/main/java/com/khmer9/boxingapplication/adapter/TopNewsCardAdapter.kt

@@ -8,7 +8,7 @@ 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 TopNewsCardAdapter(private val cardList: List<CardItem>) : RecyclerView.Adapter<TopNewsCardAdapter.ViewHolder>() {
 
     class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
 

+ 15 - 3
app/src/main/java/com/khmer9/boxingapplication/ui/home/HomeFragment.kt

@@ -8,8 +8,10 @@ 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.TopNewsCardAdapter
 import com.khmer9.boxingapplication.adapter.CardItem
+import com.khmer9.boxingapplication.adapter.NewsFeedCardAdapter
+import com.khmer9.boxingapplication.adapter.SubTopNewsCardAdapter
 import com.khmer9.boxingapplication.databinding.FragmentHomeBinding
 
 class HomeFragment : Fragment() {
@@ -33,6 +35,7 @@ class HomeFragment : Fragment() {
         val root: View = binding.root
         setTopNews()
         setSubTopNews()
+        setNewsFeed()
 
         return root
     }
@@ -45,7 +48,7 @@ class HomeFragment : Fragment() {
     private fun setTopNews() {
         val recyclerView = binding.recyclerViewTopNews
         val cardList = createCardList() // Create your card data list here
-        val cardAdapter = TopNewCardAdapter(cardList)
+        val cardAdapter = TopNewsCardAdapter(cardList)
 
         recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
         recyclerView.adapter = cardAdapter
@@ -54,12 +57,21 @@ class HomeFragment : Fragment() {
     private fun setSubTopNews() {
         val recyclerView = binding.recyclerViewSubTopNews
         val cardList = createCardList() // Create your card data list here
-        val cardAdapter = TopNewCardAdapter(cardList)
+        val cardAdapter = SubTopNewsCardAdapter(cardList)
 
         recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
         recyclerView.adapter = cardAdapter
     }
 
+    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.adapter = cardAdapter
+    }
+
     private fun createCardList(): List<CardItem> {
         // Create and return your list of CardItems
         return listOf(

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

@@ -19,4 +19,10 @@
         android:layout_height="wrap_content"
         app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
 
+    <androidx.recyclerview.widget.RecyclerView
+        android:id="@+id/recyclerViewNewsFeed"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
+
 </androidx.appcompat.widget.LinearLayoutCompat>

+ 52 - 0
app/src/main/res/layout/layout_news_feed_item_card.xml

@@ -0,0 +1,52 @@
+<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="horizontal">
+
+        <ImageView
+            android:id="@+id/imageView"
+            android:layout_width="150dp"
+            android:layout_height="120dp"
+            android:scaleType="centerCrop"
+            android:src="@drawable/ic_launcher_background" />
+
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:paddingLeft="5dp"
+            android:orientation="vertical">
+
+            <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" />
+
+            <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/descTextView"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:text="Description......................................................"
+                android:textSize="12sp" />
+
+        </LinearLayout>
+
+    </LinearLayout>
+</androidx.cardview.widget.CardView>