|
|
@@ -1,6 +1,7 @@
|
|
|
package com.bearya.kids.section
|
|
|
|
|
|
import android.os.Bundle
|
|
|
+import android.view.KeyEvent
|
|
|
import android.view.LayoutInflater
|
|
|
import android.view.View
|
|
|
import android.view.ViewGroup
|
|
|
@@ -15,6 +16,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
import androidx.viewpager2.widget.ViewPager2
|
|
|
import com.bearya.data.entity.Section
|
|
|
import com.bearya.kids.databinding.FragmentSectionBinding
|
|
|
+import com.orhanobut.logger.Logger
|
|
|
import kotlinx.coroutines.*
|
|
|
import library.ext.setData
|
|
|
|
|
|
@@ -24,13 +26,13 @@ class SectionFragment : Fragment() {
|
|
|
private val viewModel: SectionViewModel by viewModels()
|
|
|
private val args: SectionFragmentArgs by navArgs()
|
|
|
|
|
|
- private val sectionQuestionAdapter: SectionQuestionAdapter by lazy { SectionQuestionAdapter(args.chapterName) }
|
|
|
+ private val pagerAdapter: SectionPagerAdapter by lazy { SectionPagerAdapter(args.chapterName) }
|
|
|
private val previewAdapter: SectionPreviewAdapter by lazy { SectionPreviewAdapter(args.chapterName) }
|
|
|
|
|
|
private val pageChangeCallback by lazy {
|
|
|
object : ViewPager2.OnPageChangeCallback() {
|
|
|
override fun onPageSelected(position: Int) {
|
|
|
- previewAdapter.setSelectedIndex(position)
|
|
|
+ viewModel.playIndex.setData(position)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -48,40 +50,57 @@ class SectionFragment : Fragment() {
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
|
- bindView.playPager.adapter = sectionQuestionAdapter
|
|
|
-
|
|
|
+ bindView.playPager.adapter = pagerAdapter
|
|
|
bindView.playPager.registerOnPageChangeCallback(pageChangeCallback)
|
|
|
-
|
|
|
- sectionQuestionAdapter.onItemClickListener = { _: View, _: Section?, _: Int ->
|
|
|
- cancelHideSectionPreview()
|
|
|
-
|
|
|
- val visibility = bindView.playList.visibility.takeIf { it == View.VISIBLE }
|
|
|
- ?.let { View.GONE } ?: View.VISIBLE
|
|
|
- bindView.playList.visibility = visibility
|
|
|
- bindView.back.visibility = visibility
|
|
|
- bindView.analysis.visibility = visibility
|
|
|
-
|
|
|
- hideSectionPreview()
|
|
|
-
|
|
|
+ pagerAdapter.onItemClickListener = { _: View, _: Section?, _: Int ->
|
|
|
+ onItemEvents()
|
|
|
+ }
|
|
|
+ bindView.playPager.setOnKeyListener { _, keyCode, event ->
|
|
|
+ Logger.d("有了点击事件了keycode = $keyCode 和 event = ${event.action}")
|
|
|
+ if (KeyEvent.ACTION_UP == event.action) {
|
|
|
+ when (keyCode) {
|
|
|
+ KeyEvent.KEYCODE_MENU -> onItemEvents()
|
|
|
+ KeyEvent.KEYCODE_BACK -> view.findNavController().navigateUp()
|
|
|
+ KeyEvent.KEYCODE_DPAD_LEFT -> {
|
|
|
+ if (viewModel.playIndex.value ?: 0 > 0) {
|
|
|
+ viewModel.playIndex.setData(viewModel.playIndex.value?.minus(1))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ KeyEvent.KEYCODE_DPAD_RIGHT -> {
|
|
|
+ if (viewModel.playIndex.value ?: 0 < viewModel.sectionDirFileSize - 1) {
|
|
|
+ viewModel.playIndex.setData(viewModel.playIndex.value?.plus(1))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ true
|
|
|
+ }
|
|
|
+ bindView.playPager.setOnFocusChangeListener { v, hasFocus ->
|
|
|
+ if (!hasFocus) {
|
|
|
+ v.requestFocus()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- bindView.playList.layoutManager =
|
|
|
- LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
|
|
|
+ bindView.playList.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
|
|
|
bindView.playList.adapter = previewAdapter
|
|
|
-
|
|
|
previewAdapter.onItemClickListener = { _: View, _: Section?, position: Int ->
|
|
|
bindView.playPager.currentItem = position
|
|
|
}
|
|
|
|
|
|
viewModel.sections.observe(viewLifecycleOwner) {
|
|
|
- sectionQuestionAdapter.submitList(it)
|
|
|
+ pagerAdapter.submitList(it)
|
|
|
previewAdapter.submitList(it)
|
|
|
+ viewModel.sectionDirFileSize = it.size
|
|
|
+ }
|
|
|
+
|
|
|
+ viewModel.playIndex.observe(viewLifecycleOwner) {
|
|
|
+ previewAdapter.setSelectedIndex(it)
|
|
|
}
|
|
|
|
|
|
bindView.back.setOnClickListener { v -> v.findNavController().navigateUp() }
|
|
|
|
|
|
bindView.analysis.setOnClickListener {
|
|
|
- val itemData = sectionQuestionAdapter.getItemData(bindView.playPager.currentItem)
|
|
|
+ val itemData = pagerAdapter.getItemData(bindView.playPager.currentItem)
|
|
|
|
|
|
if (itemData?.hasAnalysis == true) {
|
|
|
val action = SectionFragmentDirections.actionSectionFragmentToAnalysisFragment(itemData.id ?: 1 , args.chapterName)
|
|
|
@@ -103,6 +122,18 @@ class SectionFragment : Fragment() {
|
|
|
bindView.playPager.unregisterOnPageChangeCallback(pageChangeCallback)
|
|
|
}
|
|
|
|
|
|
+ private fun onItemEvents(){
|
|
|
+ cancelHideSectionPreview()
|
|
|
+
|
|
|
+ val visibility = bindView.playList.visibility.takeIf { it == View.VISIBLE }
|
|
|
+ ?.let { View.GONE } ?: View.VISIBLE
|
|
|
+ bindView.playList.visibility = visibility
|
|
|
+ bindView.back.visibility = visibility
|
|
|
+ bindView.analysis.visibility = visibility
|
|
|
+
|
|
|
+ hideSectionPreview()
|
|
|
+ }
|
|
|
+
|
|
|
private var launchDelayHide: Job? = null
|
|
|
|
|
|
private fun hideSectionPreview() {
|