| | |
| | | import android.annotation.SuppressLint |
| | | import android.net.Uri |
| | | import android.os.Bundle |
| | | import android.text.StaticLayout |
| | | import android.text.TextPaint |
| | | import android.text.TextUtils |
| | | import android.util.TypedValue |
| | | import android.view.Gravity |
| | | import android.view.View |
| | | import android.view.ViewGroup |
| | | import android.view.WindowManager |
| | | import android.widget.LinearLayout |
| | | import android.widget.TextView |
| | | import androidx.core.os.bundleOf |
| | | import androidx.core.view.isVisible |
| | | import androidx.databinding.DataBindingUtil |
| | | import androidx.recyclerview.widget.GridLayoutManager |
| | | import androidx.recyclerview.widget.LinearLayoutManager |
| | |
| | | import com.xw.yunwei.constants.Key |
| | | import com.xw.yunwei.databinding.ActivityAccidentProcessBinding |
| | | import com.xw.yunwei.databinding.DialogAccidentFilterBinding |
| | | import com.xw.yunwei.extension.colorIdToColorValue |
| | | import com.xw.yunwei.extension.dpToPx |
| | | import com.xw.yunwei.extension.spToPx |
| | | import com.xw.yunwei.model.TaskAccidentModel |
| | | import com.xw.yunwei.ui.task.create.LeftCategoryAdapter |
| | | import com.xw.yunwei.ui.task.create.RightItemAdapter |
| | | import com.xw.yunwei.utils.DialogFactory |
| | | import com.xw.yunwei.utils.PictureSelectorFactory |
| | | import kotlin.math.ceil |
| | | |
| | | /** |
| | | * 事故处理页面 |
| | |
| | | |
| | | /** 右侧事故子类 Adapter */ |
| | | private lateinit var rightAdapter: RightItemAdapter |
| | | |
| | | //用于测量 |
| | | private val textPaint = TextPaint() |
| | | |
| | | companion object { |
| | | //获取跳转该页面参数 |
| | |
| | | resources.getColor(R.color.white), |
| | | "shape_blue_5_bg_4" |
| | | ) |
| | | viewModel.getTypeList { |
| | | binding.tvAccidentFilter.text = |
| | | if (viewModel.getSelectType().isEmpty()) "" else "选好了" |
| | | } |
| | | viewModel.getTypeList { refreshFilterData() } |
| | | } |
| | | |
| | | override fun setListener() { |
| | | super.setListener() |
| | | binding.llFilters.setOnClickListener { |
| | | viewModel.getTypeList { |
| | | showFilterDialog() |
| | | } |
| | | } |
| | | // 点击事故类型筛选按钮,先拉取类型列表再弹窗 |
| | | binding.tvAccidentFilter.setOnClickListener { |
| | | viewModel.getTypeList { |
| | |
| | | } |
| | | |
| | | // 弹窗关闭时更新筛选按钮文案(已选则显示"选好了") |
| | | filterDialog!!.setOnDismissListener { |
| | | binding.tvAccidentFilter.text = |
| | | if (viewModel.getSelectType().isEmpty()) "" else "选好了" |
| | | } |
| | | filterDialog!!.setOnDismissListener { refreshFilterData() } |
| | | } |
| | | |
| | | filterDialog?.show() |
| | |
| | | rightAdapter = RightItemAdapter(viewModel.categories[0].items) |
| | | bind.rightRecyclerView.adapter = rightAdapter |
| | | } |
| | | |
| | | //刷新筛选数据 |
| | | private fun refreshFilterData() { |
| | | val selectTypeName = viewModel.getSelectTypeName() |
| | | binding.llFilters.isVisible = selectTypeName.isNotEmpty() |
| | | binding.tvAccidentFilter.isVisible = selectTypeName.isEmpty() |
| | | binding.llFilters.removeAllViews() |
| | | if (selectTypeName.isNotEmpty()) { |
| | | binding.llFilters.post { |
| | | fill(binding.llFilters, binding.llFilters.width, selectTypeName) |
| | | } |
| | | } |
| | | } |
| | | |
| | | //填充数据 |
| | | private fun fill(parent: LinearLayout, parenWidth: Int, list: List<String>) { |
| | | val itemWidths = list.map { calculationItemViewWidth(it, 8, 4) } |
| | | val pair = findWrapIndex(parenWidth, itemWidths) |
| | | val numItemWidth = if (pair.second) { |
| | | calculationItemViewWidth("+${list.size - pair.first - 1}", 4, 0) |
| | | } else { |
| | | 0 |
| | | } |
| | | val needWeight = |
| | | pair.second && pair.first == 0 && itemWidths.first() >= (parenWidth - numItemWidth) |
| | | list.subList(0, pair.first + 1).forEach { |
| | | parent.addView(getFilterItemView(it, 8, 4, if (needWeight) 1f else 0f)) |
| | | } |
| | | if (pair.second) { |
| | | parent.addView(getFilterItemView("+${list.size - pair.first - 1}", 4, 0)) |
| | | } |
| | | } |
| | | |
| | | //查找合适的下标 |
| | | private fun findWrapIndex( |
| | | parenWidth: Int, |
| | | list: List<Int>, |
| | | needLastNum: Boolean = true |
| | | ): Pair<Int, Boolean> { |
| | | val size = list.size |
| | | var itemWidthCount = 0 |
| | | for ((index, itemWidth) in list.withIndex()) { |
| | | if (itemWidthCount + itemWidth == parenWidth) { |
| | | return Pair(index, false) |
| | | } |
| | | if (itemWidthCount + itemWidth > parenWidth) { |
| | | if (needLastNum) { |
| | | if (index == 0) { |
| | | return Pair(index, size > 1) |
| | | } |
| | | //末尾的计数 |
| | | val numberItemWidth = |
| | | calculationItemViewWidth("+${size - index}", 4, 0) |
| | | //剩余内容宽度 |
| | | val surplusContentWidth = parenWidth - numberItemWidth |
| | | //将剩余内容宽度带入,重新找适合的下标 |
| | | val wrapIndex = findWrapIndex(surplusContentWidth, list, false) |
| | | return Pair(wrapIndex.first, true) |
| | | } else { |
| | | return Pair(index - 1, false) |
| | | } |
| | | } |
| | | itemWidthCount += itemWidth |
| | | } |
| | | return Pair(size - 1, false) |
| | | } |
| | | |
| | | //计算item控件宽度 |
| | | private fun calculationItemViewWidth(item: String, padHorizontal: Int, marginEnd: Int): Int { |
| | | textPaint.textSize = 12f.spToPx(this@AccidentProcessActivity) |
| | | val sl = StaticLayout.Builder.obtain(item, 0, item.length, textPaint, Int.MAX_VALUE) |
| | | .setIncludePad(false) // 看你期望要不要 TextView 默认那套 pad |
| | | .build() |
| | | |
| | | val line0Width = sl.getLineWidth(0) // 这一行的“排版宽度” |
| | | val padWidth = padHorizontal.dpToPx(this) * 2//两侧内间距宽度 |
| | | return ceil(line0Width + padWidth + marginEnd.dpToPx(this)).toInt() |
| | | } |
| | | |
| | | //获取筛选item控件 |
| | | private fun getFilterItemView( |
| | | item: String, |
| | | padHorizontal: Int, |
| | | marginEnd: Int, |
| | | weight: Float = 0f, |
| | | ): View { |
| | | return TextView(this).apply { |
| | | setBackgroundResource(com.xw.yunwei.R.drawable.shape_gray11_bg_4) |
| | | layoutParams = LinearLayout.LayoutParams( |
| | | if (weight > 0) 0 else LinearLayout.LayoutParams.WRAP_CONTENT, |
| | | LinearLayout.LayoutParams.WRAP_CONTENT, |
| | | weight |
| | | ).apply { |
| | | this.marginEnd = marginEnd.dpToPx(this@AccidentProcessActivity) |
| | | } |
| | | setPadding( |
| | | padHorizontal.dpToPx(this@AccidentProcessActivity), |
| | | 4.dpToPx(this@AccidentProcessActivity), |
| | | padHorizontal.dpToPx(this@AccidentProcessActivity), |
| | | 4.dpToPx(this@AccidentProcessActivity), |
| | | ) |
| | | setTextColor(R.color.gray_9.colorIdToColorValue(this@AccidentProcessActivity)) |
| | | setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f) |
| | | text = item |
| | | maxLines = 1 |
| | | ellipsize = TextUtils.TruncateAt.END |
| | | } |
| | | } |
| | | } |