0

我正在尝试将 RecycleView 添加到 PDF 文档,但它是不可见的。我在 Activity 内部进行了测试,StatisticsViewHolder它工作正常,所以我确信问题在于设置或 pdf_layout.xml。StatisticsAdapterRecycleViewRecycleView

此外,我正在正确设置 PDF 文档,因为RecycleView它是我中唯一pdf_layout.xml没有显示的元素(为了简单起见,我删除了其他元素)。

设置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>

我不确定我是否遗漏了什么或者是否RecycleView不能成为 pdf_layout 的一部分PdfDocument

4

1 回答 1

0

事实证明,问题出在设置适配器上。我将适配器设置在“主”线程之外​​,导致出现以下错误(乍一看我错过了)

No adapter attached; skipping layout

为了解决这个问题,移动这部分代码就足够了:

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

由“主”线程(例如onCreate)或通过创建Handler可用于发布到主线程的函数调用。

于 2019-09-09T12:07:49.240 回答