在另一个 RelativeLayout 中膨胀 RelativeLayout

Inflating RelativeLayout within another RelativeLayout

我正在尝试在单个自定义列表视图 layout.xml 文件中包含自定义列表视图的布局模型,并使用 kotlin 使用自定义适配器 class 对其进行扩充。

数据似乎没有问题,行格式准确无误,但每行重复 header。我猜想我可以将 header 移出列表视图 layout.xml 文件,但我更愿意保留它的独立性,因此它可能是 re-used.

这是有问题的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- Header aligned to top -->

<RelativeLayout
    android:id="@+id/estimated_ftp_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@color/colorHeader"
    android:gravity="start" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:text="@string/estimated_ftp"
        android:textColor="#000"
        android:textSize="20sp" />
</RelativeLayout>

<!-- Content below header -->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:layout_below="@id/estimated_ftp_header"
    android:orientation="horizontal"
    android:padding="6dip"
    android:id="@+id/estimated_ftp_row">

    <ImageView
        android:contentDescription="@string/ftp_type"
        android:id="@+id/ftp_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/centerPoint"
        android:gravity = "center|end|end"
        android:src="@drawable/glyphicons_13_heart"/>

    <TextView
        android:id="@+id/centerPoint"
        android:text=""
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/ftp_value"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/centerPoint"
        android:gravity = "center|start|start"
        android:text="141 bpm"/>

</RelativeLayout>

我相信解决方案是弄清楚如何 select estimated_ftp_row id 并将其膨胀而不是整个 estimated_fto.xml 文件。

这是适配器中的代码,select在 getView 方法中展开之前的布局:

val inflater = activity?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
view = inflater.inflate(R.layout.estimated_ftp, null)
viewHolder = ViewHolder(view)
view?.tag = viewHolder

如何更改 inflater.inflate 以在 R.layout.estimated_ftp 内膨胀某些内容?

FWIW,activity 的 XML 布局在用作占位符的位置有一个 ListView 控件。它不引用自定义布局文件。该连接是通过上述适配器 class 完成的。不确定这是不是问题所以我想提一下...

提前感谢您的任何建议。

麦克

解决方案更新: https://whosebug.com/users/2850651/mike-m 得到了我需要的指导。我能够将行格式与 header 格式 XML 布局分开。适配器未更改,Activity 有新代码添加 header 如果找到相关列表的内容。

val headerView = layoutInflater.inflate(R.layout.estimated_ftp_head, null)
estimated_ftp_list.addHeaderView(headerView)

解决方案:

https://whosebug.com/users/2850651/mike-m 得到了我需要的指导。我能够将行格式与 header 格式 XML 布局分开。适配器未更改,Activity 有新代码添加 header 如果找到相关列表的内容。

val headerView = layoutInflater.inflate(R.layout.estimated_ftp_head, null)
estimated_ftp_list.addHeaderView(headerView)