Reputation: 11035
Having a recyclerView which displays items fine when it is in vertical orientation. But when change it to horizontal orientation:
recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
the item is not displayed correctly. One sample is the 3rd view (a spinner) in the RelativeLayout having
android:layout_alignParentEnd="true"
but it is displayed align with parent's left edge, which was supposed to be right side aligned (and it did when in vertical orientation).
There are views also having issue when with the horizontal recyclerView, such as the textView's width runs out of the parent view's width (which has
<TextView
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
and works fine in vertical recyclerView that confined inside its parent view)
Any special consideration is needed for using horizontal recyclerView?
the interested layout is:
<RelativeLayout
android:id="@id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@id/sponsored_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:singleLine="true"
android:textIsSelectable="false" />
<ImageView
android:id="@id/icon"
android:layout_width="6dp"
android:layout_height="6dp"
android:layout_alignBottom="@id/sponsored_text"
android:layout_toEndOf="@id/sponsored_text"
/>
<androidx.appcompat.widget.AppCompatSpinner
android:id="@id/spinner"
style="@style/Spinner"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:dropDownWidth="216dp"
android:spinnerMode="dropdown" />
Upvotes: 1
Views: 238
Reputation: 11035
It turns out that with horizontal recyclerView the measure behaviors differently, that the match_parent
in the RelativeLayout does not work for the horizontal RecyclerView. The widthMeasureSpec
is passed in with 0 in the
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)
Set the width for the relativeLayout in the onMeasure seems working, something like:
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED) {
val windowWidth = context.resources.displayMetrics.widthPixels
containerView?.let {
val lp = it.layoutParams
lp.width = windowWidth
}
}
... ...
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
Upvotes: 0