Archived
Switch to ViewPager2 was successful; it was essentially a drop-in replacement. There's a bug in the connected TabLayout where, if the app starts and scrolls programmatically to Wednesday or later, the tab won't be focussed until the user starts scrolling again
This commit is contained in:
@@ -7,7 +7,7 @@ import androidx.recyclerview.widget.RecyclerView
|
||||
import com.denizk0461.studip.model.StudIPEvent
|
||||
import com.denizk0461.studip.databinding.ItemEventPageBinding
|
||||
|
||||
class StudIPEventPageAdapter(private val events: List<StudIPEvent>) : RecyclerView.Adapter<StudIPEventPageAdapter.EventPageViewHolder>() {
|
||||
class StudIPEventPageAdapter(private var events: List<StudIPEvent>) : RecyclerView.Adapter<StudIPEventPageAdapter.EventPageViewHolder>() {
|
||||
|
||||
class EventPageViewHolder(val binding: ItemEventPageBinding) : RecyclerView.ViewHolder(binding.root)
|
||||
|
||||
@@ -29,4 +29,9 @@ class StudIPEventPageAdapter(private val events: List<StudIPEvent>) : RecyclerVi
|
||||
scheduleLayoutAnimation()
|
||||
}
|
||||
}
|
||||
|
||||
fun setNewItems(items: List<StudIPEvent>) {
|
||||
events = items
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -2,7 +2,7 @@ package com.denizk0461.studip.data
|
||||
|
||||
import com.denizk0461.studip.model.StudIPEvent
|
||||
|
||||
object DummyData {
|
||||
object Misc {
|
||||
|
||||
val events: Array<StudIPEvent> = arrayOf(
|
||||
StudIPEvent(1, "Literatures", "Nittel", "MZH 1380/1400", 0, 3, 1),
|
||||
@@ -58,4 +58,6 @@ object DummyData {
|
||||
val length = (timeslotEndMap[splitTime[1]] ?: 0)
|
||||
return Pair(start, length)
|
||||
}
|
||||
|
||||
val mysteryLink = "https://www.youtube.com/watch?v=nhIQMCXJzLI"
|
||||
}
|
||||
@@ -24,7 +24,7 @@ class StudIPParser {
|
||||
val parsedLecturers = entryHeader.substring(delimiter + 1 until entryHeader.length - 1)
|
||||
|
||||
val entryInfos = entry.getElementsByTag("dd")[0].text().split(", ", limit = 2)
|
||||
val timeSlot = DummyData.parseTimeslot(entryInfos[0])
|
||||
val timeSlot = Misc.parseTimeslot(entryInfos[0])
|
||||
|
||||
val event = StudIPEvent(
|
||||
id = id,
|
||||
|
||||
@@ -1,23 +1,17 @@
|
||||
package com.denizk0461.studip.fragment
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.fragment.app.Fragment
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.navigation.fragment.NavHostFragment.Companion.findNavController
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.PagerSnapHelper
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.recyclerview.widget.RecyclerView.OnScrollListener
|
||||
import com.denizk0461.studip.R
|
||||
import com.denizk0461.studip.adapter.StudIPEventPageAdapter
|
||||
import com.denizk0461.studip.databinding.FragmentEventBinding
|
||||
import com.denizk0461.studip.viewmodel.EventViewModel
|
||||
import com.google.android.material.tabs.TabLayout
|
||||
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
|
||||
import com.google.android.material.tabs.TabLayoutMediator
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
@@ -30,11 +24,16 @@ class EventFragment : Fragment() {
|
||||
// This property is only valid between onCreateView and
|
||||
// onDestroyView.
|
||||
private val binding get() = _binding!!
|
||||
private lateinit var recyclerViewAdapter: StudIPEventPageAdapter
|
||||
private lateinit var layoutManager: LinearLayoutManager
|
||||
|
||||
private lateinit var viewPagerAdapter: StudIPEventPageAdapter
|
||||
private var dayOfWeek: Int = 0
|
||||
private val pagerSnapHelper = PagerSnapHelper()
|
||||
private var isUserScrolling = false
|
||||
private val dayStrings = listOf(
|
||||
R.string.monday,
|
||||
R.string.tuesday,
|
||||
R.string.wednesday,
|
||||
R.string.thursday,
|
||||
R.string.friday,
|
||||
)
|
||||
|
||||
private val viewModel: EventViewModel by viewModels()
|
||||
|
||||
@@ -54,15 +53,15 @@ class EventFragment : Fragment() {
|
||||
else -> 0
|
||||
}
|
||||
|
||||
viewModel.allEvents.observe(viewLifecycleOwner) { events ->
|
||||
recyclerViewAdapter = StudIPEventPageAdapter(events)//DummyData.events.toList())
|
||||
binding.recyclerView.adapter = recyclerViewAdapter
|
||||
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
|
||||
binding.recyclerView.layoutManager = layoutManager
|
||||
binding.recyclerView.onFlingListener = null
|
||||
pagerSnapHelper.attachToRecyclerView(binding.recyclerView)
|
||||
// binding.recyclerView.scheduleLayoutAnimation()
|
||||
viewPagerAdapter = StudIPEventPageAdapter(listOf())//DummyData.events.toList())
|
||||
binding.viewPager.adapter = viewPagerAdapter
|
||||
|
||||
TabLayoutMediator(binding.dayTabLayout, binding.viewPager) { tab, position ->
|
||||
tab.text = getString(dayStrings[position])
|
||||
}.attach()
|
||||
|
||||
viewModel.allEvents.observe(viewLifecycleOwner) { events ->
|
||||
viewPagerAdapter.setNewItems(events)
|
||||
switchToCurrentDayView()
|
||||
}
|
||||
|
||||
@@ -70,42 +69,15 @@ class EventFragment : Fragment() {
|
||||
launchWebview()
|
||||
}
|
||||
|
||||
binding.recyclerView.addOnScrollListener(object : OnScrollListener() {
|
||||
|
||||
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
|
||||
super.onScrollStateChanged(recyclerView, newState)
|
||||
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
|
||||
binding.dayTabs.getTabAt(layoutManager.findFirstVisibleItemPosition())?.select()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
binding.dayTabs.addOnTabSelectedListener(object : OnTabSelectedListener {
|
||||
override fun onTabSelected(tab: TabLayout.Tab) {
|
||||
isUserScrolling = false
|
||||
binding.recyclerView.smoothScrollToPosition(tab.position)
|
||||
Log.d("HELLO4", binding.recyclerView.scrollY.toString())
|
||||
// TODO where to set isUserScrolling back to true?
|
||||
}
|
||||
override fun onTabUnselected(tab: TabLayout.Tab?) {}
|
||||
override fun onTabReselected(tab: TabLayout.Tab?) {}
|
||||
})
|
||||
|
||||
// binding.daytabmonday.setOnClickListener { binding.recyclerView.scrollToPosition(0) }
|
||||
// binding.daytabtuesday.setOnClickListener { binding.recyclerView.scrollToPosition(1) }
|
||||
// binding.daytabwednesday.setOnClickListener { binding.recyclerView.scrollToPosition(2) }
|
||||
// binding.daytabthursday.setOnClickListener { binding.recyclerView.scrollToPosition(3) }
|
||||
// binding.daytabfriday.setOnClickListener { binding.recyclerView.scrollToPosition(4) }
|
||||
|
||||
// binding.buttonFirst.setOnClickListener {
|
||||
// findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
|
||||
// }
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
switchToCurrentDayView()
|
||||
}
|
||||
// override fun onResume() {
|
||||
// super.onResume()
|
||||
// switchToCurrentDayView()
|
||||
// }
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
@@ -113,8 +85,8 @@ class EventFragment : Fragment() {
|
||||
}
|
||||
|
||||
private fun switchToCurrentDayView() {
|
||||
binding.recyclerView.scrollToPosition(dayOfWeek)
|
||||
binding.dayTabs.getTabAt(dayOfWeek)?.select()
|
||||
// binding.viewpager.currentItem = dayOfWeek
|
||||
binding.dayTabLayout.getTabAt(dayOfWeek)?.select()
|
||||
}
|
||||
|
||||
private fun launchWebview() {
|
||||
|
||||
@@ -7,52 +7,24 @@
|
||||
tools:context=".fragment.EventFragment">
|
||||
|
||||
<com.google.android.material.tabs.TabLayout
|
||||
android:id="@+id/day_tabs"
|
||||
android:id="@+id/day_tab_layout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:tabMode="scrollable"
|
||||
android:background="@android:color/transparent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent">
|
||||
app:layout_constraintEnd_toEndOf="parent"/>
|
||||
|
||||
<com.google.android.material.tabs.TabItem
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/monday"/>
|
||||
|
||||
<com.google.android.material.tabs.TabItem
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/tuesday"/>
|
||||
|
||||
<com.google.android.material.tabs.TabItem
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/wednesday"/>
|
||||
|
||||
<com.google.android.material.tabs.TabItem
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/thursday"/>
|
||||
|
||||
<com.google.android.material.tabs.TabItem
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/friday"/>
|
||||
|
||||
</com.google.android.material.tabs.TabLayout>
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recycler_view"
|
||||
<androidx.viewpager2.widget.ViewPager2
|
||||
android:id="@+id/view_pager"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/day_tabs"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
android:scrollbars="horizontal"
|
||||
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
|
||||
app:layout_constraintTop_toBottomOf="@+id/day_tab_layout"
|
||||
app:layout_constraintBottom_toBottomOf="parent"/>
|
||||
<!-- app:layout_behavior="@string/appbar_scrolling_view_behavior"-->
|
||||
|
||||
<com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
android:id="@+id/fab"
|
||||
|
||||
Reference in New Issue
Block a user