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