使用GridLayoutManager将Recycler View转换为Pdf

时间:2018-10-08 05:53:23

标签: android gridview pdf-generation

我正在使用以下代码从Recycler视图生成pdf,

         public void generatePDF(RecyclerView view) {
        RecyclerView.Adapter adapter = view.getAdapter();
       Bitmap bigBitmap = null;
        if (adapter != null) {
            int size = adapter.getItemCount();
            int height = 0;
            final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
            // Use 1/8th of the available memory for this memory cache.
            final int cacheSize = maxMemory / 8;
            LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize);
            for (int i = 0; i < size; i++) {
                RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));
                adapter.onBindViewHolder(holder, i);
            holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight());
                holder.itemView.setDrawingCacheEnabled(true);
                holder.itemView.buildDrawingCache();
                Bitmap drawingCache = holder.itemView.getDrawingCache();
                if (drawingCache != null) {
                    bitmaCache.put(String.valueOf(i), drawingCache);
                }
                height += holder.itemView.getMeasuredHeight();
            }

            bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);
            Canvas bigCanvas = new Canvas(bigBitmap);
            bigCanvas.drawColor(Color.WHITE);

            Document document = new Document(PageSize.A4);
            final File file = new File(getStorageDir(), "A.pdf");
            try {
                PdfWriter.getInstance(document, new FileOutputStream(file));
            } catch (DocumentException | FileNotFoundException e) {
                e.printStackTrace();
            }

            for (int i = 0; i < size; i++) {
                try {
                    //Adding the content to the document
                    Bitmap bmp = bitmaCache.get(String.valueOf(i));
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    Image image = Image.getInstance(stream.toByteArray());
                    image.scalePercent(85);
                    image.setAlignment(Image.LEFT);
                    if (!document.isOpen()) {
                        document.open();
                    }
                    document.add(image);

                } catch (Exception ex) {
                    Log.e("TAG-ORDER PRINT ERROR", ex.getMessage());
                }
            }
            if (document.isOpen()) {
                document.close();
            }
        // Set on UI Thread
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progressDialog.dismiss();

                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("Success")
                        .setMessage("PDF File Generated Successfully.")
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    Intent intent = new Intent(Intent.ACTION_VIEW);
                                    intent.setDataAndType(Uri.fromFile(file), "application/pdf");
                                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                                    startActivity(intent);
                            }
                        }).show();


                }
            });

        }

    }

当我使用LinearLayoutManager从recyclerview生成pdf时,此代码运行良好,但是当我使用GridLayoutmanager且每行中有3个项目时,无法像在recylerview中那样打印该项目。实际上,LinearLayoutManage和GirdLayoutManager的输出是相同的。但是我的要求是使用GridLayoutManager从recyclerview打印pdf,每行有3个项目。因此,请帮助我简化我的问题。

1 个答案:

答案 0 :(得分:0)

执行此操作:-

    mRecyclerView = findViewById(R.id.rvNumbers);
    mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
    List<String> list = new ArrayList(); list.add("A"); list.add("B"); list.add("C");
    MyAdapter myAdapter = new MyAdapter(this, list);
    mRecyclerView.setAdapter(myAdapter);