0

I'm using a RecyclerView with LinearLayout Manager and Horizontal orientation. It's to be used on a TV app, so it needs to be navigated with the dpad. But whenever I go do the end and come back it get stuck on the last one. See photo:

TV App

The selected ItemView is the first, but it won't go to the center. if you go to the right and the come back it will show fully.

the code:

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="320dp"
        app:layout_constraintBottom_toTopOf="@+id/tv_mini_player_view_placeholder"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/carrier_image_card"
        app:layout_constraintVertical_bias="0.10">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="@dimen/margin_start_tv_home"
            android:background="@null"
            android:clipToPadding="false"
            android:orientation="horizontal"
            android:padding="@dimen/margin_medium_big"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            tools:context=".android.screens.auth.SelectCarrierActivity"
            tools:listitem="@layout/imagecardview_station" />
    </androidx.core.widget.NestedScrollView>

One little thing I observed is the layout World Hits didn't adjust the label size. When selected these layouts show a description which makes the transparent grey box larger to acommodate. It's a "View.GONE" change on the description label. So the last selected layout the description disappeared but the layout didn't readjusted. The code for the View Holder part is below:

inner class StationViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val mainImageView: ImageView by lazy { itemView.findViewById<ImageView>(R.id.main_image) }
    val titleView: TextView by lazy { itemView.findViewById<TextView>(R.id.title_text) }
    val descriptionView: TextView by lazy { itemView.findViewById<TextView>(R.id.description_text) }


    init {
        val frameLayout = itemView.findViewById<FrameLayout>(R.id.frame_layout)
        val cardView = itemView.findViewById<FrameLayout>(R.id.cardview)
        cardView.apply {
            isFocusable = true
            isFocusableInTouchMode = true
            onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
                if (hasFocus) {
                    AbstractCardPresenter.animateScaleUp(cardView as View)
                    frameLayout.background = itemView.context.resources.getDrawable(R.drawable.card_border)
                    descriptionView.visibility = View.VISIBLE
                } else {
                    AbstractCardPresenter.animateScaleDown(cardView as View)
                    frameLayout.background = null
                    descriptionView.visibility = View.GONE
                }
            }
        }

        itemView.onAttachStateChangeListener {
            onViewAttachedToWindow {
                if (adapterPosition == 0) {
                    cardView.requestFocus()
                }
            }
        }
    }
}
Pedro Malta
  • 198
  • 9

2 Answers2

0

U need to get rid of NestedScrollView

  <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="@dimen/margin_start_tv_home"
        android:background="@null"
        android:clipToPadding="false"
        android:orientation="horizontal"
        android:padding="@dimen/margin_medium_big"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:context=".android.screens.auth.SelectCarrierActivity"
        tools:listitem="@layout/imagecardview_station" />
Shimaa Yasser
  • 587
  • 4
  • 12
  • Thanks for your answer, but it doesn't change anything. The NestedScrollView was there to hack the RecyclerView into loading all the layouts before, I was thinking it was a problem of not having the layout loaded before, but it appear not to be the case. Anyway, removed, no change. I observed one little thing. I'll update the question and add a little bit more code. – Pedro Malta Mar 24 '20 at 12:42
0

I found the offending bit of code!! I was trying to get focus on the first ViewHolder of the RecyclerView upon load. That was causing the problem. Just need to find another way to do it now! This bit of code right there:

    itemView.onAttachStateChangeListener {
        onViewAttachedToWindow {
            if (adapterPosition == 0) {
                cardView.requestFocus()
            }
        }
    }
Pedro Malta
  • 198
  • 9