I have a ConstraintLayout
file called R.layout.new_plan
which contains the Views needed to display a PlanItem
object. I'd like to display a list of R.layout.new_plan
's in a RelativeLayout
root view.
My RelativeLayout
parent/root is like so:
<RelativeLayout
android:id="@+id/rl_plan_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<!-- other properties here -->
</RelativeLayout>
In other words, I have a List<PlanItem>
and I'd like to display this list, using a R.layout.new_plan
view to display each PlanItem
, in rl_plan_items
. My current way of doing it is to loop for each PlanItem
in the list, set TextView
s in R.layout.new_plan
according to the attributes of the current PlanItem
, and adding it to rl_plan_items
using RelativeLayout.LayoutParams
somehow.
The code is like this:
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)
}
in my fragment:
for (i in planItems.indices) {
planItems[i].addToRootView(rl_plan_items, i)
}
This does show all PlanItems, but they are shown on top of each other, not going down to the bottom.
How can I display the plan item views in order going down, not crunched together?