This gist snippet is Public Domain.
Last active
March 31, 2024 21:58
-
-
Save keima/1b8cda30aec8cd50fec7743d2ccfa777 to your computer and use it in GitHub Desktop.
LifecycleOwner implemented RecyclerView ViewHolder & Adapter (concept design)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
It will cause memory leak, when RecyclerView detach from window, the onViewDetachedFromWindow callback will not invoked.