|
@@ -5,12 +5,17 @@ import androidx.fragment.app.Fragment
|
|
|
import android.view.LayoutInflater
|
|
|
import android.view.View
|
|
|
import android.view.ViewGroup
|
|
|
+import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
+import com.google.android.material.tabs.TabLayout
|
|
|
import com.sambath.sbc.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"
|
|
|
+import com.sambath.sbc.adapter.HighlightNewsAdapter
|
|
|
+import com.sambath.sbc.adapter.TopNewsAdapter
|
|
|
+import com.sambath.sbc.adapter.TopNewsCardItem
|
|
|
+import com.sambath.sbc.adapter.TopNewsHeaderAdapter
|
|
|
+import kotlinx.android.synthetic.main.fragment_news.view.recyclerViewNews
|
|
|
+import kotlinx.android.synthetic.main.fragment_news.view.recyclerViewNewsHeader
|
|
|
+import kotlinx.android.synthetic.main.fragment_news.view.selectedTabTextView
|
|
|
+import kotlinx.android.synthetic.main.fragment_news.view.tabLayout
|
|
|
|
|
|
/**
|
|
|
* A simple [Fragment] subclass.
|
|
@@ -18,16 +23,11 @@ private const val ARG_PARAM2 = "param2"
|
|
|
* create an instance of this fragment.
|
|
|
*/
|
|
|
class NewsFragment : Fragment() {
|
|
|
- // TODO: Rename and change types of parameters
|
|
|
- private var param1: String? = null
|
|
|
- private var param2: String? = null
|
|
|
+ private var _root: View? = null
|
|
|
+ private val binding get() = _root!!
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
super.onCreate(savedInstanceState)
|
|
|
- arguments?.let {
|
|
|
- param1 = it.getString(ARG_PARAM1)
|
|
|
- param2 = it.getString(ARG_PARAM2)
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
override fun onCreateView(
|
|
@@ -35,26 +35,105 @@ class NewsFragment : Fragment() {
|
|
|
savedInstanceState: Bundle?
|
|
|
): View? {
|
|
|
// Inflate the layout for this fragment
|
|
|
- return inflater.inflate(R.layout.fragment_news, container, false)
|
|
|
- }
|
|
|
+ _root = inflater.inflate(R.layout.fragment_news, container, false)
|
|
|
+
|
|
|
+ val tabTitles = listOf("TOP NEWS", "POPULAR", "LAST", "HIGHLIGHT")
|
|
|
+
|
|
|
+ // Populate TabLayout with dynamic tabs
|
|
|
+ for (title in tabTitles) {
|
|
|
+ val tab = binding.tabLayout.newTab().setText(title)
|
|
|
+ binding.tabLayout.addTab(tab)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Set tab click listener
|
|
|
+ binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
|
|
|
+ override fun onTabSelected(tab: TabLayout.Tab?) {
|
|
|
+ tab?.let {
|
|
|
+ val selectedTitle = it.text.toString()
|
|
|
+ binding.selectedTabTextView.text = "Selected Tab: $selectedTitle"
|
|
|
+ val selectedIndex = it.position
|
|
|
+ when (selectedIndex) {
|
|
|
+ 0 -> {
|
|
|
+ //Top News
|
|
|
+ setNewsHeaderFeed()
|
|
|
+ setNewsFeed()
|
|
|
+ }
|
|
|
|
|
|
- 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 NewsFragment.
|
|
|
- */
|
|
|
- // TODO: Rename and change types and number of parameters
|
|
|
- @JvmStatic
|
|
|
- fun newInstance(param1: String, param2: String) =
|
|
|
- NewsFragment().apply {
|
|
|
- arguments = Bundle().apply {
|
|
|
- putString(ARG_PARAM1, param1)
|
|
|
- putString(ARG_PARAM2, param2)
|
|
|
+ 1 -> {
|
|
|
+ // Popular
|
|
|
+ }
|
|
|
+
|
|
|
+ 2 -> {
|
|
|
+ // Last
|
|
|
+ }
|
|
|
+
|
|
|
+ 3 -> {
|
|
|
+ // Highlight
|
|
|
+ removeNewsHeaderFeed()
|
|
|
+ setHighlight()
|
|
|
+ }
|
|
|
+ // Add more cases if needed
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ override fun onTabUnselected(tab: TabLayout.Tab?) {
|
|
|
+ // No action needed
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onTabReselected(tab: TabLayout.Tab?) {
|
|
|
+ // No action needed
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ return binding
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun setNewsHeaderFeed() {
|
|
|
+ val recyclerView = binding.recyclerViewNewsHeader
|
|
|
+ val cardList = createCardList() // Create your card data list here
|
|
|
+ val cardAdapter = TopNewsHeaderAdapter(cardList)
|
|
|
+
|
|
|
+ recyclerView.layoutManager =
|
|
|
+ LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
|
|
|
+ recyclerView.adapter = cardAdapter
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun removeNewsHeaderFeed() {
|
|
|
+ val recyclerView = binding.recyclerViewNewsHeader
|
|
|
+ recyclerView.adapter = null
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private fun setNewsFeed() {
|
|
|
+ val recyclerView = binding.recyclerViewNews
|
|
|
+ val cardList = createCardList() // Create your card data list here
|
|
|
+ val cardAdapter = TopNewsAdapter(cardList)
|
|
|
+
|
|
|
+ recyclerView.layoutManager =
|
|
|
+ LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
|
|
|
+ recyclerView.adapter = cardAdapter
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun setHighlight() {
|
|
|
+ val recyclerView = binding.recyclerViewNews
|
|
|
+ val cardList = createCardList() // Create your card data list here
|
|
|
+ val cardAdapter = HighlightNewsAdapter(cardList)
|
|
|
+
|
|
|
+ recyclerView.layoutManager =
|
|
|
+ LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
|
|
|
+ recyclerView.adapter = cardAdapter
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun createCardList(): List<TopNewsCardItem> {
|
|
|
+ // Create and return your list of CardItems
|
|
|
+ return listOf(
|
|
|
+ TopNewsCardItem(R.drawable.ic_bg_dashboard, "Date 1", "Title 1"),
|
|
|
+ TopNewsCardItem(R.drawable.ic_bg_dashboard, "Date 2", "Title 2"),
|
|
|
+ TopNewsCardItem(R.drawable.ic_bg_dashboard, "Date 3", "Title 3"),
|
|
|
+ TopNewsCardItem(R.drawable.ic_bg_dashboard, "Date 4", "Title 4"),
|
|
|
+ TopNewsCardItem(R.drawable.ic_bg_dashboard, "Date 5", "Title 5"),
|
|
|
+ // Add more card items as needed
|
|
|
+ )
|
|
|
}
|
|
|
}
|