kotlin 实战-----第一个项目

最近趁着手头清闲之时,开启了我的kotlin之路。项目内容:github扒了一些图片,聚合上找了一些免费接口。项目包含四部分。新闻模块,笑话模块,历史今天模块,星座运势模块,以及我的模块。整个项目写起来还是比较简单的,毕竟没有什么复杂的页面逻辑。一下展示项目部分代码。跟成品:

项目中用到的库:

package www.app.ypy.com.journalism_kotlin.base

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import com.trello.rxlifecycle.components.support.RxAppCompatActivity
import java.util.*

/**
 * Created by ypu
 * on 2019/10/24 0024
 */
 abstract class BaseActivity : RxAppCompatActivity(){

    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()//加载数据

        /**
         * 设置布局
         *
         * @return
         */
        abstract fun intiLayout(): Int

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(null)
            setContentView(intiLayout())
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//                //透明状态栏
//                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
//                //透明导航栏
//                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
//            }
            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
        }

        /**
         * 携带数据的页面跳转
         *
         * @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)
        }




}

package www.app.ypy.com.journalism_kotlin.base

import android.app.Activity
import android.content.Context
import android.content.Intent
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


abstract class BaseFragment : RxFragment() {

    protected var isInit = false
    protected var isLoad = false
    protected val TAG = "LazyLoadFragment"
    private var mView: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? {
        mView = inflater.inflate(this.setContentView(), container, false)
        this.isInit = true
        this.mContext = this.context!!
        this.mActivity = this.activity!!
        this.isCanLoadData()
        return mView
    }
    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)
    }
    /**
     * 携带数据的页面跳转
     *
     * @param clz
     * @param bundle
     */

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

    protected fun stopLoad() {}

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

}

以上是项目的用到的基类。因项目整理较简单,时间所用不多,可以会有一些小问题。如有发现请告之…以下是项目部分展示
在这里插入图片描述在这里插入图片描述在这里插入图片描述项目地址:传送阵

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值