Skip to content

Instantly share code, notes, and snippets.

View hwd6190128's full-sized avatar
🏠
Working from home

Chang Hung Lun hwd6190128

🏠
Working from home
View GitHub Profile
/**
* Created by Howard Chang on 2018/09/13.
*/
class HttpLogger : HttpLoggingInterceptor.Logger {
private var sBuilder = StringBuilder()
override fun log(message: String) {
var mMessage: String = message

DiffUtil

DiffUtil是support-v7:24.2.0中新增的工具類,它用來比較兩個數據,尋找兩者之間的最小變化量,再將算好的結果return。因此凡是數據集的比較都能做,DiffUtil提供了callback讓user進行其他操作,本篇著重在RecyclerView的更新。

演算法

採用Myers的差異算法(git diff)。空間複雜度為O(n),時間複雜度為O(N + D ^2)。 主要是計算兩個數據集添加和刪除的最小差異數,也就是用最簡單的方式將一個數據集轉換為另一個。 DiffUtil預設支援數據的移動偵測,所以需要對結果進行二次運算,較為耗費效能,時間複雜度提高到O(N ^ 2),N是add和remove的操作總數;但如果數據集已根據條件排序,或是數據集中的數據不存在移位情境,可以關掉第二次計算來提高效能。

使用

它最大的用處就是在RecyclerView刷新時,不再無腦notifyDataSetChanged(),原本的notifyDataSetChanged()有兩個缺點:

/**
* Created by Howard Chang on 2018/11/13.
*
* @param recyclerView 被綁定的RecyclerView
* @param mIsLoadMore 是否要演出刷新動畫
* @param mIsAnim 是否有LoadMore功能,有得話在刷新之後復位position
*/
abstract class BaseDiffAdapter<T, V : RecyclerView.ViewHolder>(
protected val recyclerView: RecyclerView? = null
, protected val mIsLoadMore: Boolean = false
webView.setWebViewClient(new WebViewClient() {
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return handlerUri(view, Uri.parse(url));
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override

setup adb

Add platform-tools to your path

echo 'export ANDROID_HOME=/Users/$USER/Library/Android/sdk' >> ~/.bash_profile
echo 'export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools' >> ~/.bash_profile

Refresh your bash profile (or restart your terminal app)

source ~/.bash_profile
@hwd6190128
hwd6190128 / Android Utils.kt
Last active May 21, 2019 09:37
Utils fun of android dev
val Int.pxToDp: Int
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
val Int.dpToPx: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
val Int.spToPx: Int
get() = (this * Resources.getSystem().displayMetrics.scaledDensity).toInt()
@hwd6190128
hwd6190128 / Android DeviceUtils.kt
Last active March 8, 2020 21:47
DeviceUtils fun of android dev
/**
* immersiveMode
* set onPostResume, onSystemUiVisibilityChange
*/
fun immersiveMode(activity: Activity) {
activity.window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN)
activity.window
.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
@hwd6190128
hwd6190128 / Android ResUtils.kt
Last active May 21, 2019 09:36
ResUtils fun of android dev
/**
* Created by Howard Chang on 2018/10/25.
*/
object ResUtils {
fun getStringArray(@ArrayRes id: Int, context: Context): Array<String> {
return context.resources.getStringArray(id)
}
fun getIntArray(@ArrayRes id: Int, context: Context): IntArray {
import android.view.View
import android.view.animation.Animation
import android.view.animation.Transformation
/**
* Created by Howard Chang on 2018/10/25.
*/
object AnimUtils {
object DateUtils {
internal const val ONE_MINUTE: Long = 60
internal const val HALF_HOUR: Long = 30 * 60 // 1800
internal const val ONE_HOUR: Long = 60 * 60 // 3600
internal const val ONE_DAY: Long = 24 * 60 * 60 // 86400
internal const val HALF_MONTH: Long = ONE_DAY * 15 // 1296000
internal const val ONE_MONTH: Long = ONE_DAY * 30 // 2592000
internal const val HALF_YEAR: Long = ONE_MONTH * 6 // 15552000
internal const val ONE_YEAR: Long = ONE_MONTH * 12 // 31104000