【发布时间】:2018-04-18 18:05:10
【问题描述】:
我正在使用具有线性布局管理器水平方向的 RecyclerView 来制作水平滚动视图。在我的 ViewHolder 中,我使用 RelativeLayout,在布局的底部我有两个小图像“img_1”和“img_2”,其中“img_2”与 parentEnd 对齐,“img_1”使用“android: layout_toStartOf”。当我运行该应用程序时,“img_1”毫无例外地神奇地消失了。在“设计”XML 预览中一切正常,在实施 RecyclerView 之前,我的布局运行良好。当我删除“android:layout_toStartOf”属性时,“img_1”就在那里。我尝试将“android:layout_toStartOf”添加到子 RelativeLayout,它也消失了。
我猜这是我的 RecycleViewAdapter 中的 LayoutInflater 的一个错误,它不知道如何处理“android:layout_toStartOf”。
我试图搜索错误或类似问题,但没有找到任何相关的内容。
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?) {}
}
}
【问题讨论】:
标签: android android-recyclerview layout-inflater android-relativelayout recyclerview-layout