Layout_tostartof not working after inflating RelativeLayout in RecyclerView

时间:2018-04-18 18:05:10

标签: android android-recyclerview layout-inflater android-relativelayout recyclerview-layout

I'm using RecyclerView with horizontal orientation of Linear Layout Manager for making horizontal scroll view. In my ViewHolder I use RelativeLayout, and at the bottom of my layout I have two small images "img_1" and "img_2", where "img_2" is aligned to parentEnd, and "img_1" is aligned to "img_2" using "android:layout_toStartOf". When I run the app, "img_1" magically disappears with no exceptions. Everything is ok in "Design" XML preview, and before implementing RecyclerView my layout worked perfectly. When I remove the "android:layout_toStartOf" attribute, "img_1" is there. I tried adding "android:layout_toStartOf" to a child RelativeLayout, it also disappears.

I guess that this is a bug in LayoutInflater in my RecycleViewAdapter, which doesn't know how to deal with "android:layout_toStartOf".

I tried to search for the bug or similar problems, but didn't find anything anyhow related.

ViewHolderLayout:

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingEnd="8dp">

        <ImageView
            android:id="@+id/trailerView"
            android:layout_width="300dp"
            android:scaleType="centerCrop"
            android:layout_height="142dp"
            android:layout_below="@id/description"
            android:layout_marginTop="8dp"
            android:layout_centerHorizontal="true"/>
        <TextView
            android:id="@+id/movieTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/trailerView"
            android:layout_marginTop="8dp"
            android:layout_marginStart="@dimen/activity_vertical_margin"
            android:fontFamily="sans-serif-medium"
            android:textSize="16sp"
            android:textColor="@color/white"
            android:lineSpacingExtra="4sp"
            android:text="Title"
            />

        <TextView
            android:id="@+id/movieTagline"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_below="@id/movieTitle"
            android:layout_marginStart="@dimen/activity_vertical_margin"
            android:lineSpacingExtra="0sp"
            android:singleLine="true"
            android:ellipsize="end"
            android:text="Tagline"
            android:textColor="@color/white_87"
            android:textSize="10sp" />

    <ImageView
            android:id="@+id/img_2"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignBottom="@id/movieTagline"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="@dimen/activity_vertical_margin" />

    <ImageView
        android:id="@+id/img_1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignBottom="@id/movieTagline"
        android:layout_toStartOf="@id/img_2"
        android:layout_marginEnd="4dp" />

    </RelativeLayout>

RecyclerViewAdapter:

class RecommendViewAdapter(val recommendations: Array<MovieResponse>,
                       val images: ArrayList<Drawable>)
: RecyclerView.Adapter<RecommendViewAdapter.ViewHolder>() {

override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
    holder?.title?.text = "${recommendations[position].title} (${recommendations[position].year})"
    holder?.tagline?.text = recommendations[position].tagline
    holder?.trailer?.setImageDrawable(images[position])
}

override fun getItemCount(): Int {
    return recommendations.size ?: 0
}

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
    val view = LayoutInflater.from(parent?.context).inflate(R.layout.recommendations_content, parent, false)
    return ViewHolder(view)
}

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

    var title: TextView = itemView.findViewById(R.id.movieTitle)
    var tagline: TextView = itemView.findViewById(R.id.movieTagline)
    var trailer: ImageView = itemView.findViewById(R.id.trailerView)

    init {
        itemView.setOnClickListener(this)
    }

    override fun onClick(v: View?) {}
}
}

1 个答案:

答案 0 :(得分:0)

解决了我实现ConstraintLayout而不是RelativeLayout的问题,但错误仍然存​​在?