Skip to content

Commit

Permalink
support touch
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Mar 14, 2024
1 parent dd18779 commit e5cac3e
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 232 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

## 更新日志

### v1.0.2

* 改变部分样式

### v1.0.1

* 支持返回键退出
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.lizongying.mytv0.databinding.CategoryItemBinding
import com.lizongying.mytv0.models.TVCategoryModel
import com.lizongying.mytv0.databinding.GroupItemBinding
import com.lizongying.mytv0.models.TVGroupModel
import com.lizongying.mytv0.models.TVListModel


class CategoryAdapter(
class GroupAdapter(
private val context: Context,
private val recyclerView: RecyclerView,
private var tvCategoryModel: TVCategoryModel,
private var tvgroupModel: TVGroupModel,
) :
RecyclerView.Adapter<CategoryAdapter.ViewHolder>() {
RecyclerView.Adapter<GroupAdapter.ViewHolder>() {

private var listener: ItemListener? = null
private var focused: View? = null
Expand All @@ -29,7 +29,7 @@ class CategoryAdapter(

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(context)
val binding = CategoryItemBinding.inflate(inflater, parent, false)
val binding = GroupItemBinding.inflate(inflater, parent, false)
binding.root.isFocusable = true
binding.root.isFocusableInTouchMode = true
return ViewHolder(context, binding)
Expand All @@ -51,7 +51,7 @@ class CategoryAdapter(
}

override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val tvListModel = tvCategoryModel.getTVListModel(position)!!
val tvListModel = tvgroupModel.getTVListModel(position)!!
val view = viewHolder.itemView
view.tag = position

Expand All @@ -67,8 +67,8 @@ class CategoryAdapter(
viewHolder.focus(true)
focused = view
if (visiable) {
if (position != tvCategoryModel.position.value) {
tvCategoryModel.setPosition(position)
if (position != tvgroupModel.position.value) {
tvgroupModel.setPosition(position)
}
} else {
visiable = true
Expand Down Expand Up @@ -102,9 +102,9 @@ class CategoryAdapter(
viewHolder.bind(tvListModel.getName())
}

override fun getItemCount() = tvCategoryModel.size()
override fun getItemCount() = tvgroupModel.size()

class ViewHolder(private val context: Context, private val binding: CategoryItemBinding) :
class ViewHolder(private val context: Context, private val binding: GroupItemBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(text: String) {
binding.textView.text = text
Expand All @@ -126,7 +126,7 @@ class CategoryAdapter(

fun toPosition(position: Int) {
recyclerView.post {
Log.i(TAG, "category smoothScrollToPosition $position")
Log.i(TAG, "group smoothScrollToPosition $position")
recyclerView.scrollToPosition(position)
recyclerView.getChildAt(position)?.isSelected
recyclerView.getChildAt(position)?.requestFocus()
Expand Down
42 changes: 41 additions & 1 deletion app/src/main/java/com/lizongying/mytv0/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.GestureDetector
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.widget.Toast
Expand All @@ -31,6 +33,8 @@ class MainActivity : FragmentActivity() {

private var position = 0

private lateinit var gestureDetector: GestureDetector

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand Down Expand Up @@ -60,6 +64,8 @@ class MainActivity : FragmentActivity() {
.commitNow()
}

gestureDetector = GestureDetector(this, GestureListener())

TVList.listModel.forEach { tvModel ->
tvModel.errInfo.observe(this) { _ ->
if (tvModel.errInfo.value != null
Expand Down Expand Up @@ -87,6 +93,40 @@ class MainActivity : FragmentActivity() {
TVList.setPosition(SP.position)
}

override fun onTouchEvent(event: MotionEvent?): Boolean {
if (event != null) {
gestureDetector.onTouchEvent(event)
}
return super.onTouchEvent(event)
}

private inner class GestureListener : GestureDetector.SimpleOnGestureListener() {

override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
showMenu()
return true
}

override fun onFling(
e1: MotionEvent?,
e2: MotionEvent,
velocityX: Float,
velocityY: Float
): Boolean {
if (velocityY > 0) {
if (menuFragment.isHidden && settingFragment.isHidden) {
prev()
}
}
if (velocityY < 0) {
if (menuFragment.isHidden && settingFragment.isHidden) {
next()
}
}
return super.onFling(e1, e2, velocityX, velocityY)
}
}

fun play(position: Int) {
if (position > -1 && position < TVList.size()) {
TVList.setPosition(position)
Expand Down Expand Up @@ -129,7 +169,7 @@ class MainActivity : FragmentActivity() {

private val hideSetting = Runnable {
if (!settingFragment.isHidden) {
supportFragmentManager.beginTransaction().hide(settingFragment).commit()
supportFragmentManager.beginTransaction().hide(settingFragment).commitNow()
}
}

Expand Down
48 changes: 24 additions & 24 deletions app/src/main/java/com/lizongying/mytv0/MenuFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import com.lizongying.mytv0.models.TVList
import com.lizongying.mytv0.models.TVListModel
import com.lizongying.mytv0.models.TVModel

class MenuFragment : Fragment(), CategoryAdapter.ItemListener, ListAdapter.ItemListener {
class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemListener {
private var _binding: MenuBinding? = null
private val binding get() = _binding!!

private lateinit var categoryAdapter: CategoryAdapter
private lateinit var groupAdapter: GroupAdapter
private lateinit var listAdapter: ListAdapter

override fun onActivityCreated(savedInstanceState: Bundle?) {
Expand All @@ -35,20 +35,20 @@ class MenuFragment : Fragment(), CategoryAdapter.ItemListener, ListAdapter.ItemL
): View {
_binding = MenuBinding.inflate(inflater, container, false)

categoryAdapter = CategoryAdapter(
groupAdapter = GroupAdapter(
context!!,
binding.category,
TVList.categoryModel,
binding.group,
TVList.groupModel,
)
binding.category.adapter = categoryAdapter
binding.category.layoutManager =
binding.group.adapter = groupAdapter
binding.group.layoutManager =
LinearLayoutManager(context)
categoryAdapter.setItemListener(this)
groupAdapter.setItemListener(this)

listAdapter = ListAdapter(
context!!,
binding.list,
TVList.categoryModel.getTVListModel(TVList.categoryModel.position.value!!)!!,
TVList.groupModel.getTVListModel(TVList.groupModel.position.value!!)!!,
)
binding.list.adapter = listAdapter
binding.list.layoutManager =
Expand Down Expand Up @@ -86,8 +86,8 @@ class MenuFragment : Fragment(), CategoryAdapter.ItemListener, ListAdapter.ItemL
Toast.makeText(context, "暂无频道", Toast.LENGTH_LONG).show()
return true
}
binding.category.visibility = GONE
categoryAdapter.focusable(false)
binding.group.visibility = GONE
groupAdapter.focusable(false)
listAdapter.focusable(true)
listAdapter.toPosition(listAdapter.tvListModel.position.value!!)
return true
Expand All @@ -104,12 +104,12 @@ class MenuFragment : Fragment(), CategoryAdapter.ItemListener, ListAdapter.ItemL
override fun onKey(listAdapter: ListAdapter, keyCode: Int): Boolean {
when (keyCode) {
KeyEvent.KEYCODE_DPAD_LEFT -> {
binding.category.visibility = VISIBLE
categoryAdapter.focusable(true)
binding.group.visibility = VISIBLE
groupAdapter.focusable(true)
listAdapter.focusable(false)
listAdapter.clear()
Log.i(TAG, "category toPosition on left")
categoryAdapter.toPosition(TVList.categoryModel.position.value!!)
Log.i(TAG, "group toPosition on left")
groupAdapter.toPosition(TVList.groupModel.position.value!!)
return true
}
}
Expand All @@ -120,33 +120,33 @@ class MenuFragment : Fragment(), CategoryAdapter.ItemListener, ListAdapter.ItemL
super.onHiddenChanged(hidden)
if (!hidden) {
if (binding.list.isVisible) {
// if (binding.category.isVisible) {
// categoryAdapter.focusable(true)
// if (binding.group.isVisible) {
// groupAdapter.focusable(true)
// listAdapter.focusable(false)
// } else {
// categoryAdapter.focusable(false)
// groupAdapter.focusable(false)
// listAdapter.focusable(true)
// }
Log.i(TAG, "list on show toPosition ${listAdapter.tvListModel.position.value!!}")
listAdapter.toPosition(listAdapter.tvListModel.position.value!!)
}
if (binding.category.isVisible) {
// categoryAdapter.focusable(true)
if (binding.group.isVisible) {
// groupAdapter.focusable(true)
// listAdapter.focusable(false)
Log.i(TAG, "category on show toPosition ${TVList.categoryModel.position.value!!}")
categoryAdapter.toPosition(TVList.categoryModel.position.value!!)
Log.i(TAG, "group on show toPosition ${TVList.groupModel.position.value!!}")
groupAdapter.toPosition(TVList.groupModel.position.value!!)
}
} else {
view?.post {
categoryAdapter.visiable = false
groupAdapter.visiable = false
listAdapter.visiable = false
}
}
}

override fun onResume() {
super.onResume()
// categoryAdapter.toPosition(TVList.categoryModel.position.value!!)
// groupAdapter.toPosition(TVList.groupModel.position.value!!)
}

override fun onDestroyView() {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/lizongying/mytv0/SP.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object SP {
// Position in list of the selected channel item
private const val KEY_POSITION = "position"

private const val KEY_POSITION_CATEGORY = "position_category"
private const val KEY_POSITION_CATEGORY = "position_group"

private const val KEY_POSITION_SUB = "position_sub"

Expand Down
13 changes: 7 additions & 6 deletions app/src/main/java/com/lizongying/mytv0/models/TV.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@ import java.io.Serializable

data class TV(
var id: Int = 0,
var programId: String = "",
var name: String = "",
var title: String = "",
var description: String? = null,
var logo: String = "",
var image: String? = null,
var videoUrl: List<String>,
var uris: List<String>,
var headers: Map<String, String>? = null,
var category: String = "",
var group: String = "",
var child: List<TV>,
) : Serializable {

override fun toString(): String {
return "TV{" +
"id=" + id +
", programId='" + programId + '\'' +
", name='" + name + '\'' +
", title='" + title + '\'' +
", description='" + description + '\'' +
", logo='" + logo + '\'' +
", image='" + image + '\'' +
", videoUrl='" + videoUrl + '\'' +
", category='" + category + '\'' +
", uris='" + uris + '\'' +
", headers='" + headers + '\'' +
", group='" + group + '\'' +
'}'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.lizongying.mytv0.SP

class TVCategoryModel : ViewModel() {
private val _tvCategoryModel = MutableLiveData<MutableList<TVListModel>>()
val tvCategoryModel: LiveData<MutableList<TVListModel>>
get() = _tvCategoryModel
class TVGroupModel : ViewModel() {
private val _tvGroupModel = MutableLiveData<MutableList<TVListModel>>()
val tvGroupModel: LiveData<MutableList<TVListModel>>
get() = _tvGroupModel

private val _position = MutableLiveData<Int>()
val position: LiveData<Int>
Expand All @@ -19,31 +19,30 @@ class TVCategoryModel : ViewModel() {
}

fun setTVListModelList(tvTVListModelList: MutableList<TVListModel>) {
_tvCategoryModel.value = tvTVListModelList
_tvGroupModel.value = tvTVListModelList
}

fun addTVListModel(tvListModel: TVListModel) {
if (_tvCategoryModel.value == null) {
_tvCategoryModel.value = mutableListOf(tvListModel)
if (_tvGroupModel.value == null) {
_tvGroupModel.value = mutableListOf(tvListModel)
return
}
_tvCategoryModel.value?.add(tvListModel)
_tvGroupModel.value?.add(tvListModel)
}

fun getTVListModel(idx: Int): TVListModel? {
return _tvCategoryModel.value?.get(idx)
return _tvGroupModel.value?.get(idx)
}

init {
_position.value = SP.positionCategory
_position.value = 2
}

fun size(): Int {
if (_tvCategoryModel.value == null) {
if (_tvGroupModel.value == null) {
return 0
}

return _tvCategoryModel.value!!.size
return _tvGroupModel.value!!.size
}
}
Loading

0 comments on commit e5cac3e

Please sign in to comment.