android / views-widgets-samples

Multiple samples showing the best practices in views-widgets on Android.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inflate layout within ItemDecoration Recyclerview

cherimo opened this issue · comments

Hi there,

I have a basic Recyclerview with some basic items.

There are some items where I want to add a header layout on top of it.
The item_header.xml layout looks like this.

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="2dp"
        android:clickable="true"
        android:background="@color/dark_opacity"
        android:padding="0dp">
    
    
        <TextView
            android:id="@+id/item_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="30dp"
            android:textColor="@android:color/white"
            android:textSize="17sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

This is the way I call the ItemDecoration and the ItemDecoration it self.

    ....
    
    itemListContainer.addItemDecoration(ItemDecoration())
    ....
    
    class ItemDecoration() : RecyclerView.ItemDecoration() {
        
        override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
    
            for (i in 0 until parent.childCount) {
                val view = parent.getChildAt(i)
                val childPos: Int = parent.getChildAdapterPosition(view)
    
            }
    
        }
    
    }

I want to inflate item_header.xml within onDraw and show specific text in the TextView "item_title".

There is little to no recent information about this topic, the most topics are deprecated of incomplete.
Can somebody point me on the right direction for this.

I am already familiar with ViewTypes within Recyclerview but that is not what I am looking for at the moment.

Thanks in advance