2

I want to show one page of pdf file from one fragment using viewpager2. So every page scroll, I will be loading a PDFView from filepath. So if i have 6 pdf files, i will store the pdf files in an array and combine them all in one pdfview by using each fragment for each pdf file.

The problem is:

When I scroll , it doesn't load and display properly. If I scroll from page 1- 6 it displays properly on the first scroll left to right. But when I go back it displays and won't display on certain pages. So here is my code:

This is my MainActivity.java


  public class MainActivity extends AppCompatActivity {


    private ViewPager2 viewPager;
    private PDF_FragmentAdapter adapter;
    private Context contexts;
    private List<String> list;

    private static final String TAG = MainActivity.class.getSimpleName();

    private final static int REQUEST_CODE = 42;
    public static final int PERMISSION_CODE = 42042;


    private PDFView pdfView;

    private Uri uri;

    private Integer pageNumber = 0;
     private List<String> it;

    String pdfFileName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //--------------  get num of pdf files from assets folder
        AssetManager assetManager = getAssets();
        String[] files = new String[0];
        try {
            files = assetManager.list("pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
        it = new LinkedList<String>(Arrays.asList(files));
        // System.out.println(it.size());
        for (int i = 0; i < it.size(); i++)
            Log.d("List of files .......", it.get(i).toString());


        // for encrypted pdf files
        viewPager = (ViewPager2) findViewById(R.id.pager);
        adapter = new PDF_FragmentAdapter(getSupportFragmentManager(),this.getLifecycle(),it); //pass pdf file name array
        viewPager.setOrientation(viewPager.ORIENTATION_HORIZONTAL);
        viewPager.setAdapter(adapter);
    }

}

This is my Pdf_FragmentAdapter.java


public class PDF_FragmentAdapter extends FragmentStateAdapter  {

    private List<String> List;

    SparseArray<Fragment> registeredFragments = new SparseArray<>();


    public PDF_FragmentAdapter( @NonNull FragmentManager fm, @NonNull Lifecycle lifecycle,  List<String> arrayList) {
        super(fm,lifecycle);
        this.List = arrayList;
    }


    @NonNull
    @Override
    public Fragment createFragment(int position) {
        Fragment pdf_fragment = newInstance(List.get(position));
        registeredFragments.put(position,pdf_fragment);
        return registeredFragments.get(position);
    }


    @Override
    public int getItemCount() {
        return List.size();
    } 
}

This is my PDF_Fragment.java


    public class PDF_Fragment extends Fragment {

    private PDFView pdfViewer;

    private View.OnScrollChangeListener onScrollChangeListener;
    private OnPageChangeListener onPageChangeListener;
    private Fragment fragment;
    private int pageNumber =0;
    private String pdfFileName;


    public static PDF_Fragment newInstance(String filename) {

        PDF_Fragment fragment = new PDF_Fragment();
        Bundle args = new Bundle();
        args.putString("someMessage", filename);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.fragment_pdf, container, false);
        pdfFileName = getArguments().getString("someMessage");

        return view;
    }


    @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        PDFView pdfViewer = (PDFView) view.findViewById(R.id.pdfView);

        pdfViewer.fromAsset("pdf/" + pdfFileName)
                .load();

    }
}

This is my fragment xml layout

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

        <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

This is an image of how my log looks like when i scroll the viewpager

  • Have the same issue Could someone help to fix it? – Edhar Khimich Sep 16 '20 at 20:05
  • the pdf viewer is pretty big - you can keep 1 copy of it, and the pager/fragments can hold the pdf details. On each page change, you can instruct the pdf viewer to load the new pdf. – jj. Sep 22 '20 at 21:30

1 Answers1

1

I had exactly the same problem, fragment was destroying when I was scrolling to another fragment. To keep fragments

pager.offscreenPageLimit = 3 // The number of fragments which should be kept

-3, -2, -1, current, 1, 2, 3

If you set 3, max. viewpage will keep 3 fragments on either side

Adizbek Ergashev
  • 732
  • 8
  • 17