android - 将动态视图添加到特定的 RecyclerView.Adapter
问题描述
我目前正在测试我的代码是否RecyclerView.Adapter
需要在其中显示其他信息的情况。使用下面的代码 inside 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
以模拟每个项目中都有附加信息。如果我通过按 a 继续创建项目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>
如果需要,我想添加一个TableRow
2TextView
秒tblTicketItemDetails
。做这个的最好方式是什么?如果我在使用时不能这样做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);
推荐阅读
- html - HTML、CSS 中的侧边栏问题
- sql-server - 搜索另一种类型的文本 SQL Server VB.Net
- git - git log 显示两个分支之间不同的提交,包括共同祖先
- msal - 从具有 Kerberos 身份验证的 Web API 调用图形 API
- javascript - ES6:如何将这个对象数组简化为唯一值?
- google-apps-script - 使用 DriveApp 移动文件同步非常慢
- django - 如何将 postgresql 数据库存储在 django - 项目文件夹中?
- mysql - 使用 SSH 在 CentOS 上恢复 MYSQL 数据库时遇到问题
- c++ - 迭代的“基于范围”调用是什么
- python - 如何保持登录状态,MySQL+Python 3+ tkinter