kotLin 对基类的封装

                                KotLin 对基类简单的封装

1:为什么要用kotLin?

kotLin 对基类的封装_第1张图片## 正题:BaseActivity:

package www.app.kotlin.com.mykotlinprojecr.base
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import butterknife.ButterKnife
import com.trello.rxlifecycle.components.support.RxAppCompatActivity
import java.util.*
import kotlin.collections.ArrayList

abstract class BaseActivity : RxAppCompatActivity(), View.OnClickListener {
    var isSelect: Boolean = true
    private var toase: Toast? = null
    private var isshowtitle = true
    private var isshowstate = true
    private var mAllowFullScreen = true
    var lastClick: Long = 0
    /**
     * 是否沉浸状态栏
     */


    private var isSetStatusBar = true
    protected abstract fun initView()//初始化数据
    protected abstract fun initData()//加载数据
    protected abstract fun widgetClick(v: View?)
    /**
     * 设置布局
     *
     * @return
     */
    abstract fun intiLayout(): Int

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(intiLayout())
        ButterKnife.bind(this)
        initView()
        initData()
        mActivities.add(this)
    }


    /**
     *  关闭所有Activity
     */
    companion object {
        private val mActivities = LinkedList()
        private fun finishAll() {
            var copy: ArrayList?
            synchronized(mActivities) {
                copy = ArrayList(mActivities)
            }
            for (activity in copy!!) {//!!表示不为空时候
                activity.finish()
            }
            copy!!.clear()
        }


    }

    /**
     * 是否设置标题栏
     *
     * @return
     */
    fun setTitle(ishow: Boolean) {
        isshowtitle = ishow
    }

    /**
     *  是否全屏
     */
    fun setAllowFullScreen(allowFullScreen: Boolean) {
        mAllowFullScreen = allowFullScreen
    }

    /**
     *   长的吐司
     */
    fun toastLong(msg: String? = null) {

        if (toase == null) {
            toase = Toast.makeText(this, "", Toast.LENGTH_LONG)
            toase?.setText(msg)
            toase?.show()

        } else {
            toase?.setText(msg)
            toase?.show()
        }
    }

    /**
     *   短的吐司
     */
    fun toastShort(msg: String) {
        if (null == toase) {
            toase = Toast.makeText(this, "", Toast.LENGTH_SHORT)
            //  toast.setDuration(Toast.LENGTH_SHORT);
            toase?.setText(msg)
            toase?.show()
        } else {
            toase?.setText(msg)
            toase?.show()
        }
    }

    /**
     *   防止快速点击
     */

    private fun fastClick(): Boolean {

        if (System.currentTimeMillis() - lastClick <= 1000) {
            return false
        }
        lastClick = System.currentTimeMillis()
        return true
    }

    /**
     *  点击事件
     */
    override fun onClick(v: View?) {
        if (fastClick()) {
            widgetClick(v)
        }
    }

    /**
     * 携带数据的页面跳转
     *
     * @param clz
     * @param bundle
     */

    fun startActivity(clz: Class<*>, bundle: Bundle?) {
        val intent = Intent()
        intent.setClass(this, clz)
        if (bundle != null) {
            intent.putExtras(bundle)
        }
        startActivity(intent)
    }

    /**
     *  n 无数据跳转
     */

    fun OnIntent(clz: Class) {
        val intent = Intent(this, clz)
        startActivity(intent)
    }

    /**
     * 含有Bundle通过Class打开编辑界面
     *
     * @param cls
     * @param bundle
     * @param requestCode
     */

    fun startActivityForResult(clz: Class<*>, bundle: Bundle?, resquestCode: Int) {

        val intent = Intent()
        intent.setClass(this, clz)
        if (bundle != null) {
            intent.putExtras(bundle)
        }
        startActivityForResult(intent,resquestCode)
    }
}

2: BaseFragment :

package www.app.kotlin.com.mykotlinprojecr.base

import android.app.Activity
import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import butterknife.ButterKnife
import com.trello.rxlifecycle.components.support.RxFragment

/**
 * Created by ypu
 * on 2019/7/29 0029
 */
  abstract class BaseFragment : RxFragment() {

    protected var isInit = false
    protected var isLoad = false
    protected val TAG = "LazyLoadFragment"
    private var view = null
    var sp: SharedPreferences? = null
    protected lateinit var mContext: Context
    protected lateinit var mActivity: Activity

    @RequiresApi(api = Build.VERSION_CODES.M)
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        this.view = inflater.inflate(this.setContentView(), container, false) as Nothing?
        ButterKnife.bind(mActivity)
        this.isInit = true
        this.mContext = this.context!!
        this.mActivity = this.activity!!
        this.isCanLoadData()
        return this.view
    }

     override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        this.isCanLoadData()
    }

    private fun isCanLoadData() {
        if (this.isInit) {
            if (this.userVisibleHint) {
                this.lazyLoad()
                this.isLoad = true
            } else if (this.isLoad) {
                this.stopLoad()
            }

        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        this.isInit = false
        this.isLoad = false
    }

    protected fun showToast(message: String) {
        if (!TextUtils.isEmpty(message)) {
            Toast.makeText(this.activity, message, Toast.LENGTH_SHORT).show()
        }

    }

    protected abstract fun setContentView(): Int

    protected fun getContentView(): View? {
        return this.view
    }

    protected fun  findViewById(id: Int): T {
        return this.getContentView()!!.findViewById(id)
    }

    protected abstract fun lazyLoad()

    protected fun stopLoad() {}

    fun getpx(dp: Int): Float {
        return dp.toFloat() * this.resources.displayMetrics.density
    }

}

你可能感兴趣的:(Android,移动,kotlin)