Android viewBinding/Databinding 构建BaseActivity和公共标题栏

东方吕恭
2023-12-01

1、自定义的titlebar

interface IPageHead {
    fun getPageHead(
        activity: Activity?,
        listener: OnPageHeadClickListener?
    ): PageHead?

    fun getPageHead(activity: Activity?): PageHead?
}

class PageHead : View.OnClickListener {
    private val mActivity: Activity?
    private var mIvBack: ImageView
    private var mTvTitle: TextView
    private var mTvRight: TextView

    interface OnPageHeadClickListener {
        fun onRightClick()
    }

    private var mOnPageHeadClickListener: OnPageHeadClickListener? = null

    constructor(activity: Activity, listener: OnPageHeadClickListener?) {
        mActivity = activity
        mOnPageHeadClickListener = listener
        mIvBack = activity.findViewById(R.id.ivBack)
        mIvBack.setOnClickListener(this)
        mTvTitle = activity.findViewById(R.id.tvTitle)
        mTvRight = activity.findViewById(R.id.tvRight)
    }

    constructor(activity: Activity) {
        mActivity = activity
        mIvBack = activity.findViewById(R.id.ivBack)
        mIvBack.setOnClickListener(this)
        mTvTitle = activity.findViewById(R.id.tvTitle)
        mTvRight = activity.findViewById(R.id.tvRight)
    }

    /**
     * 设置标题名称
     */
    fun setTitleText(value: String?) {
        if (!TextUtils.isEmpty(value)) {
            mTvTitle.text = value
        } else {
            LogUtil.logV(TAG, "the value is empty!")
        }
    }

    fun hideIvBack(isShow: Boolean) {
        if (isShow) {
            mIvBack.visibility = View.GONE
        } else {
            mIvBack.visibility = View.VISIBLE
        }
    }

    interface IBack {
        fun onPressBack()
    }

    private var mBackListener: IBack? = null
    fun setBackClick(listener: IBack?) {
        mBackListener = listener
    }

    override fun onClick(v: View) {
        val id = v.id
        if (id == R.id.ivBack) {
            if (mBackListener != null) {
                mBackListener!!.onPressBack()
            } else {
                mActivity?.finish()
            }
        } else if (id == R.id.tvRight) {
            if (mOnPageHeadClickListener != null) {
                mOnPageHeadClickListener!!.onRightClick()
            }
        }
    }

    companion object {
        private const val TAG = "PageHead"
    }
}
//自定义的titlebar布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutTitleBar"
    android:layout_width="fill_parent"
    android:layout_height="48dp"
    android:background="#fff000"
    android:gravity="center_vertical">


    <ImageView
        android:id="@+id/ivBack"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:paddingLeft="15dp"
        android:paddingRight="30dp"
        android:src="@mipmap/ic_launcher"
        android:visibility="visible" />


    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:ellipsize="end"
        android:maxLength="20"
        android:textColor="#001129"
        android:textSize="18sp" />


    <TextView
        android:id="@+id/tvRight"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:textSize="15sp"
        android:visibility="gone" />


</RelativeLayout>

2、ViewBinding的baseActivity

abstract class BaseMVActivity<V : ViewBinding, VM : ViewModel> : AppCompatActivity(),
    IPageHead {

    protected lateinit var binding: V
    protected lateinit var viewModel: VM

    private lateinit var contentLayout: LinearLayout
    private lateinit var pageHead: PageHead

    abstract fun getViewBinding(): V

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = getViewBinding()
        viewModel = ViewModelProvider(this).get(getViewModelClass())
        initContentView()
        setContentView(binding.root)
       
    }

    open fun initContentView() {
        val viewGroup: ViewGroup = findViewById(android.R.id.content)
        viewGroup.removeAllViews()
        contentLayout = LinearLayout(this)
        contentLayout.orientation = LinearLayout.VERTICAL
        contentLayout.setBackgroundResource(android.R.color.white)
        viewGroup.addView(contentLayout)
        LayoutInflater.from(this).inflate(R.layout.layout_title, contentLayout, true)
    }

    override fun setContentView(view: View) {
        val lp = ViewGroup.LayoutParams(-1,-1)
        contentLayout.addView(view,lp)
    }

    private fun getViewModelClass(): Class<VM> {
        val type = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1]
        return type as Class<VM>
    }

    
   
    override fun getPageHead(
        activity: Activity,
        listener: PageHead.OnPageHeadClickListener
    ): PageHead {
        pageHead = PageHead(activity, listener)
        return pageHead
    }

    override fun getPageHead(activity: Activity): PageHead {
        pageHead = PageHead(activity)
        return pageHead
    }
}

3、databinding的baseActivity

abstract class BaseMVVMActivity<V : ViewDataBinding, VM : ViewModel> : AppCompatActivity(),
    IPageHead {

    protected lateinit var binding: V
    protected lateinit var viewModel: VM
    var viewModelId = 0


    private lateinit var contentLayout: LinearLayout
    private lateinit var pageHead: PageHead

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, layoutId())
        binding.lifecycleOwner = this
        viewModel = ViewModelProvider(this).get(getViewModelClass())
        //XML的viewModel的绑定
        viewModelId = initVariableId()
        binding.setVariable(viewModelId, viewModel)
        initContentView()
        setContentView(binding.root)
    }

    open fun initContentView() {
        val viewGroup: ViewGroup = findViewById(android.R.id.content)
        viewGroup.removeAllViews()
        contentLayout = LinearLayout(this)
        contentLayout.orientation = LinearLayout.VERTICAL
        contentLayout.setBackgroundResource(android.R.color.white)
        viewGroup.addView(contentLayout)
        LayoutInflater.from(this).inflate(R.layout.layout_title, contentLayout, true)
    }

    override fun setContentView(view: View) {
        contentLayout.addView(view)
    }

    abstract fun layoutId(): Int

    private fun getViewModelClass(): Class<VM> {
        val type = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1]
        return type as Class<VM>
    }

    /**
     * 初始化ViewModel的id
     *
     * @return BR的id
     */
    abstract fun initVariableId(): Int

    
    override fun getPageHead(
        activity: Activity,
        listener: PageHead.OnPageHeadClickListener
    ): PageHead {
        pageHead = PageHead(activity, listener)
        return pageHead
    }

    override fun getPageHead(activity: Activity): PageHead {
        pageHead = PageHead(activity)
        return pageHead
    }
    
}

 类似资料: