Android-PdfMyXml
Android-PdfMyXml copied to clipboard
print a view with a recyclerview
how can i print a view with a recyclerview ?
I tried many times. Recyclerview shows only items based on screen size. to view all items use ListView with custom ArrayAdapter (this worked with me).
I tried many times. Recyclerview shows only items based on screen size. to view all items use ListView with custom ArrayAdapter (this worked with me).
@jwhijazi can you give me a sample? i tried to use a ListView with a ArrayAdapter but don't work
page1.xml (layout of the page). `
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="2480px" android:layout_height="3508px" android:layout_marginLeft="50px" android:layout_marginRight="50px" android:orientation="vertical" android:background="@color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100px"
android:layout_marginTop="50px"
android:orientation="vertical">
<ListView
android:id="@+id/lv_expensePerCategories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
`
CustomAdapter.java
`
public class CategoryExpensesListAdapter extends ArrayAdapterprivate Context context;
private List<MonthlyExpense> mData;
public CategoryExpensesListAdapter(Context context, int resource, ArrayList<MonthlyExpense> objects) {
super(context, resource, objects);
this.context = context;
this.mData = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.monthly_expenses_list_item, null);
final MonthlyExpense me = mData.get(position);
final TextView tv_catIcon;
final ImageView img_catIcon;
TextView tv_catName;
CircleProgressView cpv_Percentage;
TextView tv_categoryTotalAmount;
tv_catIcon = view.findViewById(R.id.tv_catIcon);
img_catIcon = view.findViewById(R.id.img_categoryIcon);
tv_catName = view.findViewById(R.id.tv_catName);
tv_categoryTotalAmount = view.findViewById(R.id.tv_categoryTotalAmount);
cpv_Percentage = view.findViewById(R.id.cpv_Percentage);
tv_categoryTotalAmount.setText(me.getAmount());
tv_catName.setText(me.getCategoryName());
tv_catName.setTextColor(Color.parseColor(me.getCategoryColor()));
tv_catIcon.setTextColor(Color.parseColor(me.getCategoryColor()));
return view;
}
} `
Code in main activity to generate PDF ` final PdfDocument doc = new PdfDocument(context);
Thread superThread = new Thread(new Runnable() {
@Override
public void run() {
AbstractViewRenderer page = new AbstractViewRenderer(context, R.layout.page1) {
private List<MonthlyExpense> expenses;
@Override
protected void initView(View view) {
listView = view.findViewById(R.id.lv_expensePerCategories);
expenses = new ArrayList<MonthlyExpense>();
//region Expenses per category
expenses = db.expenseDao().getMonthlyExpenses(month, year, -1);
ArrayAdapter<MonthlyExpense> adapter = new CategoryExpensesListAdapter(context, 0, (ArrayList<MonthlyExpense>) expenses);
listView.setAdapter(adapter);
//endregion
}
};
doc.setRenderWidth(2480);
doc.setRenderHeight(3508);
doc.setOrientation(PdfDocument.A4_MODE.PORTRAIT);
doc.addPage(page);
doc.setSaveDirectory(context.getExternalFilesDir(null));
doc.setInflateOnMainThread(false);
doc.setListener(new PdfDocument.Callback() {
@Override
public void onComplete(File file) {
Log.i(PdfDocument.TAG_PDF_MY_XML, "Complete");
}
@Override
public void onError(Exception e) {
Log.i(PdfDocument.TAG_PDF_MY_XML, "Error");
}
});
}
});
superThread.start();
doc.setProgressTitle(R.string.gen_please_wait);
doc.setProgressMessage(R.string.gen_pdf_file);
doc.setFileName("mytest_pdf");
doc.createPdf(context);
`
I think the proble that the Recyclerview always recycle and reuse its view holders. So when you scroll down , the previous page is not present at all (had been reused in the current page). that is why you can't print
try this to stop recycling your views:
viewHolder.setIsRecyclable(false);
https://stackoverflow.com/questions/36313079/i-want-my-recyclerview-to-not-recycle-some-items/36313437
https://stackoverflow.com/questions/42209168/how-to-make-recyclerview-stops-recycling-defined-positions
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html#setIsRecyclable(boolean) N.B: not tested
@tabebqena @fbertanha @jwhijazi @se-bastiaan @Arkar-Aung Hello, I'm maintaining a library whose concept is also based on this library. I think your issue will be solved with it. https://github.com/Gkemon/Android-XML-to-PDF-Generator