kotLin 对基类的封装

5 篇文章 0 订阅
3 篇文章 0 订阅

哈哈
KotLin 对基类简单的封装

1:为什么要用kotLin?

在这里插入图片描述## 正题: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())
        initView()
        initData()
        mActivities.add(this)
    }


    /**
     *  关闭所有Activity
     */
    companion object {
        private val mActivities = LinkedList<BaseActivity>()
        private fun finishAll() {
            var copy: ArrayList<BaseActivity>?
            synchronized(mActivities) {
                copy = ArrayList<BaseActivity>(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<out Activity>) {
        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: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)
        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.mView
    }

    protected fun <T : View> 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
    }

}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值