HomeConsultView.kt 7.77 KB
Newer Older
1 2 3
package com.yidianling.home.ui.view

import android.content.Context
ydl committed
4
import android.graphics.Color
5 6 7 8 9 10
import android.graphics.Typeface
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
11 12
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.tabs.TabLayout
13
import com.yidianling.common.tools.LogUtil
14
import com.yidianling.common.tools.RxDeviceTool
ydl committed
15
import com.yidianling.common.tools.RxImageTool
16 17 18 19
import com.yidianling.home.R
import com.yidianling.home.event.IHomeBaseEvent
import com.yidianling.home.model.bean.HomeConsultBean
import com.yidianling.home.model.bean.HomeHeaderBean
20
import kotlinx.android.synthetic.xlzx.home_consult_view.view.*
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述: 咨询理解模块
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/02/13
 */
class HomeConsultView(private val mContext: Context, private var homeEvent: IHomeBaseEvent?) :
    LinearLayout(mContext) {
    private var mLastPosition: Int = 0
    /**
     * 专家信息view缓存list
     */
    private var cacheInfoViewList: ArrayList<HomeConsultItemView>? = null
    /**
     * TabLayout.OnTabSelectedListener
     */
    private var listener: ConsultTabSelectedListener? = null

ydl committed
41
    private var dp5: Int = 0
ydl committed
42
    private var dp6: Int = 0
ydl committed
43
    private var dp7: Int = 0
ydl committed
44
    private var dp10: Int = 0
45
    private var dp16: Int = 0
ydl committed
46

47 48 49 50 51
    init {
        initView()
    }

    private fun initView() {
ydl committed
52
        dp5 = RxImageTool.dp2px(5f)
ydl committed
53
        dp6 = RxImageTool.dp2px(6f)
ydl committed
54
        dp7 = RxImageTool.dp2px(7f)
ydl committed
55
        dp10 = RxImageTool.dp2px(10f)
56
        dp16 = RxImageTool.dp2px(16f)
ydl committed
57

58 59 60 61 62 63
        val params = RecyclerView.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        layoutParams = params
        orientation = VERTICAL
64 65 66 67 68
        View.inflate(mContext, R.layout.home_consult_view, this)
        homeModuleConfideViewHomeCommonFullTitleView.setTitle("心理咨询")
        homeModuleConfideViewHomeCommonFullTitleView.setMoreText("更多")
        homeModuleConfideViewHomeCommonFullTitleView.setTopMargin(-4)
        homeModuleConfideViewHomeCommonFullTitleView.setOnClickListener {
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
            homeEvent?.consultMoreClick()
        }
    }

    /**
     * 创建tab栏
     * @param lastPosition 上次选中的下标
     */
    fun setTitle(list: List<HomeHeaderBean.ConsultCategoryDateBean>?, lastPosition: Int) {
        if (list == null || list.isEmpty()) {
            visibility = View.GONE
            return
        }
        if (listener != null) {
            tab_layout.removeOnTabSelectedListener(listener!!)
        }
        mLastPosition = lastPosition
        tab_layout.removeAllTabs()
        for ((index, bean) in list.withIndex()) {
ydl committed
88 89 90 91 92 93 94

            val textView = updateText(TextView(mContext), index == lastPosition)
            textView.text = bean.name

            val newTab = tab_layout.newTab().setCustomView(textView)

            tab_layout.addTab(newTab, index == lastPosition)
95 96 97
        }
        listener = ConsultTabSelectedListener(list, tab_layout)
        tab_layout.addOnTabSelectedListener(listener!!)
ydl committed
98 99
        try {
            var textView = tab_layout.getTabAt(0) as TextView
100
            updateText(textView, true)
ydl committed
101
        } catch (e: Exception) {}
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            tab_layout.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
                LogUtil.e("scrollX=$scrollX")
                LogUtil.e("oldScrollX=$oldScrollX")
            }
        }
    }

    /**
     * 设置数据
     */
    fun setConsultInfoView(
        list: List<HomeConsultBean.ListBean>?,
        category: HomeHeaderBean.ConsultCategoryDateBean?
    ) {
        if (list == null || list.isEmpty()) {
            ll_content.visibility = View.GONE
            return
        }
        ll_content.visibility = View.VISIBLE
        ll_content.removeAllViews()
        if (cacheInfoViewList == null) {
            cacheInfoViewList = ArrayList()
        }

ydl committed
127
        //创建HomeConsultItemView:每个tab最多显示四个专家
128
        if (cacheInfoViewList!!.size >= list.size) {
129
            for (index in list.indices) {
130 131
                //设置数据
                cacheInfoViewList!![index].setData(
132
                    list[index],
133
                    index,
134
                    index == list.size - 1,
135 136
                    "${category?.id}"
                )
137
                cacheInfoViewList!![index].layoutParams = getViewLayoutParams(index)
138 139 140 141
                //添加到布局
                ll_content.addView(cacheInfoViewList!![index])
            }
        } else {
142
            for (index in list.indices) {
ydl committed
143
                if (index > 3) {
ydl committed
144
                    //只取前四条数据
145 146 147 148
                    break
                }
                //数据大于缓存view数量,创建view
                if (index > cacheInfoViewList!!.size - 1) {
149 150
                    var mHomeConsultItemView = createConsultInfoView()
                    cacheInfoViewList!!.add(mHomeConsultItemView)
151 152 153
                }
                //设置数据
                cacheInfoViewList!![index].setData(
154
                    list[index],
155
                    index,
156
                    index == list.size - 1,
157 158
                    "${category?.id}"
                )
159
                cacheInfoViewList!![index].layoutParams = getViewLayoutParams(index)
160 161 162 163 164 165
                //添加到布局
                ll_content.addView(cacheInfoViewList!![index])
            }
        }
    }

166
    fun getViewLayoutParams(index: Int): MarginLayoutParams {
ydl committed
167
        var itemWidth: Int = (RxDeviceTool.getScreenWidth(mContext) - dp10 - dp10) / 2
168
        var itemHeight: Int = (itemWidth * 187) / 166
ydl committed
169
        var topMargin = if (index == 2 || index == 3) itemHeight else 0
170
        val params = LayoutParams(itemWidth, itemHeight) as MarginLayoutParams
ydl committed
171
        params.setMargins(dp10 + (itemWidth) * ((index) % 2),
172 173 174 175 176 177
            topMargin,
            0,0
            )
        return params
    }

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    /**
     * 创建专家信息view
     */
    private fun createConsultInfoView(): HomeConsultItemView {
        return HomeConsultItemView(mContext, homeEvent)
    }


    inner class ConsultTabSelectedListener : TabLayout.OnTabSelectedListener {
        private var list: List<HomeHeaderBean.ConsultCategoryDateBean>? = null
        private var tabLayout: TabLayout? = null

        constructor(list: List<HomeHeaderBean.ConsultCategoryDateBean>, tabLayout: TabLayout) {
            this.list = list
            this.tabLayout = tabLayout
        }

        override fun onTabReselected(tab: TabLayout.Tab?) {
        }

        override fun onTabUnselected(tab: TabLayout.Tab?) {
ydl committed
199 200
            var textView = tabLayout?.getTabAt(tab?.position!!)?.customView as TextView

201
            updateText(textView, false)
202 203 204 205 206 207
        }

        override fun onTabSelected(tab: TabLayout.Tab?) {
            if (mLastPosition == tab!!.position) {
                return
            }
208
            var textView = tabLayout?.getTabAt(tab.position)?.customView as TextView
ydl committed
209

210 211
            updateText(textView, true)
            homeEvent!!.getConsultData(list!![tab.position], tab.position)
212 213
        }
    }
ydl committed
214 215 216 217

    fun updateText(txtView: TextView, isSelected: Boolean): TextView {
        if (!isSelected) {
            txtView.textSize = 12f
218 219
            txtView.setTextColor(Color.parseColor("#929292"))
            txtView.typeface = Typeface.DEFAULT
ydl committed
220
            txtView.setPadding(dp10, dp5, dp10, dp7)
ydl committed
221
        } else {
222 223
            txtView.textSize = 14f
            txtView.setTextColor(Color.parseColor("#5E5E5E"))
224
            txtView.typeface = Typeface.DEFAULT_BOLD
ydl committed
225
            txtView.setPadding(dp10, dp5, dp10, dp7)
ydl committed
226 227 228
        }
        return txtView
    }
229
}