HomeConsultView.kt 7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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
import com.yidianling.common.tools.LogUtil
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
18
import kotlinx.android.synthetic.xlzx.home_confide_view.view.*
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

/**
 * @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

    init {
        initView()
    }

    private fun initView() {
        val params = RecyclerView.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        layoutParams = params
        orientation = VERTICAL
konghaorui committed
50
        View.inflate(mContext, R.layout.home_confide_view, this)
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 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        homeModuleConfideViewHomeCommonTitleView.setTitle("咨询·理解")
        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()) {
            tab_layout.addTab(tab_layout.newTab().setText(bean.name), index == lastPosition)
        }
        listener = ConsultTabSelectedListener(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 = 26f
            textView.setTextColor(ContextCompat.getColor(mContext, R.color.platform_color_242424))
            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 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()
        }

        //创建HomeConsultItemView:每个tab最多显示三个专家
        if (cacheInfoViewList!!.size >= list.size) {
            for (index in 0 until list!!.size) {
                //设置数据
                cacheInfoViewList!![index].setData(
                    list[index]!!,
                    index,
                    index == list!!.size - 1,
                    "${category?.id}"
                )
                //添加到布局
                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 HomeConsultItemView = createConsultInfoView()
                    cacheInfoViewList!!.add(HomeConsultItemView)
                }
                //设置数据
                cacheInfoViewList!![index].setData(
                    list[index]!!,
                    index,
                    index == list!!.size - 1,
                    "${category?.id}"
                )
                //添加到布局
                ll_content.addView(cacheInfoViewList!![index])
            }
        }
    }

    /**
     * 创建专家信息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?) {
            var textView =
                ((tabLayout!!.getChildAt(0) as LinearLayout).getChildAt(tab!!.position) as LinearLayout).getChildAt(
                    1
                )
            if (textView != null && textView is TextView) {
                textView.textSize = 14f
                textView.setTextColor(
                    ContextCompat.getColor(
                        mContext,
                        R.color.platform_color_333333
                    )
                )
            }
        }

        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 = 26f
                textView.setTextColor(
                    ContextCompat.getColor(
                        mContext,
                        R.color.platform_color_242424
                    )
                )
                textView.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
            }
            homeEvent!!.getConsultData(list!![tab!!.position], tab!!.position)
        }
    }
}