Browse Source

修改课件支持遥控器焦点模式

infinite.likelins 5 years ago
parent
commit
283ead3dc1

+ 24 - 1
app/src/main/java/com/bearya/kids/chapter/ChapterAdapter.kt

@@ -4,6 +4,7 @@ import android.text.TextUtils
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import androidx.core.view.ViewCompat
 import androidx.databinding.DataBindingUtil
 import androidx.paging.PagedListAdapter
 import androidx.recyclerview.widget.DiffUtil
@@ -26,6 +27,7 @@ class ChapterAdapter : PagedListAdapter<Chapter, UnitViewHolder>(object : DiffUt
 }) {
 
     var onItemClickListener: OnItemClickListener<Chapter>? = null
+    private var recyclerView: RecyclerView? = null
 
     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UnitViewHolder = UnitViewHolder(
             ItemChapterBinding.inflate(LayoutInflater.from(parent.context), parent, false).root
@@ -35,9 +37,30 @@ class ChapterAdapter : PagedListAdapter<Chapter, UnitViewHolder>(object : DiffUt
         val item = getItem(position)
         holder.bindView?.name = item?.name
         holder.bindView?.cover = "chapter/${item?.name}/${item?.name}.webp".assetsPath()
-        holder.itemView.setOnClickListener { onItemClickListener?.invoke(it, item, position) }
+        holder.itemView.apply {
+
+            setOnClickListener {
+                onItemClickListener?.invoke(it, item, position)
+            }
+            onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
+                if (hasFocus) {
+                    recyclerView?.smoothScrollToPosition(position)
+                }
+                val scale = if (hasFocus) 1.1f else 1.0f
+                ViewCompat.animate(v).scaleX(scale).scaleY(scale).start()
+            }
+        }
+    }
+
+    override fun onDetachedFromRecyclerView(rv: RecyclerView) {
+        super.onDetachedFromRecyclerView(rv)
+        recyclerView = null
     }
 
+    override fun onAttachedToRecyclerView(rv: RecyclerView) {
+        super.onAttachedToRecyclerView(rv)
+        recyclerView = rv
+    }
 }
 
 class UnitViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

+ 6 - 3
app/src/main/java/com/bearya/kids/chapter/ChapterFragment.kt

@@ -30,8 +30,10 @@ class ChapterFragment : Fragment() {
         ChapterAdapter().apply {
             onItemClickListener = { view: View, item: Chapter?, _: Int ->
                 view.findNavController().navigate(
-                        ChapterFragmentDirections.actionChapterFragmentToSectionFragment(item?.id
-                                ?: 1, item?.name)
+                    ChapterFragmentDirections.actionChapterFragmentToSectionFragment(
+                        item?.id
+                            ?: 1, item?.name
+                    )
                 )
             }
         }
@@ -59,7 +61,8 @@ class ChapterFragment : Fragment() {
         }
 
         bindView.back.setOnClickListener { v -> v.findNavController().navigateUp() }
-        bindView.chapter.layoutManager = GridLayoutManager(context, 2, GridLayoutManager.HORIZONTAL, false)
+        bindView.chapter.layoutManager =
+            GridLayoutManager(context, 2, GridLayoutManager.HORIZONTAL, false)
         bindView.chapter.adapter = chapterAdapter
 
         viewModel.chapters.observe(viewLifecycleOwner) {

+ 9 - 1
app/src/main/java/com/bearya/kids/grade/GradeFragment.kt

@@ -10,7 +10,7 @@ import com.bearya.kids.BuildConfig
 import com.bearya.kids.R
 import com.bearya.kids.databinding.FragmentGradeBinding
 
-class GradeFragment : Fragment(), View.OnClickListener {
+class GradeFragment : Fragment(), View.OnClickListener, View.OnFocusChangeListener {
 
     private val gradeLevelPrimary = 1 // 小班
     private val gradeLevelMiddle = 2 // 中班
@@ -29,8 +29,11 @@ class GradeFragment : Fragment(), View.OnClickListener {
         val versionName = "当前版本 : v ${BuildConfig.VERSION_NAME}.${BuildConfig.VERSION_CODE}"
         bindView.version.text = versionName
         bindView.gradePrimary.setOnClickListener(this)
+        bindView.gradePrimary.onFocusChangeListener = this
         bindView.gradeMiddle.setOnClickListener(this)
+        bindView.gradeMiddle.onFocusChangeListener = this
         bindView.gradeLarge.setOnClickListener(this)
+        bindView.gradeLarge.onFocusChangeListener = this
         bindView.back.setOnClickListener(this)
     }
 
@@ -43,4 +46,9 @@ class GradeFragment : Fragment(), View.OnClickListener {
         }
     }
 
+    override fun onFocusChange(v: View?, hasFocus: Boolean) {
+        val scale = if (hasFocus) 1.2f else 1.0f
+        v?.animate()?.scaleX(scale)?.scaleY(scale)?.start()
+    }
+
 }

+ 4 - 4
app/src/main/java/com/bearya/kids/section/SectionAdapter.kt

@@ -16,7 +16,7 @@ import com.bearya.kids.databinding.ItemSectionPreviewBinding
 import library.OnItemClickListener
 import library.ext.assetsPath
 
-class SectionQuestionAdapter(private val dirName: String?) : PagedListAdapter<Section, SectionQuestionViewHolder>(object : DiffUtil.ItemCallback<Section>() {
+class SectionPagerAdapter(private val dirName: String?) : PagedListAdapter<Section, SectionPagerViewHolder>(object : DiffUtil.ItemCallback<Section>() {
 
     override fun areItemsTheSame(oldItem: Section, newItem: Section): Boolean = oldItem.id == newItem.id
 
@@ -27,11 +27,11 @@ class SectionQuestionAdapter(private val dirName: String?) : PagedListAdapter<Se
 
     var onItemClickListener: OnItemClickListener<Section>? = null
 
-    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SectionQuestionViewHolder = SectionQuestionViewHolder(
+    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SectionPagerViewHolder = SectionPagerViewHolder(
             ItemSectionBinding.inflate(LayoutInflater.from(parent.context), parent, false).root
     )
 
-    override fun onBindViewHolder(holder: SectionQuestionViewHolder, position: Int) {
+    override fun onBindViewHolder(holder: SectionPagerViewHolder, position: Int) {
         val item = getItem(position)
         holder.bindView?.image = "chapter/${dirName}/${item?.name}.webp".assetsPath()
         holder.itemView.setOnClickListener { onItemClickListener?.invoke(it, item, position) }
@@ -41,7 +41,7 @@ class SectionQuestionAdapter(private val dirName: String?) : PagedListAdapter<Se
 
 }
 
-class SectionQuestionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
+class SectionPagerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
     val bindView: ItemSectionBinding? = DataBindingUtil.bind(itemView)
 }
 

+ 52 - 21
app/src/main/java/com/bearya/kids/section/SectionFragment.kt

@@ -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() {

+ 5 - 0
app/src/main/java/com/bearya/kids/section/SectionViewModel.kt

@@ -7,6 +7,7 @@ import androidx.lifecycle.switchMap
 import androidx.paging.PagedList
 import com.bearya.data.entity.Section
 import com.bearya.data.repository.SectionRepository
+import library.ext.setData
 import library.ext.toLiveData
 
 class SectionViewModel : ViewModel() {
@@ -18,4 +19,8 @@ class SectionViewModel : ViewModel() {
     val sections :LiveData<PagedList<Section>> = chapterId.switchMap {
         repository.findSectionByChapterId(it).toLiveData()
     }
+
+    var sectionDirFileSize : Int = 0
+
+    val playIndex: MutableLiveData<Int> by lazy { MutableLiveData<Int>().apply { setData(0) } }
 }

+ 2 - 0
app/src/main/res/layout/fragment_chapter.xml

@@ -46,6 +46,8 @@
             android:layout_height="0dp"
             android:overScrollMode="never"
             android:scrollbars="none"
+            android:clipChildren="false"
+            android:clipToPadding="true"
             app:layout_constraintBottom_toBottomOf="parent"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintStart_toStartOf="parent"

+ 8 - 0
app/src/main/res/layout/fragment_grade.xml

@@ -28,6 +28,8 @@
             android:id="@+id/grade_primary"
             android:layout_width="0dp"
             android:layout_height="0dp"
+            android:focusable="true"
+            android:nextFocusRight="@+id/grade_middle"
             app:layout_constraintBottom_toBottomOf="@id/horizontal"
             app:layout_constraintEnd_toStartOf="@+id/grade_middle"
             app:layout_constraintHeight_percent="0.45"
@@ -53,6 +55,9 @@
             android:id="@+id/grade_middle"
             android:layout_width="0dp"
             android:layout_height="0dp"
+            android:focusable="true"
+            android:nextFocusLeft="@id/grade_primary"
+            android:nextFocusRight="@+id/grade_large"
             app:layout_constraintBottom_toBottomOf="@id/horizontal"
             app:layout_constraintEnd_toStartOf="@+id/grade_large"
             app:layout_constraintHeight_percent="0.45"
@@ -65,6 +70,8 @@
         <androidx.appcompat.widget.AppCompatTextView
             android:layout_width="0dp"
             android:layout_height="wrap_content"
+            android:focusable="true"
+            android:nextFocusLeft="@id/grade_middle"
             android:padding="@dimen/dp8"
             android:text="@string/grade_middle"
             android:textAlignment="center"
@@ -78,6 +85,7 @@
             android:id="@+id/grade_large"
             android:layout_width="0dp"
             android:layout_height="0dp"
+            android:focusable="true"
             app:layout_constraintBottom_toBottomOf="@id/horizontal"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintHeight_percent="0.45"

+ 3 - 1
app/src/main/res/layout/fragment_section.xml

@@ -11,6 +11,8 @@
             android:id="@+id/play_pager"
             android:layout_width="0dp"
             android:layout_height="0dp"
+            android:focusable="true"
+            android:focusableInTouchMode="true"
             android:orientation="horizontal"
             app:layout_constraintBottom_toBottomOf="parent"
             app:layout_constraintEnd_toEndOf="parent"
@@ -67,4 +69,4 @@
 
     </androidx.constraintlayout.widget.ConstraintLayout>
 
-</layout>
+</layout>

+ 6 - 6
app/src/main/res/layout/item_chapter.xml

@@ -18,10 +18,10 @@
     <androidx.cardview.widget.CardView
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
-        android:layout_marginStart="@dimen/dp4"
-        android:layout_marginTop="@dimen/dp4"
-        android:layout_marginEnd="@dimen/dp4"
-        android:layout_marginBottom="@dimen/dp4"
+        android:layout_marginStart="@dimen/dp24"
+        android:layout_marginTop="@dimen/dp8"
+        android:layout_marginEnd="@dimen/dp24"
+        android:layout_marginBottom="@dimen/dp8"
         android:focusable="true"
         android:foreground="?selectableItemBackground"
         android:gravity="center"
@@ -60,12 +60,12 @@
                 android:textAllCaps="false"
                 android:textColor="@color/colorWhite"
                 app:autoSizeTextType="uniform"
-                app:layout_constraintHeight_percent="0.2"
-                app:layout_constraintVertical_bias="1"
                 app:layout_constraintBottom_toBottomOf="@id/unit_cover"
                 app:layout_constraintEnd_toEndOf="@id/unit_cover"
+                app:layout_constraintHeight_percent="0.2"
                 app:layout_constraintStart_toStartOf="@id/unit_cover"
                 app:layout_constraintTop_toTopOf="@id/unit_cover"
+                app:layout_constraintVertical_bias="1"
                 tools:text="@tools:sample/full_names" />
 
         </androidx.constraintlayout.widget.ConstraintLayout>

+ 1 - 1
app/src/main/res/layout/item_section_preview.xml

@@ -17,7 +17,7 @@
 
     <androidx.constraintlayout.widget.ConstraintLayout
         android:layout_width="wrap_content"
-        android:layout_height="match_parent"
+        android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:descendantFocusability="afterDescendants"
         android:focusable="true"

+ 3 - 3
app/version.properties

@@ -1,3 +1,3 @@
-#Sun Apr 26 11:34:21 CST 2020
-VERSION_NAME=1.1.0
-VERSION_CODE=200
+#Mon Apr 27 15:13:46 CST 2020
+VERSION_NAME=1.1.4
+VERSION_CODE=222