RecycleView在PDF(PdfDocument)中不可见

时间:2019-09-08 15:41:14

标签: android android-recyclerview pdfdocument

我正在尝试将RecycleView添加到PDF文档中,但是它是不可见的。我在Activity中测试了StatisticsViewHolder的{​​{1}}和StatisticsAdapter,并且可以正常工作,所以我确定问题出在设置RecycleView或pdf_layout.xml。

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

设置pdf_layout.xml

RecycleView

pdf_layout.xml

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);

我不确定是否缺少某些内容,或者<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的一部分

1 个答案:

答案 0 :(得分: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调用的函数。