HomeConsultView.kt 7.95 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
import android.graphics.Typeface
import android.os.Build
YKai committed
7 8 9
import com.google.android.material.tabs.TabLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
10
import android.text.Layout
11 12 13 14 15
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.yidianling.common.tools.LogUtil
16
import com.yidianling.common.tools.RxDeviceTool
ydl committed
17
import com.yidianling.common.tools.RxImageTool
18 19 20 21
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
konghaorui committed
22
import kotlinx.android.synthetic.xlzx.home_confide_view.view.*
ydl committed
23
import java.lang.Exception
24
import kotlin.math.ceil
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

/**
 * @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
45
    private var dp5: Int = 0
ydl committed
46
    private var dp6: Int = 0
ydl committed
47
    private var dp7: Int = 0
ydl committed
48
    private var dp10: Int = 0
49
    private var dp16: Int = 0
ydl committed
50

51 52 53 54 55
    init {
        initView()
    }

    private fun initView() {
ydl committed
56
        dp5 = RxImageTool.dp2px(5f)
ydl committed
57
        dp6 = RxImageTool.dp2px(6f)
ydl committed
58
        dp7 = RxImageTool.dp2px(7f)
ydl committed
59
        dp10 = RxImageTool.dp2px(10f)
60
        dp16 = RxImageTool.dp2px(16f)
ydl committed
61

62 63 64 65 66 67
        val params = RecyclerView.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        layoutParams = params
        orientation = VERTICAL
konghaorui committed
68
        View.inflate(mContext, R.layout.home_confide_view, this)
ydl committed
69 70
        homeModuleConfideViewHomeCommonTitleView.setTitle("心理咨询")
        homeModuleConfideViewHomeCommonTitleView.setMoreText("更多咨询")
ydl committed
71
        homeModuleConfideViewHomeCommonTitleView.setTopMargin(-4)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        homeModuleConfideViewHomeCommonTitleView.setOnClickListener {
            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
92 93 94 95 96 97 98

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

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

            tab_layout.addTab(newTab, index == lastPosition)
99 100 101
        }
        listener = ConsultTabSelectedListener(list, tab_layout)
        tab_layout.addOnTabSelectedListener(listener!!)
ydl committed
102 103
        try {
            var textView = tab_layout.getTabAt(0) as TextView
104
            updateText(textView, true)
ydl committed
105
        } catch (e: Exception) {}
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        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
131
        //创建HomeConsultItemView:每个tab最多显示四个专家
132
        if (cacheInfoViewList!!.size >= list.size) {
133
            for (index in list.indices) {
134 135
                //设置数据
                cacheInfoViewList!![index].setData(
136
                    list[index],
137
                    index,
138
                    index == list.size - 1,
139 140
                    "${category?.id}"
                )
141
                cacheInfoViewList!![index].layoutParams = getViewLayoutParams(index)
142 143 144 145
                //添加到布局
                ll_content.addView(cacheInfoViewList!![index])
            }
        } else {
146
            for (index in list.indices) {
ydl committed
147
                if (index > 3) {
ydl committed
148
                    //只取前四条数据
149 150 151 152
                    break
                }
                //数据大于缓存view数量,创建view
                if (index > cacheInfoViewList!!.size - 1) {
153 154
                    var mHomeConsultItemView = createConsultInfoView()
                    cacheInfoViewList!!.add(mHomeConsultItemView)
155 156 157
                }
                //设置数据
                cacheInfoViewList!![index].setData(
158
                    list[index],
159
                    index,
160
                    index == list.size - 1,
161 162
                    "${category?.id}"
                )
163
                cacheInfoViewList!![index].layoutParams = getViewLayoutParams(index)
164 165 166 167 168 169
                //添加到布局
                ll_content.addView(cacheInfoViewList!![index])
            }
        }
    }

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

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    /**
     * 创建专家信息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
203 204
            var textView = tabLayout?.getTabAt(tab?.position!!)?.customView as TextView

205
            updateText(textView, false)
206 207 208 209 210 211
        }

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

214 215
            updateText(textView, true)
            homeEvent!!.getConsultData(list!![tab.position], tab.position)
216 217
        }
    }
ydl committed
218 219 220 221 222

    fun updateText(txtView: TextView, isSelected: Boolean): TextView {
        if (!isSelected) {
            txtView.textSize = 12f
            txtView.setTextColor(Color.parseColor("#999999"))
ydl committed
223
            txtView.setPadding(dp10, dp5, dp10, dp7)
ydl committed
224 225 226 227
            txtView.setBackgroundResource(R.drawable.home_consult_tab_item_unselected_bg)
        } else {
            txtView.textSize = 12f
            txtView.setTextColor(Color.parseColor("#FD9B0A"))
ydl committed
228
            txtView.setPadding(dp10, dp5, dp10, dp7)
ydl committed
229 230 231 232 233
            txtView.setBackgroundResource(R.drawable.home_consult_tab_item_selected_bg)

        }
        return txtView
    }
234
}