This commit is contained in:
luyao 2019-06-10 15:00:48 +08:00
parent f95fc0f6eb
commit c562c1d278
26 changed files with 386 additions and 43 deletions

View File

@ -29,7 +29,9 @@ dependencies {
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.material:material:1.1.0-alpha07'
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.45-androidx'
implementation project(path: ':library')
}

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="luyao.ktx">
<application
@ -8,7 +9,8 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name="luyao.util.ktx.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
@ -17,6 +19,7 @@
</intent-filter>
</activity>
<activity android:name="luyao.util.ktx.mvvm.MvvmActivity"/>
<activity android:name="luyao.util.ktx.ui.ExtActivity"/>
</application>
</manifest>

View File

@ -2,8 +2,9 @@ package luyao.util.ktx
import kotlinx.android.synthetic.main.activity_main.*
import luyao.ktx.R
import luyao.ktx.base.BaseActivity
import luyao.util.ktx.base.BaseActivity
import luyao.util.ktx.mvvm.MvvmActivity
import luyao.util.ktx.ui.ExtActivity
class MainActivity : BaseActivity() {
@ -13,5 +14,6 @@ class MainActivity : BaseActivity() {
override fun initData() {
btMvvm.setOnClickListener { startActivity(MvvmActivity::class.java) }
btExt.setOnClickListener { startActivity(ExtActivity::class.java) }
}
}

View File

@ -0,0 +1,16 @@
package luyao.util.ktx.adapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import luyao.ktx.R
/**
* Created by luyao
* on 2019/6/10 10:57
*/
class CommonAdapter(layoutResId: Int = R.layout.item_common) : BaseQuickAdapter<String, BaseViewHolder>(layoutResId) {
override fun convert(helper: BaseViewHolder, item: String) {
helper.setText(R.id.itemTv, item)
}
}

View File

@ -4,8 +4,8 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import kotlinx.android.synthetic.main.activity_mvvm_demo.*
import luyao.ktx.R
import luyao.ktx.base.BaseVMActivity
import luyao.ktx.ext.toast
import luyao.util.ktx.base.BaseVMActivity
import luyao.util.ktx.ext.toast
/**
* Created by luyao

View File

@ -1,12 +1,10 @@
package luyao.util.ktx.mvvm
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import luyao.ktx.base.BaseViewModel
import luyao.util.ktx.base.BaseViewModel
/**
* Created by luyao

View File

@ -0,0 +1,53 @@
package luyao.util.ktx.ui
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import kotlinx.android.synthetic.main.activity_ext.*
import luyao.ktx.R
import luyao.util.ktx.base.BaseActivity
import luyao.util.ktx.ui.fragment.ContextExtFragment
/**
* Created by luyao
* on 2019/6/10 10:00
*/
class ExtActivity : BaseActivity() {
private val titleList = arrayOf("Context", "String")
private val fragmentList = arrayListOf<Fragment>()
private val contextExtFragment = ContextExtFragment()
private val stringExtFragment = ContextExtFragment()
override fun getLayoutResId() = R.layout.activity_ext
init {
fragmentList.add(contextExtFragment)
fragmentList.add(stringExtFragment)
}
override fun initView() {
mToolbar.run {
title = "扩展函数"
setNavigationIcon(R.drawable.arrow_back)
}
initViewPager()
}
override fun initData() {
}
private fun initViewPager() {
extViewPager.adapter =
object : FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun getItem(position: Int) = fragmentList[position]
override fun getCount() = fragmentList.size
override fun getPageTitle(position: Int) = titleList[position]
}
extTabLayout.setupWithViewPager(extViewPager)
}
}

View File

@ -0,0 +1,37 @@
package luyao.util.ktx.ui.fragment
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_context_ext.*
import luyao.ktx.R
import luyao.util.ktx.adapter.CommonAdapter
import luyao.util.ktx.base.BaseFragment
/**
* Created by luyao
* on 2019/6/10 13:18
*/
abstract class CommonFragment : BaseFragment() {
open val dataList = ArrayList<String>()
private val commonAdapter by lazy { CommonAdapter() }
override fun getLayoutResId() = R.layout.fragment_context_ext
override fun initView() {
commonAdapter.setOnItemClickListener { _, _, position ->
handleClick(position)
}
recycleView.run {
layoutManager = LinearLayoutManager(context)
adapter = commonAdapter
}
}
override fun initData() {
fillList()
commonAdapter.setNewData(dataList)
}
abstract fun fillList()
abstract fun handleClick(position: Int)
}

View File

@ -0,0 +1,25 @@
package luyao.util.ktx.ui.fragment
import luyao.util.ktx.ext.getVersionName
import luyao.util.ktx.ext.toast
/**
* Created by luyao
* on 2019/6/10 10:46
*/
class ContextExtFragment : CommonFragment() {
override fun fillList() {
dataList.add("toast")
dataList.add("getVersionName")
}
override fun handleClick(position: Int) {
context?.run {
when (position) {
0 -> toast("I'am a short toast!")
1 -> toast(getVersionName())
}
}
}
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z" />
</vector>

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="@+id/mToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<com.google.android.material.tabs.TabLayout
android:id="@+id/extTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#ADBE107E"
app:tabMode="scrollable"
app:tabTextAppearance="@style/TabTextStyle"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/extViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -6,7 +6,7 @@
android:layout_height="match_parent"
tools:context="luyao.util.ktx.MainActivity"
android:orientation="vertical"
android:gravity="center_horizontal">
android:gravity="center">
<Button
android:id="@+id/btMvvm"
@ -14,4 +14,11 @@
android:layout_height="50dp"
android:text="MVVM"/>
<Button
android:id="@+id/btExt"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="扩展函数"
android:layout_marginTop="10dp"/>
</LinearLayout>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/itemTv"
android:textColor="#000000"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:paddingLeft="8dp"
android:gravity="center_vertical"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/light_gray"/>
</LinearLayout>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/mToolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="?attr/colorPrimary"
app:titleTextColor="#ffffff"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</FrameLayout>

View File

@ -3,4 +3,5 @@
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="light_gray">#e0e0e0</color>
</resources>

View File

@ -1,11 +1,15 @@
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="TabTextStyle" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
</style>
</resources>

View File

@ -19,7 +19,7 @@ allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}

View File

@ -1,2 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="luyao.ktx"/>
package="luyao.util.ktx"/>

View File

@ -1,27 +0,0 @@
package luyao.ktx.ext
import android.content.Context
import android.widget.Toast
import androidx.annotation.StringRes
/**
* Created by luyao
* on 2019/5/31 16:42
*/
var showToast: Toast? = null
fun Context.toast(content: String, duration: Int = Toast.LENGTH_SHORT) {
showToast?.apply {
setText(content)
show()
} ?: run {
Toast.makeText(this.applicationContext, content, Toast.LENGTH_SHORT).apply {
showToast = this
}.show()
}
}
fun Context.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_SHORT) {
toast(getString(id), duration)
}

View File

@ -1,4 +1,4 @@
package luyao.ktx.base
package luyao.util.ktx.base
import android.content.Intent
import android.os.Bundle

View File

@ -0,0 +1,29 @@
package luyao.util.ktx.base
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
/**
* Created by luyao
* on 2019/6/10 10:47
*/
abstract class BaseFragment : androidx.fragment.app.Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(getLayoutResId(), container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
initView()
initData()
super.onViewCreated(view, savedInstanceState)
}
abstract fun getLayoutResId(): Int
abstract fun initView()
abstract fun initData()
}

View File

@ -1,4 +1,4 @@
package luyao.ktx.base
package luyao.util.ktx.base
import android.os.Bundle
import androidx.lifecycle.LifecycleObserver

View File

@ -0,0 +1,49 @@
package luyao.util.ktx.base
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProviders
/**
* Created by luyao
* on 2019/6/10 10:48
*/
abstract class BaseVMFragment<VM : BaseViewModel> : androidx.fragment.app.Fragment() {
protected lateinit var mViewModel: VM
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(getLayoutResId(), container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
initVM()
initView()
initData()
startObserve()
super.onViewCreated(view, savedInstanceState)
}
open fun startObserve() {}
abstract fun getLayoutResId(): Int
abstract fun initView()
abstract fun initData()
private fun initVM() {
providerVMClass()?.let {
mViewModel = ViewModelProviders.of(this).get(it)
lifecycle.addObserver(mViewModel)
}
}
open fun providerVMClass(): Class<VM>? = null
override fun onDestroy() {
lifecycle.removeObserver(mViewModel)
super.onDestroy()
}
}

View File

@ -1,4 +1,4 @@
package luyao.ktx.base
package luyao.util.ktx.base
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.MutableLiveData

View File

@ -0,0 +1,48 @@
package luyao.util.ktx.ext
import android.content.Context
import android.widget.Toast
import androidx.annotation.StringRes
/**
* Created by luyao
* on 2019/5/31 16:42
*/
fun Context.toast(content: String, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this.applicationContext, content, duration).apply {
show()
}
}
fun Context.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_SHORT) {
toast(getString(id), duration)
}
fun Context.longToast(content: String) {
toast(content, Toast.LENGTH_LONG)
}
fun Context.longToast(@StringRes id: Int) {
toast(id, Toast.LENGTH_LONG)
}
fun Any.toast(context: Context, content: String, duration: Int = Toast.LENGTH_SHORT) {
context.toast(content, duration)
}
fun Any.toast(context: Context, @StringRes id: Int, duration: Int) {
context.toast(id, duration)
}
fun Any.longToast(context: Context, content: String) {
context.longToast(content)
}
fun Any.longToast(context: Context, @StringRes id: Int) {
context.longToast(id)
}
fun Context.getVersionName(): String = packageManager.getPackageInfo(packageName, 0).versionName