Skip to content

Commit 4d8f509

Browse files
committed
Migrate to ViewPager2
1 parent daf0715 commit 4d8f509

File tree

11 files changed

+76
-61
lines changed

11 files changed

+76
-61
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ buildscript {
2222
'buildTools' : '29.0.3',
2323
'appcompat' : '1.2.0-alpha02',
2424
'constraintlayout': '2.0.0-beta4',
25-
'kotlin' : '1.3.61'
25+
'kotlin' : '1.3.61',
26+
'viewpager2' : '1.0.0'
2627
]
2728

2829
repositories {

dynamic-motion/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ android {
3636

3737
dependencies {
3838
api "androidx.constraintlayout:constraintlayout:${versions.constraintlayout}"
39+
api "androidx.viewpager2:viewpager2:${versions.viewpager2}"
3940
}
4041

4142
if (project.rootProject.file('local.properties').exists()) {

dynamic-motion/src/main/java/com/pranavpandey/android/dynamic/motion/adapter/ViewPagerAdapter.java

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,77 @@
1616

1717
package com.pranavpandey.android.dynamic.motion.adapter;
1818

19-
import android.content.Context;
2019
import android.view.LayoutInflater;
2120
import android.view.View;
2221
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;
2922

3023
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;
3227

3328
/**
34-
* A PagerAdapter to show different views as pages.
29+
* A {@link RecyclerView.Adapter} to show different views as pages.
3530
* <p>It will be used internally to add dummy views inside the view pager.
3631
*/
37-
public class ViewPagerAdapter extends PagerAdapter {
32+
public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ViewHolder> {
3833

3934
/**
40-
* A list to hold the views.
35+
* Item count for this adapter.
4136
*/
42-
private List<View> mPages;
37+
final int mPageCount;
4338

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;
5146
}
5247

5348
@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));
5852
}
5953

6054
@Override
61-
public int getCount() {
62-
return mPages.size();
63-
}
55+
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { }
6456

6557
@Override
66-
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
67-
return view.equals(object);
58+
public int getItemCount() {
59+
return mPageCount;
6860
}
6961

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+
}
7391
}
7492
}

dynamic-motion/src/main/java/com/pranavpandey/android/dynamic/motion/widget/DynamicMotionLayout.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
import androidx.annotation.Nullable;
2525
import androidx.constraintlayout.motion.widget.MotionLayout;
2626
import androidx.viewpager.widget.ViewPager;
27+
import androidx.viewpager2.widget.ViewPager2;
2728

2829
import com.pranavpandey.android.dynamic.motion.R;
2930
import com.pranavpandey.android.dynamic.motion.adapter.ViewPagerAdapter;
3031

3132
/**
32-
* A MotionLayout to perform operations according to the {@link ViewPager} page offset.
33+
* A {@link MotionLayout} to perform operations according to the {@link ViewPager2} page offset.
3334
*/
34-
public class DynamicMotionLayout extends MotionLayout implements ViewPager.OnPageChangeListener {
35+
public class DynamicMotionLayout extends MotionLayout {
3536

3637
/**
3738
* Constant for the unknown page count.
@@ -46,7 +47,7 @@ public class DynamicMotionLayout extends MotionLayout implements ViewPager.OnPag
4647
/**
4748
* View pager used by this view.
4849
*/
49-
private ViewPager mViewPager;
50+
private ViewPager2 mViewPager;
5051

5152
public DynamicMotionLayout(Context context) {
5253
this(context, null);
@@ -93,17 +94,6 @@ public void initialize() {
9394
}
9495
}
9596

96-
@Override
97-
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
98-
setProgress((position + positionOffset) / (mPageCount - 1));
99-
}
100-
101-
@Override
102-
public void onPageSelected(int position) { }
103-
104-
@Override
105-
public void onPageScrollStateChanged(int state) { }
106-
10797
/**
10898
* Set the page count for the view pager.
10999
*
@@ -120,12 +110,16 @@ public void setPageCount(int pageCount,
120110
mViewPager = findViewById(R.id.adm_view_pager);
121111
}
122112

123-
mViewPager.setAdapter(new ViewPagerAdapter(getContext(), pageCount));
124-
mViewPager.addOnPageChangeListener(this);
113+
mViewPager.setAdapter(new ViewPagerAdapter(pageCount));
114+
mViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
115+
@Override
116+
public void onPageScrolled(int position,
117+
float positionOffset, int positionOffsetPixels) {
118+
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
125119

126-
if (onPageChangeListener != null) {
127-
mViewPager.addOnPageChangeListener(onPageChangeListener);
128-
}
120+
setProgress((position + positionOffset) / (mPageCount - 1));
121+
}
122+
});
129123

130124
if (mViewPager.getAdapter() != null) {
131125
mViewPager.getAdapter().notifyDataSetChanged();
@@ -146,7 +140,7 @@ public int getPageCount() {
146140
*
147141
* @return The view pager used by this view.
148142
*/
149-
public @Nullable ViewPager getViewPager() {
143+
public @Nullable ViewPager2 getViewPager() {
150144
return mViewPager;
151145
}
152146
}

dynamic-motion/src/main/res/layout/adm_view_pager.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
limitations under the License.
1616
-->
1717

18-
<androidx.viewpager.widget.ViewPager
18+
<androidx.viewpager2.widget.ViewPager2
1919
xmlns:android="http://schemas.android.com/apk/res/android"
2020
xmlns:app="http://schemas.android.com/apk/res-auto"
2121
android:id="@+id/adm_view_pager"

sample/src/main/java/com/pranavpandey/android/dynamic/motion/sample/widget/DynamicImageView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import androidx.appcompat.widget.AppCompatImageView;
2727

2828
/**
29-
* An ImageView to apply color filter according to the supplied parameters.
29+
* An {@link AppCompatImageView} to apply color filter according to the supplied parameters.
3030
*/
3131
public class DynamicImageView extends AppCompatImageView {
3232

@@ -39,7 +39,7 @@ public DynamicImageView(@NonNull Context context, @Nullable AttributeSet attrs)
3939
}
4040

4141
public DynamicImageView(@NonNull Context context,
42-
@Nullable AttributeSet attrs, int defStyleAttr) {
42+
@Nullable AttributeSet attrs, int defStyleAttr) {
4343
super(context, attrs, defStyleAttr);
4444
}
4545

sample/src/main/res/drawable/ic_android.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
android:height="24dp"
2121
android:width="24dp"
2222
android:viewportHeight="24"
23-
android:viewportWidth="24"
24-
android:autoMirrored="true">
23+
android:viewportWidth="24">
2524

2625
<path
2726
android:fillColor="#FF000000"

sample/src/main/res/drawable/ic_launcher_foreground.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818
<vector
1919
xmlns:android="http://schemas.android.com/apk/res/android"
20+
xmlns:tools="http://schemas.android.com/tools"
2021
android:width="108dp"
2122
android:height="108dp"
2223
android:viewportHeight="108"
2324
android:viewportWidth="108"
24-
android:autoMirrored="true">
25+
android:autoMirrored="true"
26+
tools:targetApi="kitkat">
2527

2628
<path
2729
android:fillColor="@color/color_accent"

sample/src/main/res/layout/activity_dynamic_motion.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@
3838
android:contentDescription="@null"
3939
app:srcCompat="@drawable/ic_android" />
4040

41-
</com.pranavpandey.android.dynamic.motion.widget.DynamicMotionLayout>
41+
</com.pranavpandey.android.dynamic.motion.widget.DynamicMotionLayout>

sample/src/main/res/mipmap-anydpi-v26/ic_launcher.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828

2929
</foreground>
3030

31-
</adaptive-icon>
31+
</adaptive-icon>

0 commit comments

Comments
 (0)