This commit is contained in:
luyao 2019-06-14 16:21:17 +08:00
parent b52ad830bf
commit e3ba6d151c
17 changed files with 227 additions and 70 deletions

View File

@ -20,6 +20,7 @@
</activity>
<activity android:name="luyao.util.ktx.mvvm.MvvmActivity"/>
<activity android:name="luyao.util.ktx.ui.ExtActivity"/>
<activity android:name="luyao.util.ktx.ui.ToastExtActivity"/>
</application>
</manifest>

View File

@ -1,11 +1,12 @@
package luyao.util.ktx.ui
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_ext.*
import kotlinx.android.synthetic.main.title_layout.*
import luyao.ktx.R
import luyao.util.ktx.adapter.CommonAdapter
import luyao.util.ktx.base.BaseActivity
import luyao.util.ktx.ui.fragment.ContextExtFragment
import luyao.util.ktx.ext.itemPadding
/**
* Created by luyao
@ -13,19 +14,18 @@ import luyao.util.ktx.ui.fragment.ContextExtFragment
*/
class ExtActivity : BaseActivity() {
private val titleList = arrayOf("Context", "String")
private val fragmentList = arrayListOf<Fragment>()
private val contextExtFragment = ContextExtFragment()
private val stringExtFragment = ContextExtFragment()
private val dataList = ArrayList<String>()
private val commonAdapter by lazy { CommonAdapter() }
init {
dataList.run {
add("ToastExt")
add("AppExt")
}
}
override fun getLayoutResId() = R.layout.activity_ext
init {
fragmentList.add(contextExtFragment)
fragmentList.add(stringExtFragment)
}
override fun initView() {
mToolbar.run {
@ -33,21 +33,27 @@ class ExtActivity : BaseActivity() {
setNavigationIcon(R.drawable.arrow_back)
}
initViewPager()
initRecycleView()
}
override fun initData() {
commonAdapter.setNewData(dataList)
}
private fun initViewPager() {
extViewPager.adapter =
object : FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun getItem(position: Int) = fragmentList[position]
private fun initRecycleView() {
commonAdapter.setOnItemClickListener { _, _, position ->
handleClick(position)
}
extRecycleView.run {
itemPadding(5,5,10,10)
layoutManager = LinearLayoutManager(this@ExtActivity)
adapter = commonAdapter
}
}
override fun getCount() = fragmentList.size
override fun getPageTitle(position: Int) = titleList[position]
}
extTabLayout.setupWithViewPager(extViewPager)
private fun handleClick(position: Int) {
when (position) {
0 -> startActivity(ToastExtActivity::class.java)
}
}
}

View File

@ -0,0 +1,27 @@
package luyao.util.ktx.ui
import kotlinx.android.synthetic.main.activity_toast_ext.*
import kotlinx.android.synthetic.main.title_layout.*
import luyao.ktx.R
import luyao.util.ktx.base.BaseActivity
import luyao.util.ktx.ext.longToast
import luyao.util.ktx.ext.toast
/**
* Created by luyao
* on 2019/6/14 14:36
*/
class ToastExtActivity : BaseActivity() {
override fun getLayoutResId() = R.layout.activity_toast_ext
override fun initView() {
mToolbar.title = "ToastExt"
}
override fun initData() {
toastBt.setOnClickListener { toast("I am groot.") }
longToastBt.setOnClickListener { longToast(R.string.groot) }
}
}

View File

@ -0,0 +1,6 @@
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item>
<color android:color="@android:color/white"/>
</item>
</ripple>

View File

@ -1,36 +1,17 @@
<?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">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include layout="@layout/title_layout"/>
<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"
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/extRecycleView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
android:paddingTop="20dp">
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>

View File

@ -0,0 +1,19 @@
<?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">
<include layout="@layout/title_layout"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toastBt"
android:text="toast"
style="@style/NormalButton"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/longToastBt"
android:text="longToast"
style="@style/NormalButton"/>
</LinearLayout>

View File

@ -1,20 +1,17 @@
<?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">
<androidx.cardview.widget.CardView 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:layout_height="45dp"
android:paddingLeft="8dp"
android:gravity="center_vertical"/>
android:gravity="center_vertical"
android:background="@drawable/ripple"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/light_gray"/>
</LinearLayout>
</androidx.cardview.widget.CardView>

View File

@ -4,4 +4,6 @@
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="light_gray">#e0e0e0</color>
<color name="black">#000000</color>
<color name="white">#ffffff</color>
</resources>

View File

@ -0,0 +1,17 @@
<resources>
<style name="NormalButton">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">center</item>
<item name="android:textAllCaps">false</item>
<item name="android:padding">2dp</item>
<item name="android:layout_marginBottom">5dp</item>
<item name="android:layout_marginTop">5dp</item>
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_marginRight">10dp</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/white</item>
<item name="android:background">#333</item>
</style>
</resources>

View File

@ -1,3 +1,4 @@
<resources>
<string name="app_name">AndroidUtilCodeKTX</string>
<string name="groot">I am groot.</string>
</resources>

View File

@ -38,6 +38,7 @@ dependencies {
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1"
api "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha01"
api 'androidx.lifecycle:lifecycle-extensions:2.2.0-alpha01'
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha06'
}

View File

@ -10,9 +10,8 @@ import java.io.File
* on 2019/6/12 10:53
*/
/**
* get [AppInfo] by apk file path
*/
fun Context.getVersionName(): String = packageManager.getPackageInfo(packageName, 0).versionName
fun Context.getAppInfo(apkPath: String): AppInfo {
val packageInfo = packageManager.getPackageInfo(apkPath,PackageManager.GET_META_DATA)
packageInfo.applicationInfo.sourceDir=apkPath
@ -26,9 +25,6 @@ fun Context.getAppInfo(apkPath: String): AppInfo {
return AppInfo(apkPath,packageName,versionName,versionCode,appName,icon)
}
/**
* get [AppInfo] list by apk folder path
*/
fun Context.getAppInfos(apkFolderPath:String):List<AppInfo>{
val appInfoList = ArrayList<AppInfo>()
for (file in File(apkFolderPath).listFiles())

View File

@ -0,0 +1,29 @@
package luyao.util.ktx.ext
import android.content.Context
import android.view.View
/**
* Created by luyao
* on 2019/6/14 14:23
*/
fun Context.dp2px(dp: Float): Int {
val scale = resources.displayMetrics.density
return (dp * scale + 0.5f).toInt()
}
fun Context.px2dp(px: Float): Int {
val scale = resources.displayMetrics.density
return (px / scale + 0.5f).toInt()
}
fun View.dp2px(dp: Float): Int {
val scale = resources.displayMetrics.density
return (dp * scale + 0.5f).toInt()
}
fun View.px2dp(px: Float): Int {
val scale = resources.displayMetrics.density
return (px / scale + 0.5f).toInt()
}

View File

@ -0,0 +1,27 @@
package luyao.util.ktx.ext
import java.security.MessageDigest
/**
* Created by luyao
* on 2019/6/14 15:28
*/
@kotlin.internal.LowPriorityInOverloadResolution
fun ByteArray.md5(): ByteArray = hash(this, Hash.MD5)
fun ByteArray.md5() : String = hash(this,Hash.MD5).toHexString()
fun ByteArray.sha1(): ByteArray = hash(this, Hash.SHA1)
fun ByteArray.sha256(): ByteArray = hash(this, Hash.SHA256)
enum class Hash(algorithm: String) {
MD5("md5"),
SHA1("sha1"),
SHA256("sha256")
}
fun hash(data: ByteArray, algorithm: Hash): ByteArray {
val messageDigest = MessageDigest.getInstance(algorithm.name)
return messageDigest.digest(data)
}

View File

@ -0,0 +1,29 @@
package luyao.util.ktx.ext
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
/**
* Created by luyao
* on 2019/6/14 14:07
*/
fun RecyclerView.itemPadding(top: Int, bottom: Int, left: Int = 0, right: Int = 0) {
addItemDecoration(PaddingItemDecoration(top, bottom, left, right))
}
class PaddingItemDecoration(top: Int, bottom: Int, left: Int, right: Int) : RecyclerView.ItemDecoration() {
private val mTop = top
private val mBottom = bottom
private val mLeft = left
private val mRight = right
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
outRect.bottom = view.dp2px(mBottom.toFloat())
outRect.top = view.dp2px(mTop.toFloat())
outRect.left = view.dp2px(mLeft.toFloat())
outRect.right = view.dp2px(mRight.toFloat())
}
}

View File

@ -44,5 +44,4 @@ fun Any.longToast(context: Context, @StringRes id: Int) {
context.longToast(id)
}
fun Context.getVersionName(): String = packageManager.getPackageInfo(packageName, 0).versionName

View File

@ -0,0 +1,19 @@
package luyao.util.ktx.ext
import kotlin.experimental.and
/**
* Created by luyao
* on 2019/6/14 15:43
*/
fun ByteArray.toHexString(): String {
val builder = StringBuilder()
for (b in this) {
var hex = Integer.toHexString((b and 0xFF.toByte()).toInt())
if (hex.length == 1) hex = "0$hex"
builder.append(hex.toUpperCase())
}
return builder.toString()
}