Android自定义组合控件

42 篇文章 0 订阅

自定义组合控件的使用步骤:

  1. 编写布局文件
  2. 实现构造方法
  3. 初始化UI
  4. 提供对外的方法
  5. 在布局当中引用该控件
  6. activity中使用

布局文件

layout:lib_platform_layout.xml

attrs.xml中设置自定义属性:

    <declare-styleable name="PublishPlatformLayout">
        <attr name="lineVisible" format="boolean" />
    </declare-styleable>

完整代码:

open class PublishPlatformLayout : LinearLayoutCompat {
    private lateinit var binding: LibPlateformLayoutBinding
    private var platformListener: OnPlatformListener? = null

    constructor(context: Context) : super(context) {
        init(context, null)
    }

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

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

    private fun init(context: Context, attrs: AttributeSet?) {
        if (attrs != null) {
            val ta = context.obtainStyledAttributes(attrs, R.styleable.PublishPlatformLayout)
            val lineVisible = ta.getBoolean(R.styleable.PublishPlatformLayout_lineVisible, false)
            ta.recycle()
        }

        binding = LibPlateformLayoutBinding.inflate(LayoutInflater.from(context), this, true)
    }

    fun initLayout(onPlatformListener: OnPlatformListener) {
        platformListener = onPlatformListener
    }

    private fun buildPlatFormItemView(): View {
        val itemBinding = ItemPublishPlatformBinding
            .inflate(LayoutInflater.from(context), binding.gridLayout, false)

        itemBinding.rlValid.visibility = View.GONE

        // ....
        return itemBinding.root
    }
    fun showPlatformAdapter(list: List<ThirdAuthApi.ThirdAuthInfo>?) {
        binding.gridLayout.removeAllViews()
		// 。。。。
        if (!hasDouyin) {
            binding.gridLayout.addView(buildPlatFormItemView(false, Constant.PLATFORM_DOUYIN, null))
        }

    }

    interface OnPlatformListener {
        fun onCheckedChange(isChecked: Boolean)
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值