Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
junixapp committed Sep 5, 2022
1 parent a93b3e4 commit 0fdb841
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
17 changes: 15 additions & 2 deletions app/src/main/java/com/lxj/easyadapter/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<View>(android.R.id.text1).setBackgroundColor(Color.RED)
}
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/layout/adapter_one.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="12dp"
android:background="#098765"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/avatar"
android:src="@mipmap/ic_launcher"
android:layout_width="40dp"
android:layout_height="40dp"/>
<TextView
android:id="@+id/name"
android:text="条目1111111"
android:layout_marginLeft="10dp"
android:textColor="#222222"
android:layout_width="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ class ItemDelegateManager<T> {
}
}
return 0
// throw IllegalArgumentException(
// "No ItemDelegate added that matches position=$position in data source")

}

fun convert(holder: ViewHolder, item: T, position: Int, payloads: List<Any>? = null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ open class MultiItemTypeAdapter<T>(var data: List<T>) : RecyclerView.Adapter<Vie

val layoutId = itemViewDelegate.getLayoutId()
val holder = ViewHolder.createViewHolder(parent.context, parent, layoutId)
onViewHolderCreated(holder, holder.convertView)
onViewHolderCreated(holder, holder.itemView)
setListener(parent, holder, viewType)
return holder
}
Expand All @@ -64,14 +64,14 @@ open class MultiItemTypeAdapter<T>(var data: List<T>) : RecyclerView.Adapter<Vie

protected fun setListener(parent: ViewGroup, viewHolder: ViewHolder, viewType: Int) {
if (!isEnabled(viewType)) return
viewHolder.convertView.setOnClickListener { v ->
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)
Expand Down
10 changes: 5 additions & 5 deletions easy-adapter/src/main/java/com/lxj/easyadapter/ViewHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<View> = SparseArray()

fun <T : View> 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
Expand All @@ -24,20 +24,20 @@ class ViewHolder(val convertView: View) : RecyclerView.ViewHolder(convertView) {
fun <T : View> 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<TextView>(viewId)
val tv = getViewOrNull<TextView>(viewId)
tv?.text = text
return this
}

fun setImageResource(viewId: Int, resId: Int): ViewHolder {
val view = getView<ImageView>(viewId)
val view = getViewOrNull<ImageView>(viewId)
view?.setImageResource(resId)
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object WrapperUtils {
return fn(layoutManager, spanSizeLookup, position)
}
}
layoutManager.spanCount = layoutManager.spanCount
// layoutManager.spanCount = layoutManager.spanCount
}
}

Expand Down

0 comments on commit 0fdb841

Please sign in to comment.