HomeConfideView.kt 6.58 KB
Newer Older
徐健 committed
1 2 3 4 5 6 7 8 9
package com.yidianling.home.ui.view

import android.content.Context
import android.graphics.Typeface
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
YKai committed
10 11 12
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.tabs.TabLayout
万齐军 committed
13
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
14
import com.yidianling.common.tools.LogUtil
徐健 committed
15
import com.yidianling.home.R
16
import com.yidianling.home.constract.HomeViewConfig
17
import com.yidianling.home.event.IHomeBaseEvent
徐健 committed
18 19
import com.yidianling.home.model.bean.HomeConfideBean
import com.yidianling.home.model.bean.HomeHeaderBean
konghaorui committed
20
import kotlinx.android.synthetic.ydl.home_confide_view.view.*
徐健 committed
21 22 23 24 25 26 27 28

/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述: 倾诉*排解模块
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/02/13
 */
29
class HomeConfideView(private val mContext: Context, private var homeEvent: IHomeBaseEvent?) : LinearLayout(mContext) {
徐健 committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    private var mLastPosition: Int = 0
    /**
     * 专家信息view缓存list
     */
    private var cacheInfoViewList: ArrayList<HomeConfideExpertInfoView>? = null
    /**
     * TabLayout.OnTabSelectedListener
     */
    private var listener: ConfideExpertTabSelectedListener? = null

    init {
        initView()
    }

    private fun initView() {
        val params = RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        layoutParams = params
        orientation = VERTICAL
konghaorui committed
48
        View.inflate(mContext, R.layout.home_confide_view, this)
49
        homeModuleConfideViewHomeCommonTitleView.setTitle(HomeViewConfig.getOrder().confideTitle)
徐健 committed
50
        homeModuleConfideViewHomeCommonTitleView.setOnClickListener {
万齐军 committed
51
            ActionCountUtils.record("listen_counselor_list_page", "listen_counselor_list_page_visit", "2")
徐健 committed
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
            homeEvent?.confideMoreClick()
        }
    }

    /**
     * 创建tab栏
     * @param lastPosition 上次选中的下标
     */
    fun setTitle(list: List<HomeHeaderBean.ListenCategoryDateBean>?, 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()) {
            tab_layout.addTab(tab_layout.newTab().setText(bean.name), index == lastPosition)
        }
        listener = ConfideExpertTabSelectedListener(list, tab_layout)
        tab_layout.addOnTabSelectedListener(listener!!)
        var textView = ((tab_layout!!.getChildAt(0) as LinearLayout).getChildAt(lastPosition) as LinearLayout).getChildAt(1)
        if (textView != null && textView is TextView) {
            textView.textSize = 17f
徐健 committed
78
            textView.setTextColor(ContextCompat.getColor(mContext, R.color.platform_color_242424))
徐健 committed
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 119 120 121 122
            textView.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        }
        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 setConfideExpertInfoView(list: List<HomeConfideBean.BodyBean>?) {
        if (list == null || list.isEmpty()) {
            ll_content.visibility = View.GONE
            return
        }
        ll_content.visibility = View.VISIBLE
        ll_content.removeAllViews()
        if (cacheInfoViewList == null) {
            cacheInfoViewList = ArrayList()
        }

        //创建HomeConfideExpertInfoView:每个tab最多显示三个专家
        if (cacheInfoViewList!!.size >= list.size) {
            for (index in 0 until list!!.size) {
                //设置数据
                cacheInfoViewList!![index].setData(list[index]!!, index, index == list!!.size - 1)
                //添加到布局
                ll_content.addView(cacheInfoViewList!![index])
            }
        } else {
            for (index in 0 until list!!.size) {
                if (index > 2) {
                    //只取前三条数据
                    break
                }
                //数据大于缓存view数量,创建view
                if (index > cacheInfoViewList!!.size - 1) {
                    var homeConfideExpertInfoView = createExpertInfoView()
                    cacheInfoViewList!!.add(homeConfideExpertInfoView)
                }
                //设置数据
YKai committed
123
                cacheInfoViewList!![index].setData(list[index], index, index == list!!.size - 1)
徐健 committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
                //添加到布局
                ll_content.addView(cacheInfoViewList!![index])
            }
        }
    }

    /**
     * 创建专家信息view
     */
    private fun createExpertInfoView(): HomeConfideExpertInfoView {
        return HomeConfideExpertInfoView(mContext, homeEvent)
    }


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

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

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

        override fun onTabUnselected(tab: TabLayout.Tab?) {
            var textView = ((tabLayout!!.getChildAt(0) as LinearLayout).getChildAt(tab!!.position) as LinearLayout).getChildAt(1)
            if (textView != null && textView is TextView) {
                textView.textSize = 15f
徐健 committed
154
                textView.setTextColor(ContextCompat.getColor(mContext, R.color.platform_color_333333))
徐健 committed
155 156 157 158 159 160 161
            }
        }

        override fun onTabSelected(tab: TabLayout.Tab?) {
            if (mLastPosition == tab!!.position){
                return
            }
YKai committed
162
            var textView = ((tabLayout!!.getChildAt(0) as LinearLayout).getChildAt(tab.position) as LinearLayout).getChildAt(1)
徐健 committed
163 164
            if (textView != null && textView is TextView) {
                textView.textSize = 17f
徐健 committed
165
                textView.setTextColor(ContextCompat.getColor(mContext, R.color.platform_color_242424))
徐健 committed
166 167 168
                textView.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
            }

YKai committed
169
            homeEvent!!.getConfideData(list!![tab.position], tab.position)
徐健 committed
170 171 172
        }
    }
}