Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print a view with a recyclerview #34

Open
fbertanha opened this issue Oct 31, 2018 · 5 comments
Open

print a view with a recyclerview #34

fbertanha opened this issue Oct 31, 2018 · 5 comments

Comments

@fbertanha
Copy link

how can i print a view with a recyclerview ?

@jwhijazi
Copy link

jwhijazi commented Nov 1, 2018

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

@fbertanha
Copy link
Author

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

@jwhijazi
Copy link

jwhijazi commented Nov 7, 2018

page1.xml (layout of the page).
`

<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 ArrayAdapter {
private 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);

`

@tabebqena
Copy link

tabebqena commented Jan 12, 2020

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

@Gkemon
Copy link

Gkemon commented Dec 15, 2022

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants