【发布时间】:2019-06-28 09:19:48
【问题描述】:
我有一个名为R.layout.new_plan 的ConstraintLayout 文件,其中包含显示PlanItem 对象所需的视图。我想在RelativeLayout 根视图中显示一个列表 R.layout.new_plan。
我的RelativeLayout parent/root 是这样的:
<RelativeLayout
android:id="@+id/rl_plan_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<!-- other properties here -->
</RelativeLayout>
换句话说,我有一个List<PlanItem>,我想显示此列表,使用R.layout.new_plan 视图来显示rl_plan_items 中的每个PlanItem。我目前的做法是循环列表中的每个PlanItem,根据当前PlanItem的属性在R.layout.new_plan中设置TextViews,并以某种方式使用RelativeLayout.LayoutParams将其添加到rl_plan_items .
代码是这样的:
private fun PlanItem.addToRootView(rootView: RelativeLayout, pos: Int) {
val planItemView: ConstraintLayout = inflater
.inflate(R.layout.new_plan, rootView, false) as ConstraintLayout
planItemView.id = pos
with(planItemView) {
tv_title.text = name
tv_desc.setTextAndGoneIfEmpty(description)
tv_date.text = getDateToDisplay(resources)
tv_subject.setTextAndGoneIfEmpty(subject?.name)
}
val params = planItemView.layoutParams as RelativeLayout.LayoutParams
val idOfBelow: Int = if (pos > 0) planItemView.id - 1 else rootView.id
params.addRule(RelativeLayout.BELOW, idOfBelow)
rootView.addView(planItemView, params)
}
在我的片段中:
for (i in planItems.indices) {
planItems[i].addToRootView(rl_plan_items, i)
}
这确实显示了所有 PlanItem,但它们显示在彼此的顶部,而不是向下显示。
如何按顺序显示计划项目视图,而不是一起显示?
【问题讨论】:
-
为什么不为此使用回收站视图?
-
我确实为每个日期使用了 recyclerview,现在对于每个日期行我想显示一个计划列表。我不希望平面视图滚动。
-
你的意思是每个“持有者”都是一个文本视图列表?使用线性布局不会让您的生活更轻松吗?如果你真的想让每个item成为一个约束布局,你需要在每个item之间设置约束。
-
我明白了...我想因为父级是
RelativeLayout我会遵循RelativeLayout的方式在每个项目的底部对齐项目。 -
在每一行内也使用另一个回收视图
标签: android list android-relativelayout layoutparams