androidkotlinarraylistfocusxlm

How to set focus on the last element of an ArrayList<> in kotlin (Android)?


I'm new at kotlin and I would like some help in how to set focus of the scroll on the last element of an ArrayList<tags> here a look of my code

  <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/RVTags"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="12dp"
    android:visibility="gone"
    app:layout_constraintBottom_toTopOf="@id/startStop"
    app:layout_constraintTop_toBottomOf="@id/ui_power_range_inventory_layout"
    />
class FindTagsAdapter internal constructor(
    private val ServicesList: ArrayList<Tag>
) :
    RecyclerView.Adapter<FindTagViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FindTagViewHolder {
        return FindTagViewHolder(LayoutInflater.from(parent.context), parent)
    }

    override fun onBindViewHolder(holder: FindTagViewHolder, position: Int) {
        holder.bind(ServicesList[position])
    }

    override fun getItemCount(): Int {
        return ServicesList.size
    }
}

Solution

  • Solve my problem:

    RecyclerView.scrollToPosition(var.lastIndex)

    That way it scroll automatically to bottom.