Reputation: 575
I'm currently testing my code for situations where a RecyclerView.Adapter
needs additional information to be displayed in it. Using the code below which is inside onBindViewHolder
, I was able to make this work.
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);
That code is always called inside OnBindViewHolder
to simulate that each item have additional info in them. If I keep creating an item by pressing a Button
, the additional info increases for each item.
How do I target the specific adapter's layout while using DataBinding
?
This is the layout that I am using for 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>
I want to add a TableRow
with 2 TextView
s in tblTicketItemDetails
if needed. What is the best way to do this? If I can't do it while using DataBinding
, then I would switch to the old way.
Upvotes: 0
Views: 503
Reputation: 4371
I usually add dynamic views like this using 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);
}
}
Modify TableLayout in xml,
<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}">
Declare a new variable in xml,
<variable
name="position"
type="int" />
Send position from Java to xml
holder.binding.setVariable(BR.position, position);
or
holder.binding.setPosition(position);
Upvotes: 1