https://kotlinlang.org logo
#compose
Title
# compose
i

Indu

04/06/2022, 11:37 PM
RecyclerView - ComposeView as view holder Not sure why the screen is not recomposed consistently during orientation change. When the device is rotated to landscape mode the screen sometimes displays the same width as in portrait mode for the recycler view. This does not happen all the time.
😯 1
The activity handles screen orientation changes. So in manifest android:configChanges="orientation” is set.
The fragment receives onConfigurationChanged() call whenever there is an orientation change. The log statement in compose view holder is not displayed every time the onConfigurationChanged() is called.
Sample code
Copy code
class SampleAdapter: ComposeListAdapter<ArrayList<ArrayList<String>>, SampleAdapter.SampleViewHolder>(VisitDiffCallback()) {

    class SampleViewHolder(
        composeView: ComposeView
    ) : ComposeViewHolder(composeView) {
        @Composable
        override fun ViewHolder() {
            val historyItems = ReminderUtil.getHistory()
            Column(
                Modifier
                    .fillMaxWidth()
                    .padding(16.dp)) {
                historyItems.forEachIndexed{ index, history ->
                    Card(backgroundColor = Color.Yellow){
                        LazyRow(
                            Modifier.fillMaxWidth(),
                            contentPadding = PaddingValues(horizontal = 8.dp),
                            horizontalArrangement = Arrangement.spacedBy(8.dp)
                        ) {
                            itemsIndexed(history) { index, items ->
                                Column(
                                    Modifier
                                        .fillMaxWidth()
                                        .padding(16.dp)) {
                                    items.forEach { item ->
                                        Text(color= Color.DarkGray,text = item)
                                    }
                                }

                            }
                        }
                    }
                }
            }
        }

    }
    
    override fun onBindViewHolder(holder: SampleViewHolder, position: Int) {
        holder.bindViewHolder()
    }

    private class VisitDiffCallback : DiffUtil.ItemCallback<ArrayList<ArrayList<String>>>() {

        override fun areItemsTheSame(oldItem: ArrayList<ArrayList<String>>, newItem: ArrayList<ArrayList<String>>): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(oldItem: ArrayList<ArrayList<String>>, newItem: ArrayList<ArrayList<String>>): Boolean {
            return oldItem == newItem
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SampleViewHolder {
        return SampleViewHolder(ComposeView(parent.context))
    }
}
}
Copy code
abstract class ComposeViewHolder(val composeView: ComposeView,

) : RecyclerView.ViewHolder(composeView) {

    @Composable
    abstract fun ViewHolder()

    init {
        composeView.setViewCompositionStrategy(
            ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed
        )
    }

    fun bindViewHolder() {
        composeView.setContent {
            ViewHolder()
        }
    }
}

abstract class ComposeListAdapter<T, VH : ComposeViewHolder>(
    diffCallback: DiffUtil.ItemCallback<T>
) : ListAdapter<T, VH>(diffCallback) {

    override fun onViewRecycled(holder: VH) {
        holder.composeView.disposeComposition()
        super.onViewRecycled(holder)
    }
}
112 Views