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
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.