“如何使用 RecyclerView 下载 Android 布局的 PDF,捕获可见和不可见的数据?”

问题描述 投票:0回答:1

当尝试下载整个布局时,我遇到了一个问题,即下载的内容仅限于布局数据本身。此外,当从 RecyclerView 中提取数据时,下载中仅包含当前可见的数据。此限制导致无法全面捕获 RecyclerView 中的完整内容,我正在寻求有关如何确保成功下载整个布局(包括 RecyclerView 中的所有数据)的指导。

活动代码:

private fun createPdf() {
    val pdfDocument = PdfDocument()
    val pageInfo = PdfDocument.PageInfo.Builder(
        binding.downloadRecyler.width,
        binding.downloadRecyler.height,
        1
    ).create()

    val page = pdfDocument.startPage(pageInfo)
    val canvas = page.canvas
    binding.downloadRecyler.draw(canvas)
    pdfDocument.finishPage(page)

    val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
    val pdfFileName = "${userName}_PDF_$timeStamp.pdf"

    val directory = File(
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Attendance app"
    )

    if (!directory.exists()) {
        directory.mkdirs()
    }

    val pdfFile = File(directory, pdfFileName)

    try {
        pdfFile.createNewFile()
        val outputStream = FileOutputStream(pdfFile)
        pdfDocument.writeTo(outputStream)
        pdfDocument.close()
        outputStream.close()

        sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(pdfFile)))

        Toast.makeText(this, getString(R.string.pdf_downloaded_successfully), Toast.LENGTH_SHORT).show()
        showDownloadNotification(pdfFile)

    } catch (e: IOException) {
        e.printStackTrace()
        Toast.makeText(this, "Error creating PDF", Toast.LENGTH_SHORT).show()
    }
}

XML 代码:

<?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".DownloadActivity">

        <!-- ActionBar -->
       <FrameLayout
        android:id="@+id/actionBar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:layout_centerHorizontal="true"
        android:gravity="center">

        <!-- Back Button -->
        <ImageView
            android:id="@+id/btnBack"
            android:layout_width="@dimen/_20sdp"
            android:layout_height="30dp"
            android:layout_gravity="center|start"
            android:layout_marginStart="@dimen/_5sdp"
            android:src="@drawable/left_arrow" />

        <!-- Download Button -->
        <ImageView
            android:id="@+id/btnDownload"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center|end"
            android:layout_marginEnd="@dimen/_8sdp"
            android:src="@drawable/icon_download" />

    </FrameLayout>

    <!-- Main Layout -->
    <LinearLayout
        android:id="@+id/layoutDownload"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/actionBar"
        android:orientation="vertical"
        android:layout_marginHorizontal="@dimen/_10sdp">

        <!-- Logo and App Name -->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center">

            <ImageView
                android:layout_width="@dimen/_50sdp"
                android:layout_height="@dimen/_50sdp"
                android:src="@drawable/icon_logo_transparent" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/app_name"
                android:textSize="@dimen/_18ssp" />

        </LinearLayout>

        <!-- User Information -->
        <TextView
            android:id="@+id/txtName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/actionBar"
            android:layout_marginStart="@dimen/_10sdp"
            android:text="Shivam"
            android:textAllCaps="true"
            android:textColor="@color/black"
            android:textSize="@dimen/_12ssp"
            android:textStyle="bold" />

        <!-- Additional User Details -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="@dimen/_10sdp"
            android:layout_marginBottom="@dimen/_10sdp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txtPhone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Shivam"
                android:textSize="@dimen/_10ssp" />

            <TextView
                android:id="@+id/txtAge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Shivam"
                android:textSize="@dimen/_10ssp" />

            <TextView
                android:id="@+id/txtYear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Shivam"
                android:textStyle="bold"
                android:textColor="@color/black"
                android:layout_marginTop="@dimen/_10sdp"
                android:textSize="@dimen/_10ssp" />

        </LinearLayout>

        <!-- RecyclerView -->
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/downloadRecyler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="-5dp"
            android:layout_marginBottom="@dimen/_10sdp"/>

    </LinearLayout>

    <!-- Empty Data Image -->
    <ImageView
        android:id="@+id/emptyDataImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/data_empty" />

    <!-- ProgressBar -->
    <ProgressBar
        android:id="@+id/progreesBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>
android kotlin pdf android-recyclerview
1个回答
0
投票

Recyclerview 的问题是它只能绘制满一屏的足够数据。

要将 Recyclerview 绘制为 pdf,您实际上必须将 Recycler 的所有内容绘制为更合适的视图类型(线性布局很合适)。如果您想将其绘制为标准页面尺寸(如 A4 等),并意识到 PDF 充当小尺寸屏幕,您还必须了解内容的大小。

完整的工作示例https://github.com/Zardozz/RecyclerviewPdf向您展示了如何将recyclerView的内容绘制到多个标准页面。

该示例并未展示如何使回收商的内容最适合 PDF 的小“屏幕”尺寸。有多种技术,例如让 recyclerview 调整其文本绘制的字体大小,缩放图像以在 pdf 画布上设置比例。

我倾向于专门为 pdf 进行自定义布局,以获得 pdf 的最佳绘图。

© www.soinside.com 2019 - 2024. All rights reserved.