Hi, I tried to create a PDF output file from Recyclerview using the iText library. After hours of struggle, I was able to create a PDF from recylerview.
Below are the classes that I used to create the PDF
Codes from the main class
private void getPrint() { ArrayList<View> viewArrayList = mAdapter.getPrintView(); // A function from Adapter class which returns ArrayList of VIEWS Document document = new Document(PageSize.A4); final File file = new File(getStorageDir("PDF"), "print.pdf"); try { PdfWriter.getInstance(document, new FileOutputStream(file)); } catch (DocumentException | FileNotFoundException e) { e.printStackTrace(); } for (int im = 0; im < viewArrayList.size(); im++) { // Iterate till the last of the array list and add each view individually to the document. try { viewArrayList.get(im).buildDrawingCache(); //Adding the content to the document Bitmap bmp = viewArrayList.get(im).getDrawingCache(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); Image image = Image.getInstance(stream.toByteArray()); image.scalePercent(70); image.setAlignment(Image.MIDDLE); if (!document.isOpen()) { document.open(); } document.add(image); } catch (Exception ex) { Log.e("TAG-ORDER PRINT ERROR", ex.getMessage()); } } if (document.isOpen()) { document.close(); } AlertDialog.Builder builder = new AlertDialog.Builder(Index.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(); }
RecyclerView generic adapter class
public class RecyclerAdapter<T, VM extends ViewDataBinding> extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> { private final Context context; private ArrayList<T> items; private int layoutId; private RecyclerCallback<VM, T> bindingInterface; private static ArrayList<View> mPrintView = new ArrayList<>(); public RecyclerAdapter(Context context, ArrayList<T> items, int layoutId, RecyclerCallback<VM, T> bindingInterface) { this.items = items; this.context = context; this.layoutId = layoutId; this.bindingInterface = bindingInterface; } public class RecyclerViewHolder extends RecyclerView.ViewHolder { VM binding; public RecyclerViewHolder(View view) { super(view); binding = DataBindingUtil.bind(view); } public void bindData(T model) { bindingInterface.bindData(binding, model); binding.executePendingBindings(); } } @Override public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(layoutId, parent, false); return new RecyclerViewHolder(v); } @Override public void onBindViewHolder(RecyclerAdapter.RecyclerViewHolder holder, int position) { T item = items.get(position); Log.e("PRINT ", holder.binding.getRoot().getId() + ""); mPrintView.add(holder.binding.getRoot()); holder.bindData(item); } @Override public int getItemCount() { if (items == null) { return 0; } return items.size(); } public static ArrayList<View> getPrintView() { return mPrintView; }
}
I am using an Arraylist called mPrintView to save views inside RecylerView. The problem arises when the USER scrolls UP and DOWM repeatedly when the data inside the ArrayList is duplicated.
Below are images of the results that I received after converting PDF
Case 1: when the user scrolls only one time

Case 2: when the user scrolls UP and DOWN several times

In the above image, you may notice that Apple is duplicating
Any help is appreciated.