@@ -2,6 +2,7 @@ package com.lizongying.mytv0
2
2
3
3
import android.os.Bundle
4
4
import android.os.Handler
5
+ import android.os.Looper
5
6
import android.view.LayoutInflater
6
7
import android.view.View
7
8
import android.view.ViewGroup
@@ -13,60 +14,71 @@ import com.lizongying.mytv0.models.TVModel
13
14
14
15
class ChannelFragment : Fragment () {
15
16
private var _binding : ChannelBinding ? = null
16
- private val binding get() = _binding !!
17
+ private val binding get() = _binding
17
18
18
- private val handler = Handler ()
19
+ private val handler = Handler (Looper .getMainLooper() )
19
20
private val delay: Long = 3000
20
21
private var channel = 0
21
22
22
23
override fun onCreateView (
23
24
inflater : LayoutInflater , container : ViewGroup ? ,
24
25
savedInstanceState : Bundle ?
25
- ): View {
26
+ ): View ? {
26
27
_binding = ChannelBinding .inflate(inflater, container, false )
27
- _binding !! .root.visibility = View .GONE
28
+ return _binding ?.root?.apply {
29
+ visibility = View .GONE
30
+ }
31
+ }
28
32
29
- val application = requireActivity().applicationContext as MyTVApplication
33
+ override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
34
+ super .onViewCreated(view, savedInstanceState)
35
+ setupUI()
36
+ }
30
37
31
- binding.channel.layoutParams.width = application.px2Px(binding.channel.layoutParams.width)
32
- binding.channel.layoutParams.height = application.px2Px(binding.channel.layoutParams.height)
38
+ private fun setupUI () {
39
+ binding?.let { binding ->
40
+ val application = requireActivity().applicationContext as MyTVApplication
33
41
34
- val layoutParams = binding.channel.layoutParams as ViewGroup .MarginLayoutParams
35
- layoutParams.topMargin = application.px2Px(binding.channel.marginTop)
36
- layoutParams.marginEnd = application.px2Px(binding.channel.marginEnd)
37
- binding.channel.layoutParams = layoutParams
42
+ binding.channel.layoutParams.width = application.px2Px(binding.channel.layoutParams.width)
43
+ binding.channel.layoutParams.height = application.px2Px(binding.channel.layoutParams.height)
38
44
39
- binding.content.textSize = application.px2PxFont(binding.content.textSize)
40
- binding.time.textSize = application.px2PxFont(binding.time.textSize)
45
+ val layoutParams = binding.channel.layoutParams as ViewGroup .MarginLayoutParams
46
+ layoutParams.topMargin = application.px2Px(binding.channel.marginTop)
47
+ layoutParams.marginEnd = application.px2Px(binding.channel.marginEnd)
48
+ binding.channel.layoutParams = layoutParams
41
49
42
- binding.main.layoutParams.width = application.shouldWidthPx( )
43
- binding.main.layoutParams.height = application.shouldHeightPx( )
50
+ binding.content.textSize = application.px2PxFont(binding.content.textSize )
51
+ binding.time.textSize = application.px2PxFont(binding.time.textSize )
44
52
45
- return binding.root
53
+ binding.main.layoutParams.width = application.shouldWidthPx()
54
+ binding.main.layoutParams.height = application.shouldHeightPx()
55
+ }
46
56
}
47
57
48
58
fun show (tvViewModel : TVModel ) {
49
59
handler.removeCallbacks(hideRunnable)
50
60
handler.removeCallbacks(playRunnable)
51
- binding.content.text = (tvViewModel.tv.id.plus(1 )).toString()
61
+ binding? .content? .text = (tvViewModel.tv.id.plus(1 )).toString()
52
62
view?.visibility = View .VISIBLE
53
63
handler.postDelayed(hideRunnable, delay)
54
64
}
55
65
56
66
fun show (channel : String ) {
57
- if (binding.content.text.length > 1 ) {
58
- return
59
- }
60
- this .channel = " ${binding.content.text}$channel " .toInt()
61
- handler.removeCallbacks(hideRunnable)
62
- handler.removeCallbacks(playRunnable)
63
- if (binding.content.text == " " ) {
64
- binding.content.text = channel
65
- view?.visibility = View .VISIBLE
66
- handler.postDelayed(playRunnable, delay)
67
- } else {
68
- binding.content.text = " ${binding.content.text}$channel "
69
- handler.postDelayed(playRunnable, 0 )
67
+ binding?.let { binding ->
68
+ if (binding.content.text.length > 1 ) {
69
+ return
70
+ }
71
+ this .channel = " ${binding.content.text}$channel " .toInt()
72
+ handler.removeCallbacks(hideRunnable)
73
+ handler.removeCallbacks(playRunnable)
74
+ if (binding.content.text.isEmpty()) {
75
+ binding.content.text = channel
76
+ view?.visibility = View .VISIBLE
77
+ handler.postDelayed(playRunnable, delay)
78
+ } else {
79
+ binding.content.text = " ${binding.content.text}$channel "
80
+ handler.postDelayed(playRunnable, 0 )
81
+ }
70
82
}
71
83
}
72
84
@@ -84,17 +96,19 @@ class ChannelFragment : Fragment() {
84
96
}
85
97
86
98
private val hideRunnable = Runnable {
87
- binding.content.text = " "
99
+ binding? .content? .text = " "
88
100
view?.visibility = View .GONE
89
101
}
90
102
91
103
private val playRunnable = Runnable {
92
- (activity as MainActivity ).play(channel - 1 )
104
+ (activity as ? MainActivity )? .play(channel - 1 )
93
105
handler.postDelayed(hideRunnable, delay)
94
106
}
95
107
96
108
override fun onDestroyView () {
97
109
super .onDestroyView()
110
+ handler.removeCallbacks(hideRunnable)
111
+ handler.removeCallbacks(playRunnable)
98
112
_binding = null
99
113
}
100
114
0 commit comments