@@ -4,7 +4,7 @@ A ViewPager and a PagerAdapter that can:
4
4
5
5
1 . AutoScroll (On/Off able)
6
6
2 . Infinite Loop (On/Off able)
7
- 3 . ViewPager's height can be wrap_content
7
+ 3 . ViewPager's height can be wrap_content / an aspect ratio
8
8
4 . Adjustable auto scroll interval
9
9
5 . Won't scroll nor loop if there is only 1 item
10
10
6 . Works well with notifyDataSetChanged()
@@ -27,7 +27,7 @@ I cannot find one that fits all of the below requirements:
27
27
2 . Last updated in less than 3 years
28
28
3 . Good infinite looping effect
29
29
4 . Configurable auto-scroll
30
- 5 . ViewPager that supports wrap_content (or at least aspect ratio)
30
+ 5 . ViewPager that supports fixed aspect ratio (Or wrap_content )
31
31
6 . Good support with Page Indicators
32
32
33
33
Especially for 6, even some of them supports, they provide built-in indicators only; or does not tell user how to implement their own indicator.
@@ -50,7 +50,7 @@ allprojects {
50
50
And then add the below to your app's build.gradle:
51
51
52
52
``` groovy
53
- implementation 'com.asksira.android:loopingviewpager:1.0.4 '
53
+ implementation 'com.asksira.android:loopingviewpager:1.0.5 '
54
54
```
55
55
56
56
### Step 1: Create LoopingViewPager in XML
@@ -63,15 +63,22 @@ And then add the below to your app's build.gradle:
63
63
app : isInfinite =" true"
64
64
app : autoScroll =" true"
65
65
app : scrollInterval =" 5000"
66
- app : wrap_content = " true " />
66
+ app : viewpagerAspectRatio = " 1.33 " />
67
67
```
68
68
69
- | Attribute Name | Default | Allowed Values |
70
- | :-----------------| :--------| :------------------------------|
71
- | isInfinite | false | true / false |
72
- | autoScroll | false | true / false |
73
- | wrap_content | true | true / false |
74
- | scrollInterval | 5000 | any integer (represents ms) |
69
+ | Attribute Name | Default | Allowed Values |
70
+ | :------------------------| :--------| :------------------------------|
71
+ | isInfinite | false | true / false |
72
+ | autoScroll | false | true / false |
73
+ | viewpagerAspectRatio | 0 | any float (width / height) |
74
+ | wrap_content | true | true / false |
75
+ | scrollInterval | 5000 | any integer (represents ms) |
76
+
77
+ viewpagerAspectRatio 0 means does not apply aspectRatio.
78
+ That means, default LoopingViewPager has no aspect ratio and wrap_content is true.
79
+ Once aspect ratio is set, wrap_content will be overrided (meaningless).
80
+
81
+ In most cases, you should set an aspect ratio.
75
82
76
83
If you wonder why you need to set ` app:wrap_content="true" ` , take a look at [ this Stackoverflow post] ( https://stackoverflow.com/questions/8394681/android-i-am-unable-to-have-viewpager-wrap-content ) .
77
84
@@ -80,7 +87,7 @@ If you wonder why you need to set `app:wrap_content="true"`, take a look at [thi
80
87
You should
81
88
1 . Specify the data type in the generic (` <DataType> ` )
82
89
2 . Create your own constructor according to this ` DataType `
83
- 3 . override ` getItemView `
90
+ 3 . override ` inflateView() ` and ` bindView() `
84
91
85
92
``` java
86
93
public class DemoInfiniteAdapter extends LoopingPagerAdapter<Integer > {
@@ -89,22 +96,21 @@ public class DemoInfiniteAdapter extends LoopingPagerAdapter<Integer> {
89
96
super (context, itemList, isInfinite);
90
97
}
91
98
92
- // You should return the View (With data binded) to display in this method .
99
+ // This method will be triggered if the item View has not been inflated before .
93
100
@Override
94
- protected View getItemView (View convertView , int listPosition , ViewPager container ) {
95
- if (convertView == null ) {
96
- convertView = LayoutInflater . from(context). inflate(R . layout. item_pager, null );
97
- container. addView(convertView);
98
- }
99
-
100
- // Bind your view elements with data in itemList here.
101
- // You can also consider using a ViewHolder pattern.
102
- // Below is just an example in the demo app.
101
+ protected View inflateView () {
102
+ return LayoutInflater . from(context). inflate(R . layout. item_pager, null );
103
+ }
104
+
105
+ // Bind your data with your item View here.
106
+ // Below is just an example in the demo app.
107
+ // You can assume convertView will not be null here.
108
+ // You may also consider using a ViewHolder pattern.
109
+ @Override
110
+ protected void bindView (View convertView , int listPosition ) {
103
111
convertView. findViewById(R . id. image). setBackgroundColor(context. getResources(). getColor(getBackgroundColor(listPosition)));
104
112
TextView description = convertView. findViewById(R . id. description);
105
113
description. setText(String . valueOf(itemList. get(listPosition)));
106
-
107
- return convertView;
108
114
}
109
115
}
110
116
```
@@ -234,6 +240,11 @@ if you cannot accept these minor defects, I suggest you use `onIndicatorPageChan
234
240
235
241
## Release notes
236
242
243
+ v1.0.5
244
+ - Added asepct ratio attribute for ` LoopingViewPager `
245
+ - Rewrote the way of caching Views in ` LoopingPagerAdapter ` , and therefore separated inflation and data binding
246
+ - Rewrote the way of implementing ViewPager wrap_content
247
+
237
248
v1.0.4
238
249
- Indicator now works with page skipping as well (By calling ` selectCurrentItem() ` )
239
250
- Leviated indicator fluctuating phenomenon when using ` onIndicatorProgress() ` callback
0 commit comments