From 0c0ced013701792e824d4822646456470d8efe61 Mon Sep 17 00:00:00 2001 From: Zhanty Date: Fri, 12 Jul 2019 18:07:58 +0800 Subject: [PATCH] optimize --- config/repository.gradle | 2 +- .../adapter/recycler/SimpleRecyclerAdapter.kt | 21 +++++++++---------- .../java/com/android/base/data/Resource.kt | 6 ------ 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/config/repository.gradle b/config/repository.gradle index 65b96ce..4e64a2d 100644 --- a/config/repository.gradle +++ b/config/repository.gradle @@ -303,7 +303,7 @@ static Repository innerNewRepository() { /*text view*/ //https://github.com/chenBingX/SuperTextView - superTextView : 'com.github.chenBingX:SuperTextView:v3.1.1', + superTextView : 'com.github.chenBingX:SuperTextView:v3.1.4', //https://github.com/iwgang/CountdownView countdownView : 'com.github.iwgang:countdownview:2.1.3', diff --git a/lib_base/src/main/java/com/android/base/adapter/recycler/SimpleRecyclerAdapter.kt b/lib_base/src/main/java/com/android/base/adapter/recycler/SimpleRecyclerAdapter.kt index ed14837..fb3bdbd 100644 --- a/lib_base/src/main/java/com/android/base/adapter/recycler/SimpleRecyclerAdapter.kt +++ b/lib_base/src/main/java/com/android/base/adapter/recycler/SimpleRecyclerAdapter.kt @@ -4,8 +4,7 @@ import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import kotlin.reflect.KClass -import kotlin.reflect.full.primaryConstructor +import com.android.base.kotlin.KtViewHolder /** * A simple way to build a simple list. If you want to build a multi type list, perhaps you need to use [MultiTypeAdapter]. @@ -14,23 +13,23 @@ import kotlin.reflect.full.primaryConstructor * Email: ztiany3@gmail.com * Date : 2019-01-15 11:41 */ -abstract class SimpleRecyclerAdapter(context: Context, data: List? = null) : RecyclerAdapter(context, data) { +abstract class SimpleRecyclerAdapter(context: Context, data: List? = null) : RecyclerAdapter(context, data) { private var mLayoutInflater: LayoutInflater = LayoutInflater.from(mContext) - final override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH { + final override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): KtViewHolder { val layout = provideLayout(parent, viewType) val itemView = if (layout is Int) { mLayoutInflater.inflate(layout, parent, false) } else layout as View - return provideViewHolder(itemView) + return KtViewHolder(itemView).apply { + onViewHolderCreated(this) + } } - @Suppress("UNCHECKED_CAST") - open fun provideViewHolder(itemView: View): VH { - return (this::class.supertypes[0].arguments[1].type?.classifier as? KClass)?.primaryConstructor?.call(itemView) - ?: throw IllegalArgumentException("need primaryConstructor, and arguments is (View)") + protected fun onViewHolderCreated(ktViewHolder: KtViewHolder) { + } /**provide a layout id or a View*/ @@ -40,7 +39,7 @@ abstract class SimpleRecyclerAdapter(context: Context, data: return TYPE_ITEM } - override fun onBindViewHolder(viewHolder: VH, position: Int) { + override fun onBindViewHolder(viewHolder: KtViewHolder, position: Int) { if (viewHolder.itemViewType == TYPE_ITEM) { bind(viewHolder, getItem(position)) } else { @@ -48,7 +47,7 @@ abstract class SimpleRecyclerAdapter(context: Context, data: } } - protected abstract fun bind(viewHolder: VH, item: T) + protected abstract fun bind(viewHolder: KtViewHolder, item: T) protected open fun bindOtherTypes(viewHolder: ViewHolder, position: Int) {} diff --git a/lib_base/src/main/java/com/android/base/data/Resource.kt b/lib_base/src/main/java/com/android/base/data/Resource.kt index 2ac50d9..0ca07e0 100644 --- a/lib_base/src/main/java/com/android/base/data/Resource.kt +++ b/lib_base/src/main/java/com/android/base/data/Resource.kt @@ -1,7 +1,5 @@ package com.android.base.data -import android.arch.lifecycle.LiveData - inline fun Resource.onLoading(onLoading: () -> Unit): Resource { if (this.isLoading) { @@ -38,8 +36,4 @@ inline fun Resource.onSuccessWithData(onSuccess: (data: T) -> Unit): Reso onSuccess(this.data()) } return this -} - -fun LiveData>.resourceData(): T? { - return if (value?.isSuccess == true) value?.orElse(null) else null } \ No newline at end of file