0

I am trying to add RecycleView to PDF document but it's invisible. I tested StatisticsViewHolder and StatisticsAdapter for RecycleView inside Activity and it worked so I am sure that problem lies in setting up RecycleView or pdf_layout.xml.

Moreover I am setting up PDF document correctly since RecycleView is the only element in my pdf_layout.xml which doesn't show up (I removed other elements just for simplicity).

Setting up RecycleView:

PdfDocument document = new PdfDocument();

PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(2250, 1400, 1).create();

PdfDocument.Page page = document.startPage(pageInfo);

LayoutInflater inflater = (LayoutInflater)
        getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View content = inflater.inflate(R.layout.pdf_layout, null);

int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);

content.measure(measureWidth, measuredHeight);
content.layout(0, 0, page.getCanvas().getWidth(), page.getCanvas().getHeight());

RecyclerView recyclerViewPdf = content.findViewById(R.id.recyclerViewPDF);
recyclerViewPdf.setHasFixedSize(true);
double[] statistics = new double[]{0, 0, 0, 0, 0, 0};
recyclerViewPdf.setLayoutManager(new LinearLayoutManager(getActivity()));
pdfStatisticsAdapter = new StatisticsAdapter(getActivity(), statistics);
recyclerViewPdf.setAdapter(pdfStatisticsAdapter);

content.draw(page.getCanvas());
document.finishPage(page);

pdf_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewPDF"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />
</LinearLayout>

I'm not sure if I'm missing something or whether RecycleView cannot be part of pdf_layout in PdfDocument

Paweł Bęza
  • 1,375
  • 1
  • 13
  • 23
  • 1
    `wrap_content` is almost never the right answer for the height of a `RecyclerView`. Either use `match_parent`, or a fixed dimension, or switch from `LinearLayout` to `ConstraintLayout` and have the `RecyclerView` height be set by constraints. – CommonsWare Sep 08 '19 at 15:51
  • @CommonsWare Yeach I am aware of that I tried both setting `match_parent` and fixed dimension but neither of those didn't solve the problem. – Paweł Bęza Sep 08 '19 at 15:56
  • Temporarily put a background color on the `RecyclerView` in the layout, then generate your PDF and see if the color shows up where you expect. If it does, then the problem is that the `RecyclerView` is not rendering its items, or those items' sizes have problems (e.g., zero height). If the background color itself does not show up, that suggests that you are winding up with a zero-height `RecyclerView`. You might try moving your `content.measure()` and `content.layout()` calls until after you have completely populated the `content` `ViewGroup`. – CommonsWare Sep 08 '19 at 15:59
  • It is rendering issue since `onCreateViewHolder` isn't called at all while creating PDF (when I tested it in activity everything was showed up properly so I am sure it appears only while creating PDF). Do you have any idea why it happens? – Paweł Bęza Sep 08 '19 at 16:28
  • I have never tried using a `RecyclerView` in a PDF. Since a PDF cannot be scrolled or interacted with in any way, using a `RecyclerView` does not make much sense -- there is nothing to recycle. However, I do not know why it is giving you the specific symptoms that it is. – CommonsWare Sep 08 '19 at 16:32
  • Generally in my app I have some `RecycleView` in activity which contains some info which can be downloaded by user. Since I didn't want to create *n* includes of some layout in my pdf layout I wanted to create it in similar manner using `RecycleView`. So do you have any suggestion how to do it without usage of `RecycleVIew`/`ListView` and without doing multiple includes? – Paweł Bęza Sep 08 '19 at 16:45
  • Inflate the rows yourself using `LayoutInflater`, adding them to your `LinearLayout`. – CommonsWare Sep 08 '19 at 16:48
  • Yeach that will do the job, thanks for help. – Paweł Bęza Sep 08 '19 at 16:54

1 Answers1

0

As it turned out the problem was in setting adapter. I set my adapter outside "main" thread which resulted in following error (I missed that at first glance)

No adapter attached; skipping layout

To solve this problem it is enough to move this part of code:

RecyclerView recyclerViewPdf = content.findViewById(R.id.recyclerViewPDF);
recyclerViewPdf.setHasFixedSize(true);
recyclerViewPdf.setLayoutManager(new LinearLayoutManager(getActivity()));
pdfStatisticsAdapter = new StatisticsAdapter(getActivity(), statistics);
recyclerViewPdf.setAdapter(pdfStatisticsAdapter);

To function called by "main" thread(for example onCreate) or by creating Handler that can be used to post to the main thread.

Paweł Bęza
  • 1,375
  • 1
  • 13
  • 23