最近趁着手头清闲之时,开启了我的kotlin之路。项目内容:github扒了一些图片,聚合上找了一些免费接口。项目包含四部分。新闻模块,笑话模块,历史今天模块,星座运势模块,以及我的模块。整个项目写起来还是比较简单的,毕竟没有什么复杂的页面逻辑。一下展示项目部分代码。跟成品:
项目中用到的库:
- //鸿洋大神封装的okhttpUtils
implementation ‘com.zhy:okhttputils:2.6.2’
网络请求okhttp
异步框架Rxjava2
网络请求Retrofit
刷新控件
卡片式布局
指示器
项目结构:
base类:
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
}
}
以上是项目的用到的基类。因项目整理较简单,时间所用不多,可以会有一些小问题。如有发现请告之…以下是项目部分展示
项目地址:传送阵