ToastExt
This commit is contained in:
parent
f95fc0f6eb
commit
c562c1d278
|
|
@ -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')
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ allprojects {
|
|||
repositories {
|
||||
google()
|
||||
jcenter()
|
||||
|
||||
maven { url "https://jitpack.io" }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="luyao.ktx"/>
|
||||
package="luyao.util.ktx"/>
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package luyao.ktx.base
|
||||
package luyao.util.ktx.base
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
|
|
@ -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()
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package luyao.ktx.base
|
||||
package luyao.util.ktx.base
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.lifecycle.LifecycleObserver
|
||||
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package luyao.ktx.base
|
||||
package luyao.util.ktx.base
|
||||
|
||||
import androidx.lifecycle.LifecycleObserver
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
|
|
@ -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
|
||||
|
||||
Loading…
Reference in New Issue