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

import android.content.Context
import android.graphics.Typeface
import android.os.Build
import android.support.design.widget.TabLayout
import android.support.v4.content.ContextCompat
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
13
import com.yidianling.common.tools.LogUtil
徐健 committed
14
import com.yidianling.home.R
15
import com.yidianling.home.constract.HomeViewConfig
16
import com.yidianling.home.event.IHomeBaseEvent
徐健 committed
17 18
import com.yidianling.home.model.bean.HomeConfideBean
import com.yidianling.home.model.bean.HomeHeaderBean
konghaorui committed
19
import kotlinx.android.synthetic.ydl.home_confide_view.view.*
徐健 committed
20 21 22 23 24 25 26 27

/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述: 倾诉*排解模块
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/02/13
 */
28
class HomeConfideView(private val mContext: Context, private var homeEvent: IHomeBaseEvent?) : LinearLayout(mContext) {
徐健 committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    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
47
        View.inflate(mContext, R.layout.home_confide_view, this)
48
        homeModuleConfideViewHomeCommonTitleView.setTitle(HomeViewConfig.getOrder().confideTitle)
徐健 committed
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
        homeModuleConfideViewHomeCommonTitleView.setOnClickListener {
            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
76
            textView.setTextColor(ContextCompat.getColor(mContext, R.color.platform_color_242424))
徐健 committed
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 119 120 121 122 123 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
            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)
                }
                //设置数据
                cacheInfoViewList!![index].setData(list[index]!!, index, index == list!!.size - 1)
                //添加到布局
                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
152
                textView.setTextColor(ContextCompat.getColor(mContext, R.color.platform_color_333333))
徐健 committed
153 154 155 156 157 158 159 160 161 162
            }
        }

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

            homeEvent!!.getConfideData(list!![tab!!.position], tab!!.position)
        }
    }
}