Skip to content

Commit 156fa0c

Browse files
troZeePiotr Trockialpha0010ptrockiMateWW
authored
Merge next branch into master (#289)
Co-authored-by: Piotr Trocki <[email protected]> Co-authored-by: Alpha <[email protected]> Co-authored-by: ptrocki <[email protected]> Co-authored-by: Mateusz Wit <[email protected]> Co-authored-by: Mateusz Wit <[email protected]> Co-authored-by: Arkadiusz Kubaczkowski <[email protected]> Co-authored-by: Arkadiusz Kubaczkowski <[email protected]>
1 parent adbc1d2 commit 156fa0c

31 files changed

+730
-857
lines changed

README.md

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# react-native-viewpager
1+
# react-native-viewpager <img src="docs/viewpager-logo.png" alt="ViewPager" width="24" height="24">
22

33
[![CircleCI branch](https://img.shields.io/circleci/build/github/callstack/react-native-viewpager/master.svg)](https://circleci.com/gh/callstack/react-native-viewpager/tree/master)
44
[![npm package](https://badge.fury.io/js/%40react-native-community%2Fviewpager.svg)](https://badge.fury.io/js/%40react-native-community%2Fviewpager)
@@ -7,27 +7,25 @@
77

88
This component allows the user to swipe left and right through pages of data. Under the hood it is using the native [Android ViewPager](https://developer.android.com/reference/android/support/v4/view/ViewPager) and the [iOS UIPageViewController](https://developer.apple.com/documentation/uikit/uipageviewcontroller) implementations. [See it in action!](https://github.com/react-native-community/react-native-viewpager#preview)
99

10-
<img src="docs/viewpager-logo.png" alt="ViewPager" width="500" height="500">
11-
12-
## Versions
13-
14-
| 1.x | 2.x | 3.x | 4.0.x, 4.1.x | >= 4.2.x |
15-
| ---------------- | ------------- | ------------- | ------------- | ------------- |
16-
| | iOS support | iOS support | iOS support | iOS support |
17-
| Android support | Android support | AndroidX support | ViewPager2 support | [Reverted to 3.3.0](https://github.com/callstack/react-native-viewpager/issues/233#issue-711000654) |
10+
<br/>
11+
<p align="center">
12+
<img src="docs/vp-carousel.gif" alt="ViewPager" width="300">
13+
</p>
1814

15+
<br/>
1916

20-
## Experimental version
21-
22-
[ViewPager2](https://developer.android.com/jetpack/androidx/releases/viewpager2) for Android uses another implementation than ViewPager1, hence it caused lots of issues. You can try experimental version using below command
17+
## Versions
2318

24-
`yarn add @react-native-community/viewpager@next`
19+
| 3.x | ~~4.0.x, 4.1.x~~ | >= 4.2.x | 5.x
20+
| ------------- | ------------- | ------------- | ------------- |
21+
| iOS support | Deprecated | iOS support | iOS support |
22+
| ViewPager1 support | Deprecated | [Reverted to 3.3.0](https://github.com/callstack/react-native-viewpager/issues/233#issue-711000654) | ViewPager2 support |
2523

2624
## Getting started
2725

2826
`yarn add @react-native-community/viewpager`
2927

30-
## Linking
28+
## Linking
3129

3230
### >= 0.60
3331

@@ -50,6 +48,7 @@ Follow the [instructions in the React Native documentation](https://facebook.git
5048
```ruby
5149
pod 'react-native-viewpager', :path => '../node_modules/@react-native-community/viewpager'
5250
```
51+
5352
</details>
5453

5554
<details>
@@ -58,12 +57,14 @@ pod 'react-native-viewpager', :path => '../node_modules/@react-native-community/
5857
Make the following changes:
5958

6059
#### `android/settings.gradle`
60+
6161
```groovy
6262
include ':@react-native-community_viewpager'
6363
project(':@react-native-community_viewpager').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/viewpager/android')
6464
```
6565

6666
#### `android/app/build.gradle`
67+
6768
```groovy
6869
dependencies {
6970
...
@@ -72,6 +73,7 @@ dependencies {
7273
```
7374

7475
#### `android/app/src/main/.../MainApplication.java`
76+
7577
On top, where imports are:
7678

7779
```java
@@ -89,6 +91,7 @@ protected List<ReactPackage> getPackages() {
8991
);
9092
}
9193
```
94+
9295
</details>
9396

9497
## Usage
@@ -119,10 +122,11 @@ const styles = StyleSheet.create({
119122
```
120123

121124
**Attention:** Note that you can only use `View` components as children of `ViewPager` (cf. folder */example*)
122-
. For Android if `View` has own children, set prop `collapsable` to false https://reactnative.dev/docs/view#collapsable, otherwise react-native might remove those children views and and it's children will be rendered as separate pages
125+
. For Android if `View` has own children, set prop `collapsable` to false <https://reactnative.dev/docs/view#collapsable>, otherwise react-native might remove those children views and and it's children will be rendered as separate pages
123126

124127
## Advanced usage
125-
For advanced usage please take a look into our [example project](https://github.com/callstack/react-native-viewpager/blob/master/example/src/BasicViewPagerExample.tsx)
128+
129+
For advanced usage please take a look into our [example project](https://github.com/callstack/react-native-viewpager/blob/master/example/src/BasicViewPagerExample.tsx)
126130

127131
## API
128132

@@ -139,6 +143,37 @@ For advanced usage please take a look into our [example project](https://github.
139143
|`transitionStyle: TransitionStyle`|Use `scroll` or `curl` to change transition style (it does **not** work dynamically)|iOS
140144
|`showPageIndicator: boolean`|Shows the dots indicator at the bottom of the view|iOS
141145
|`overScrollMode: OverScollMode`|Used to override default value of overScroll mode. Can be `auto`, `always` or `never`. Defaults to `auto`|Android
146+
|`offscreenPageLimit: number`|Set the number of pages that should be retained to either side of the currently visible page(s). Pages beyond this limit will be recreated from the adapter when needed. Defaults to RecyclerView's caching strategy. The given value must either be larger than 0.|Android
147+
|`overdrag: boolean`|Allows for overscrolling after reaching the end or very beginning or pages|iOS
148+
149+
## Development workflow
150+
151+
To get started with the project, run `yarn bootstrap` in the root directory to install the required dependencies.
152+
153+
```sh
154+
yarn bootstrap
155+
```
156+
157+
While developing, you can run the example to check your changes
158+
159+
```sh
160+
cd example
161+
yarn android
162+
yarn ios
163+
```
164+
165+
Before sending a pull rquest, make sure your code passes TypeScript and ESLint. Run the following to verify:
166+
167+
```sh
168+
yarn typescript
169+
yarn lint
170+
```
171+
172+
To fix formatting errors, run the following:
173+
174+
```sh
175+
yarn lint --fix
176+
```
142177

143178
## Preview
144179

android/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ repositories {
3838
dependencies {
3939
//noinspection GradleDynamicVersion
4040
api 'com.facebook.react:react-native:+'
41+
implementation 'androidx.viewpager2:viewpager2:1.0.0'
4142
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.reactnativecommunity.viewpager;
2+
3+
import android.view.View;
4+
import android.view.ViewGroup;
5+
6+
import androidx.annotation.NonNull;
7+
import androidx.fragment.app.Fragment;
8+
import androidx.fragment.app.FragmentActivity;
9+
import androidx.viewpager2.adapter.FragmentStateAdapter;
10+
11+
12+
import java.util.ArrayList;
13+
import java.util.Collections;
14+
import java.util.List;
15+
16+
public class FragmentAdapter extends FragmentStateAdapter {
17+
private List<View> childrenViews = new ArrayList<>();
18+
public FragmentAdapter(@NonNull FragmentActivity fragmentActivity) {
19+
super(fragmentActivity);
20+
}
21+
22+
23+
@NonNull
24+
@Override
25+
public Fragment createFragment(int position) {
26+
return new ViewPagerFragment(childrenViews.get(position));
27+
}
28+
29+
@Override
30+
public int getItemCount() {
31+
return childrenViews.size();
32+
}
33+
34+
@Override
35+
public long getItemId(int position) {
36+
return childrenViews.get(position).getId();
37+
}
38+
39+
@Override
40+
public boolean containsItem(long itemId) {
41+
for(View child: childrenViews) {
42+
if((int) itemId == child.getId()) {
43+
return true;
44+
}
45+
}
46+
return false;
47+
}
48+
49+
public void addFragment(View child, int index) {
50+
childrenViews.add(index, child);
51+
notifyItemInserted(index);
52+
}
53+
54+
public void removeFragment(View child) {
55+
int index = childrenViews.indexOf(child);
56+
removeFragmentAt(index);
57+
}
58+
59+
public void removeFragmentAt(int index) {
60+
childrenViews.remove(index);
61+
notifyItemRemoved(index);
62+
}
63+
64+
public void removeAll() {
65+
childrenViews.clear();
66+
notifyDataSetChanged();
67+
}
68+
69+
public View getChildViewAt(int index) {
70+
return childrenViews.get(index);
71+
}
72+
}

0 commit comments

Comments
 (0)