2

enter image description here

In the image section with Favorites and the horizontal RecyclerView is one adapter, adapterFavoriteProperties, while the row with chart is another adapter, adapterChart, of the main RecyclerView. Main RecyclerView is composed of rows that coming each from different adapter that is glued with Concat Adapter.

val concatAdapter =
        ConcatAdapter(
            adapterFavoriteProperties,
            adapterChart,
            adapterMostViewedProperties,
            adapterRecommendedProperties
        )

Also adapter for chart has a lambda available in the fragment

    val chartItemClickLambda: ((Float) -> Unit) = { position ->
        println(" DashboardFragment chart CLICK $position")

    }

    val adapterChart =
        ChartContainerAdapter(chartItemClickLambda)

The section that contains title, see All and recyclerView is

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>

        <variable
            name="propertyListModel"
            type="com.smarttoolfactory.dashboard.model.PropertyItemListModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        visibilityBasedOn="@{!propertyListModel.items.isEmpty()}"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="12dp"
            android:layout_marginBottom="8dp"
            android:text="@{propertyListModel.title}"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Title" />

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/tvSeeAll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            visibilityBasedOn="@{propertyListModel.seeAll}"
            android:text="See all"
            android:textColor="#FB8C00"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@id/tvTitle"
            app:layout_constraintEnd_toEndOf="parent"
            tools:text="See all" />


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="8dp"
            android:paddingEnd="8dp"
            app:items="@{propertyListModel.items}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvTitle" />


    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

And it's adapter

class HorizontalListWithTitleAdapter(
    private val onItemClick: ((PropertyItem) -> Unit)? = null
) :
    ListAdapter<PropertyItemListModel, HorizontalItemViewHolder>(
        DefaultItemDiffCallback()
    ) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HorizontalItemViewHolder {

        return HorizontalItemViewHolder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                R.layout.layout_list_with_title,
                parent, false
            ),
            onItemClick
        )
    }

    override fun onBindViewHolder(holder: HorizontalItemViewHolder, position: Int) {
        holder.bindTo(currentList[position])
    }
}

and ViewHolder

class HorizontalItemViewHolder(
    private val binding: LayoutListWithTitleBinding,
    private val onItemClick: ((PropertyItem) -> Unit)? = null
) :
    RecyclerView.ViewHolder(binding.root) {

    fun bindTo(item: PropertyItemListModel) {

        binding.setVariable(BR.propertyListModel, item)

        binding.recyclerView.apply {

            // Set Layout manager
            this.layoutManager =
                ScaledLinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false)

            // Set RecyclerViewAdapter
            val itemListAdapter = PropertyListAdapter(R.layout.item_property_favorite, onItemClick)

            this.adapter = itemListAdapter

        }

        binding.executePendingBindings()
    }
}

This where i should have either reference of RecyclerView or have ViewHolder to call it's recyclerView's smoothScrollToPosition(position)

4

0 回答 0