|
16 | 16 |
|
17 | 17 | package com.pranavpandey.android.dynamic.motion.adapter; |
18 | 18 |
|
19 | | -import android.content.Context; |
20 | 19 | import android.view.LayoutInflater; |
21 | 20 | import android.view.View; |
22 | 21 | import android.view.ViewGroup; |
23 | | -import android.widget.LinearLayout; |
24 | | - |
25 | | -import com.pranavpandey.android.dynamic.motion.R; |
26 | | - |
27 | | -import java.util.ArrayList; |
28 | | -import java.util.List; |
29 | 22 |
|
30 | 23 | import androidx.annotation.NonNull; |
31 | | -import androidx.viewpager.widget.PagerAdapter; |
| 24 | +import androidx.recyclerview.widget.RecyclerView; |
| 25 | + |
| 26 | +import com.pranavpandey.android.dynamic.motion.R; |
32 | 27 |
|
33 | 28 | /** |
34 | | - * A PagerAdapter to show different views as pages. |
| 29 | + * A {@link RecyclerView.Adapter} to show different views as pages. |
35 | 30 | * <p>It will be used internally to add dummy views inside the view pager. |
36 | 31 | */ |
37 | | -public class ViewPagerAdapter extends PagerAdapter { |
| 32 | +public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ViewHolder> { |
38 | 33 |
|
39 | 34 | /** |
40 | | - * A list to hold the views. |
| 35 | + * Item count for this adapter. |
41 | 36 | */ |
42 | | - private List<View> mPages; |
| 37 | + final int mPageCount; |
43 | 38 |
|
44 | | - public ViewPagerAdapter(@NonNull Context context, int pages) { |
45 | | - this.mPages = new ArrayList<>(); |
46 | | - |
47 | | - for (int i = 0; i < pages; i++) { |
48 | | - mPages.add(LayoutInflater.from(context).inflate( |
49 | | - R.layout.adm_dummy_layout, new LinearLayout(context), false)); |
50 | | - } |
| 39 | + /** |
| 40 | + * Constructor to initialize an object of this class. |
| 41 | + * |
| 42 | + * @param pageCount The item count for this adapter. |
| 43 | + */ |
| 44 | + public ViewPagerAdapter(int pageCount) { |
| 45 | + this.mPageCount = pageCount; |
51 | 46 | } |
52 | 47 |
|
53 | 48 | @Override |
54 | | - public @NonNull Object instantiateItem(@NonNull ViewGroup container, int position) { |
55 | | - View page = mPages.get(position); |
56 | | - container.addView(page); |
57 | | - return page; |
| 49 | + public @NonNull ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
| 50 | + return new ViewHolder(LayoutInflater.from(parent.getContext()) |
| 51 | + .inflate(R.layout.adm_dummy_layout, parent, false)); |
58 | 52 | } |
59 | 53 |
|
60 | 54 | @Override |
61 | | - public int getCount() { |
62 | | - return mPages.size(); |
63 | | - } |
| 55 | + public void onBindViewHolder(@NonNull ViewHolder holder, int position) { } |
64 | 56 |
|
65 | 57 | @Override |
66 | | - public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { |
67 | | - return view.equals(object); |
| 58 | + public int getItemCount() { |
| 59 | + return mPageCount; |
68 | 60 | } |
69 | 61 |
|
70 | | - @Override |
71 | | - public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { |
72 | | - container.removeView((View) object); |
| 62 | + /** |
| 63 | + * View holder to hold the dummy pages. |
| 64 | + */ |
| 65 | + static class ViewHolder extends RecyclerView.ViewHolder { |
| 66 | + |
| 67 | + /** |
| 68 | + * Root view used by this view holder. |
| 69 | + */ |
| 70 | + private final ViewGroup root; |
| 71 | + |
| 72 | + /** |
| 73 | + * Constructor to initialize views from the supplied root. |
| 74 | + * |
| 75 | + * @param view The view for this view holder. |
| 76 | + */ |
| 77 | + ViewHolder(@NonNull View view) { |
| 78 | + super(view); |
| 79 | + |
| 80 | + root = view.findViewById(R.id.adm_dummy_layout); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get the root view used by this view holder. |
| 85 | + * |
| 86 | + * @return The root view used by this view holder. |
| 87 | + */ |
| 88 | + @NonNull ViewGroup getRoot() { |
| 89 | + return root; |
| 90 | + } |
73 | 91 | } |
74 | 92 | } |
0 commit comments