日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 多个标签页,Android一个标签页的实现

發布時間:2025/3/19 Android 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 多个标签页,Android一个标签页的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近要實現一個類似于下面的頁面

想著每次都要自己重新寫,太麻煩了,這里記錄一下自己寫的自定義的view。一開始本來準備用ConstraintLayout的,但是這貨addview的時候總有bug,就用RelativeLayout了。

package com.android.demo.view

import android.content.Context

import android.util.AttributeSet

import android.view.View

import android.widget.RelativeLayout

import android.widget.TextView

import androidx.core.view.doOnLayout

import com.android.demo.R

import com.bigkoo.convenientbanner.utils.ScreenUtil

/**

* Created by JayChou on 2020/10/30.

*/

class TagView : RelativeLayout {

constructor(context: Context) : super(context) {

initView(context)

}

constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {

initView(context)

}

constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(

context,

attrs,

defStyleAttr

) {

initView(context)

}

private lateinit var defaultTextView: TextView //添加一個空白的view,用來精準測量每個tag的實際大小

private val tags: MutableList = mutableListOf()

private val views: MutableList = mutableListOf()

private var lastViewId = -1

private var index = 0

var click: ((String) -> Unit)? = null

private fun initView(context: Context) {

val layoutParams =

LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)

defaultTextView = getTextView("")

defaultTextView.visibility = View.INVISIBLE

addView(defaultTextView, layoutParams)

}

//這里是用來提供tag的樣式,替換你自己的樣式

private fun getTextView(tag: String): TextView {

val textView = TextView(context)

textView.id = View.generateViewId()

textView.text = tag

// textView.setBackgroundResource(R.drawable.shape_f4f4f4_6)

// textView.setTextColor(context.resources.getColor(R.color.greyishBrownThree))

textView.setBackgroundResource(R.drawable.shape_bright_red_6)

textView.setTextColor(context.resources.getColor(R.color.white))

textView.textSize = 14f

textView.setPadding(

ScreenUtil.dip2px(context, 10f),

ScreenUtil.dip2px(context, 5f),

ScreenUtil.dip2px(context, 10f),

ScreenUtil.dip2px(context, 5f)

)

return textView

}

//添加標簽

fun setTags(list: MutableList) {

clearAllViews()

this.tags.addAll(list)

addTextView()

}

fun addTags(list: MutableList) {

if (list.isNotEmpty()) {

index = this.tags.size

this.tags.addAll(list)

addTextView()

}

}

private fun addTextView() {

if (index < 0 || index >= tags.size) {

return

}

post {

val tag = tags[index]

defaultTextView.text = tag

defaultTextView.doOnLayout {

// L.i(" defaultTextView.measuredWidth $tag ${defaultTextView.measuredWidth}")

val textView = getTextView(tag)

val layoutParams =

LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)

if (lastViewId == -1) {

layoutParams.addRule(ALIGN_PARENT_TOP)

layoutParams.addRule(ALIGN_PARENT_START)

} else {

val location: IntArray = intArrayOf(0, 0)

views[index - 1].getLocationOnScreen(location)

// L.i(" lastViewId $lastViewId ${measuredWidth - location[0] - views[index - 1].measuredWidth} ")

if (measuredWidth - location[0] - views[index - 1].measuredWidth > defaultTextView.measuredWidth) {

layoutParams.marginStart = ScreenUtil.dip2px(context, 10f)

layoutParams.addRule(END_OF, lastViewId)

layoutParams.addRule(ALIGN_TOP, lastViewId)

layoutParams.addRule(ALIGN_BOTTOM, lastViewId)

} else {

layoutParams.topMargin = ScreenUtil.dip2px(context, 10f)

layoutParams.addRule(ALIGN_PARENT_START)

layoutParams.addRule(BELOW, lastViewId)

}

}

lastViewId = textView.id

// L.i(" tag ${textView.id} ")

views.add(textView)

addView(textView, layoutParams)

textView.requestLayout()

requestLayout()

textView.setOnClickListener {

click?.invoke(tag)

}

textView.doOnLayout {

index += 1

addTextView()

}

}

}

}

//清除所有的tag

fun clearAllViews() {

views.forEach {

removeView(it)

}

views.clear()

tags.clear()

lastViewId = -1

index = 0

}

}

然后使用的時候,直接setTags就行了

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的android 多个标签页,Android一个标签页的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。