How to inflate a custom view within a recyclerview view?

Asked

Viewed 79 times

-1

Following this example,I am creating an expandable recyclerview,to create a comments session and feedback responses in my app. It is possible to create a linear vertical layout, in which several textviews will be inflated.However, I need to make a more complex layout, another linear layout containing an image and a textview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <com.makeramen.roundedimageview.RoundedImageView
        android:id="@+id/roundedImageView"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_margin="4dp"
        android:scaleType="centerCrop"
        app:riv_corner_radius="5dp"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:layout_marginBottom="4dp"
        tools:text="This is a textview" />
</LinearLayout>

In the viewholder the textview are being inflated in this way, as in the example:

for (int indexView = 0; indexView < intMaxNoOfChild; indexView++) {
    TextView textView = new TextView(context);
    textView.setId(indexView);
    textView.setPadding(0, 20, 0, 20);
    textView.setGravity(Gravity.CENTER);
    textView.setBackground(ContextCompat.getDrawable(context, R.drawable.background_sub_module_text));
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    linearLayout_childItems.addView(textView, layoutParams);
}

How can I inflate this custom view and for each inflated view, assign a text and an image?

1 answer

-1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.