【问题标题】:Android RelativeLayout: Show a list of ConstraintLayoutsAndroid RelativeLayout:显示 ConstraintLayouts 列表
【发布时间】:2019-06-28 09:19:48
【问题描述】:

我有一个名为R.layout.new_planConstraintLayout 文件,其中包含显示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&lt;PlanItem&gt;,我想显示此列表,使用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


【解决方案1】:

我需要做的关键是将planItemView id 设置为我的应用中从未使用过的 id。所以我创建了一个其他视图不太可能使用的数字常量,并将其用作第二个、第三个、第四个等计划项目视图的基础。

之后,RelativeLayout 根将正确显示个人 plan item view

  1. 添加到类的顶部

    companion object {
        private const val BASE_PLAN_ITEM_ID = 164
    }
    
  2. 更新方法

    private fun PlanItem.addToRootView(
        rootView: RelativeLayout,
        pos: Int,
        r: Resources
    ) {
        val planItemView: ConstraintLayout = inflater
            .inflate(R.layout.new_plan, rootView, false) as ConstraintLayout
    
        planItemView.id = BASE_PLAN_ITEM_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 (planItemView.id > BASE_PLAN_ITEM_ID) {
            idOfBelow = planItemView.id - 1
    
            // If it is the 2nd or more item in the list, add a margin above it
            params.setMargins(
                params.leftMargin,
                calculatePx(16, r),
                params.rightMargin,
                params.bottomMargin
            )
        } else {
            idOfBelow = rootView.id
        }
        params.addRule(RelativeLayout.BELOW, idOfBelow)
    
        rootView.addView(planItemView, params)
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 2017-11-25
    相关资源
    最近更新 更多