From 0fdb841c53b658569a2106ec13212fc9241f0ca8 Mon Sep 17 00:00:00 2001 From: jun <16167479@qq.com> Date: Mon, 5 Sep 2022 22:37:58 +0800 Subject: [PATCH] improve code --- .../com/lxj/easyadapter/sample/MainActivity.kt | 17 +++++++++++++++-- app/src/main/res/layout/adapter_one.xml | 3 +++ .../com/lxj/easyadapter/ItemDelegateManager.kt | 3 +-- .../com/lxj/easyadapter/MultiItemTypeAdapter.kt | 6 +++--- .../main/java/com/lxj/easyadapter/ViewHolder.kt | 10 +++++----- .../java/com/lxj/easyadapter/WrapperUtils.kt | 2 +- 6 files changed, 28 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/lxj/easyadapter/sample/MainActivity.kt b/app/src/main/java/com/lxj/easyadapter/sample/MainActivity.kt index d197a9a..f855e78 100644 --- a/app/src/main/java/com/lxj/easyadapter/sample/MainActivity.kt +++ b/app/src/main/java/com/lxj/easyadapter/sample/MainActivity.kt @@ -9,10 +9,12 @@ import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.DiffUtil +import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.blankj.utilcode.util.CloneUtils import com.blankj.utilcode.util.GsonUtils +import com.blankj.utilcode.util.LogUtils import com.bumptech.glide.Glide import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions @@ -43,6 +45,8 @@ class MainActivity : AppCompatActivity() { userList.add(User(name = "都是")) userList.add(User(name = "CEAD")) userList.add(User(name = "大图")) + userList.add(User(name = "大图11111")) + userList.add(User(name = "大图111122222")) add.setOnClickListener { val old = deepCopy() val range = (0 until userList.size) @@ -76,7 +80,16 @@ class MainActivity : AppCompatActivity() { userList.reverse() DiffUtil.calculateDiff(UserDiffCallback(old, userList)).dispatchUpdatesTo(recyclerView.adapter!!) } - recyclerView.layoutManager = LinearLayoutManager(this) +// recyclerView.layoutManager = LinearLayoutManager(this) + recyclerView.layoutManager = GridLayoutManager(this, 3).apply { + spanSizeLookup = object : GridLayoutManager.SpanSizeLookup(){ + override fun getSpanSize(position: Int): Int { + LogUtils.e("posi: $position count: ${if(position <1) 1 else 3}") + return if(position <1) 3 else 1 + } + + } + } //prepare data // for (i in 0..6) { @@ -198,7 +211,7 @@ class MainActivity : AppCompatActivity() { return position >=2 } override fun bind(holder: ViewHolder, user: User, position: Int) { - holder.setText(android.R.id.text1, "age: " + user.age) + holder.setText(android.R.id.text1, "age: " + position) holder.getView(android.R.id.text1).setBackgroundColor(Color.RED) } } diff --git a/app/src/main/res/layout/adapter_one.xml b/app/src/main/res/layout/adapter_one.xml index 9fe9764..4a0ff91 100644 --- a/app/src/main/res/layout/adapter_one.xml +++ b/app/src/main/res/layout/adapter_one.xml @@ -2,16 +2,19 @@ { } } return 0 -// throw IllegalArgumentException( -// "No ItemDelegate added that matches position=$position in data source") + } fun convert(holder: ViewHolder, item: T, position: Int, payloads: List? = null) { diff --git a/easy-adapter/src/main/java/com/lxj/easyadapter/MultiItemTypeAdapter.kt b/easy-adapter/src/main/java/com/lxj/easyadapter/MultiItemTypeAdapter.kt index 6613649..54650dd 100644 --- a/easy-adapter/src/main/java/com/lxj/easyadapter/MultiItemTypeAdapter.kt +++ b/easy-adapter/src/main/java/com/lxj/easyadapter/MultiItemTypeAdapter.kt @@ -46,7 +46,7 @@ open class MultiItemTypeAdapter(var data: List) : RecyclerView.Adapter(var data: List) : RecyclerView.Adapter + viewHolder.itemView.setOnClickListener { v -> if (mOnItemClickListener != null) { val position = viewHolder.adapterPosition - headersCount mOnItemClickListener!!.onItemClick(v, viewHolder, position) } } - viewHolder.convertView.setOnLongClickListener(View.OnLongClickListener { v -> + viewHolder.itemView.setOnLongClickListener(View.OnLongClickListener { v -> if (mOnItemClickListener != null) { val position = viewHolder.adapterPosition - headersCount return@OnLongClickListener mOnItemClickListener!!.onItemLongClick(v, viewHolder, position) diff --git a/easy-adapter/src/main/java/com/lxj/easyadapter/ViewHolder.kt b/easy-adapter/src/main/java/com/lxj/easyadapter/ViewHolder.kt index b1eef31..b42b708 100644 --- a/easy-adapter/src/main/java/com/lxj/easyadapter/ViewHolder.kt +++ b/easy-adapter/src/main/java/com/lxj/easyadapter/ViewHolder.kt @@ -9,13 +9,13 @@ import android.widget.ImageView import android.widget.TextView import androidx.recyclerview.widget.RecyclerView -class ViewHolder(val convertView: View) : RecyclerView.ViewHolder(convertView) { +class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { private val mViews: SparseArray = SparseArray() fun getView(viewId: Int): T { var view: View? = mViews.get(viewId) if (view == null) { - view = convertView.findViewById(viewId) + view = itemView.findViewById(viewId) mViews.put(viewId, view) } return view as T @@ -24,20 +24,20 @@ class ViewHolder(val convertView: View) : RecyclerView.ViewHolder(convertView) { fun getViewOrNull(viewId: Int): T? { var view: View? = mViews.get(viewId) if (view == null) { - view = convertView.findViewById(viewId) + view = itemView.findViewById(viewId) mViews.put(viewId, view) } return view as? T } fun setText(viewId: Int, text: CharSequence): ViewHolder { - val tv = getView(viewId) + val tv = getViewOrNull(viewId) tv?.text = text return this } fun setImageResource(viewId: Int, resId: Int): ViewHolder { - val view = getView(viewId) + val view = getViewOrNull(viewId) view?.setImageResource(resId) return this } diff --git a/easy-adapter/src/main/java/com/lxj/easyadapter/WrapperUtils.kt b/easy-adapter/src/main/java/com/lxj/easyadapter/WrapperUtils.kt index 31947f6..881915b 100644 --- a/easy-adapter/src/main/java/com/lxj/easyadapter/WrapperUtils.kt +++ b/easy-adapter/src/main/java/com/lxj/easyadapter/WrapperUtils.kt @@ -20,7 +20,7 @@ object WrapperUtils { return fn(layoutManager, spanSizeLookup, position) } } - layoutManager.spanCount = layoutManager.spanCount +// layoutManager.spanCount = layoutManager.spanCount } }