左晓为主开发手持机充值管理机
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.dayu.general.activity
 
import android.os.Bundle
import android.view.LayoutInflater
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.dayu.general.R
import com.dayu.general.adapter.TabAdapter
import com.dayu.general.databinding.ActivityMainBinding
 
class MainActivity : BaseActivity() {
 
    var binding: ActivityMainBinding? = null
    private val fragments: ArrayList<Fragment> = ArrayList()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(LayoutInflater.from(this))
        setContentView(binding?.root)
        setupFragments()
        initView()
        initTab()
 
 
    }
 
 
    private fun initView() {
        binding!!.BSCardLL.setOnClickListener { v -> changeBottomState(Tab.BSC) }
        binding!!.rechargeLL.setOnClickListener { v -> changeBottomState(Tab.RECHARGE) }
        binding!!.myLL.setOnClickListener { v -> changeBottomState(Tab.MY) }
    }
 
 
    private fun setupFragments() {
        fragments.add(BSCardFragment())
        fragments.add(RechargeFragment())
        fragments.add(MyFragment())
    }
 
    private fun initTab() {
        binding?.viewPager?.adapter = TabAdapter(this, fragments)
        binding?.viewPager?.currentItem = (1)
        binding?.viewPager?.offscreenPageLimit = 3
        binding?.viewPager?.isUserInputEnabled = false
    }
 
    private enum class Tab {
        BSC, RECHARGE, MY
    }
 
    /**
     * 修改底部状态
     */
    private fun changeBottomState(tab: Tab) {
        resetTabState()
        when (tab) {
            Tab.BSC -> updateTabUI(0, R.drawable.bottom_card_white, R.color.white)
            Tab.RECHARGE -> updateTabUI(1, R.drawable.bottom_recharge_white, R.color.white)
            Tab.MY -> updateTabUI(2, R.drawable.bottom_my_white, R.color.white)
        }
    }
 
    /**
     * 重置所有 Tab 的默认状态
     */
    private fun resetTabState() {
        binding!!.BSCardImg.setImageDrawable(
            ContextCompat.getDrawable(
                this,
                R.drawable.bottom_card_black
            )
        )
        binding!!.BSCardText.setTextColor(ContextCompat.getColor(this, R.color.black))
 
        binding!!.rechargeImg.setImageDrawable(
            ContextCompat.getDrawable(
                this,
                R.drawable.bottom_recharge_black
            )
        )
        binding!!.rechargeText.setTextColor(ContextCompat.getColor(this, R.color.black))
 
        binding!!.myImg.setImageDrawable(
            ContextCompat.getDrawable(
                this,
                R.drawable.bottom_my_black
            )
        )
        binding!!.myText.setTextColor(ContextCompat.getColor(this, R.color.black))
    }
 
    /**
     * 更新某个 Tab 的 UI 状态
     */
    private fun updateTabUI(position: Int, iconResId: Int, textColorResId: Int) {
        if (position == 1) {
            binding!!.viewPager.setCurrentItem(position, true)
        } else {
            binding!!.viewPager.setCurrentItem(position, false)
        }
        when (position) {
            0 -> {
                binding!!.BSCardImg.setImageDrawable(ContextCompat.getDrawable(this, iconResId))
                binding!!.BSCardText.setTextColor(ContextCompat.getColor(this, textColorResId))
            }
 
            1 -> {
                binding!!.rechargeImg.setImageDrawable(ContextCompat.getDrawable(this, iconResId))
                binding!!.rechargeText.setTextColor(ContextCompat.getColor(this, textColorResId))
            }
 
            2 -> {
                binding!!.myImg.setImageDrawable(ContextCompat.getDrawable(this, iconResId))
                binding!!.myText.setTextColor(ContextCompat.getColor(this, textColorResId))
            }
        }
    }
}