Skip to content

Instantly share code, notes, and snippets.

@cesclong
Last active June 23, 2021 07:18
Show Gist options
  • Save cesclong/f798984240327a4aeeb5f889e520e06b to your computer and use it in GitHub Desktop.
Save cesclong/f798984240327a4aeeb5f889e520e06b to your computer and use it in GitHub Desktop.
响应生命周期的ViewHolder和Adapter
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import app.keima.android.recyclerviewsandbox.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.recyclerView.apply {
adapter = MyAdapter(
arrayOf(
"A", "B", "C", "D",
"A", "B", "C", "D",
"A", "B", "C", "D",
"A", "B", "C", "D"
)
)
}
}
}
class MyAdapter(private val dataset: Array<String>) :
LifecycleRecyclerAdapter<MyAdapter.MyViewHolder>() {
class MyViewHolder(private val textView: TextView) : LifecycleViewHolder(textView) {
private val observer = MyObserver()
init {
lifecycle.addObserver(observer)
}
fun bindData(data: String) {
textView.text = data
observer.data = data
}
}
class MyObserver() : LifecycleObserver {
var data: String = "?"
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate() {
Log.d("MyObserver", "appear: $data")
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
Log.d("MyObserver", "disappear: $data")
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(TextView(parent.context).apply {
setPadding(8, 40, 8, 40)
})
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.bindData("${dataset[position]} $position")
}
override fun getItemCount() = dataset.size
}
import androidx.recyclerview.widget.RecyclerView
abstract class LifecycleRecyclerAdapter<T : LifecycleViewHolder> : RecyclerView.Adapter<T>() {
override fun onViewAttachedToWindow(holder: T) {
super.onViewAttachedToWindow(holder)
holder.onAppear()
}
override fun onViewDetachedFromWindow(holder: T) {
super.onViewDetachedFromWindow(holder)
holder.onDisappear()
}
}
import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.recyclerview.widget.RecyclerView
abstract class LifecycleViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView), LifecycleOwner {
private val lifecycleRegistry = LifecycleRegistry(this)
init {
lifecycleRegistry.currentState = Lifecycle.State.INITIALIZED
}
fun onAppear() {
lifecycleRegistry.currentState = Lifecycle.State.CREATED
}
fun onDisappear() {
lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
}
override fun getLifecycle(): Lifecycle {
return lifecycleRegistry
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment