为特定 RecyclerView.Adapter 添加动态视图

Add dynamic view to specific RecyclerView.Adapter

我目前正在测试我的代码,以应对 RecyclerView.Adapter 需要在其中显示其他信息的情况。使用下面 onBindViewHolder 中的代码,我能够完成这项工作。

TableRow row = new TableRow(mCtx);
row.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
row.setOrientation(LinearLayout.VERTICAL);

TextView lbl = new TextView(mCtx);
lbl.setText("Desc1");

TextView lbl2 = new TextView(mCtx);
lbl2.setText("54.77");

row.addView(lbl);
row.addView(lbl2);

holder.binding.tblTicketItemDetails.addView(row);

该代码总是在 OnBindViewHolder 内部调用,以模拟每个项目中都有附加信息。如果我通过按 Button 继续创建项目,每个项目的附加信息都会增加。

如何在使用 DataBinding 时定位特定适配器的布局?

这是我用于 RecyclerView.Adapter 的布局。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="ticket_item"
            type="com.example.com.TicketItem" />
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow
            android:id="@+id/tableRow6"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="8dp"
            android:weightSum="2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/lblTicketItemName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="8dp"
                android:text="@{ticket_item.description}"
                android:textColor="@color/black"
                android:textSize="18sp"
                android:textStyle="bold"
                tools:text="Test Text" />

            <TextView
                android:id="@+id/lblTicketItemPrice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="end"
                android:padding="8dp"
                android:text="@{ticket_item.price}"
                android:textColor="@color/black"
                android:textSize="18sp"
                android:textStyle="bold"
                tools:text="Test Text" />
        </TableRow>

        <TableLayout
            android:id="@+id/tblTicketItemDetails"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tableRow6">

        </TableLayout>

    </android.support.constraint.ConstraintLayout>
</layout>

如果需要,我想在 tblTicketItemDetails 中添加一个带有 2 个 TextViewTableRow。做这个的最好方式是什么?如果我在使用 DataBinding 时做不到,那么我会切换到旧方法。

我通常使用数据绑定添加这样的动态视图:

@BindingAdapter({"addDynamicView", "position"})
public static void addDynamicView(TableLayout layout, TicketItem item, int position) {
   // LayoutInflater inflater = LayoutInflater.from(layout.getContext()); // if you need layout inflater
    for (int i = 0; i <= position; i++) { // loop to whatever condition you have
        TextView lbl = new TextView(layout.getContext());
        lbl.setText("Desc1");
        layout.addView(lbl);
    }
}

修改 xml、

中的 TableLayout
 <TableLayout
            android:id="@+id/tblTicketItemDetails"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tableRow6"
            app:addDynamicView = "@{ticket_item}"
            app:position = "@{position}">

在xml、

中声明一个新变量
<variable
            name="position"
            type="int" />

发送位置从 Java 到 xml

holder.binding.setVariable(BR.position, position);

holder.binding.setPosition(position);