Android 将动态视图添加到特定的RecyclerView.Adapter
我目前正在测试我的代码,以确定RecyclerView.Adapter需要在其中显示其他信息的情况。使用下面onBindViewHolder中的代码,我能够完成这项工作Android 将动态视图添加到特定的RecyclerView.Adapter,android,android-recyclerview,android-databinding,Android,Android Recyclerview,Android Databinding,我目前正在测试我的代码,以确定RecyclerView.Adapter需要在其中显示其他信息的情况。使用下面onBindViewHolder中的代码,我能够完成这项工作 TableRow row = new TableRow(mCtx); row.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); row.s
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中调用,以模拟每个项目中都包含其他信息。如果我一直按按钮创建项目,则每个项目的附加信息都会增加
在使用数据绑定时,如何针对特定适配器的布局
这是我用于RecyclerView.Adapter的布局
如果需要,我想在tblTicketItemDetails中添加一个包含2个文本视图的TableRow。最好的方法是什么?如果在使用数据绑定时做不到这一点,那么我会切换到旧方法。我通常使用数据绑定添加动态视图,如下所示:
@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);
}
}
或
我在哪里可以叫addDynamicView?我可以按一下按钮调用它吗?你不需要手动调用它。它将由xml`app:addDynamicView=@{ticket\u item}`中的行自动调用。此外,我还添加了一些代码,以便对位置进行更改。如果需要,我可以编写完整的xml和java。让我知道。@rminaj如果有帮助,请投票回答,如果有效,请接受。我从哪里获得BR.position?在xml中声明position变量后,它将自动生成。
@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);
}
}
<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}">
<variable
name="position"
type="int" />
holder.binding.setVariable(BR.position, position);
holder.binding.setPosition(position);