├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── layout
│ │ │ │ ├── grid_row.xml
│ │ │ │ ├── linear_row.xml
│ │ │ │ ├── progress_loading.xml
│ │ │ │ ├── layout_recyclerview.xml
│ │ │ │ ├── staggered_row.xml
│ │ │ │ └── activity_main.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── recyclerview
│ │ │ │ └── loadmore
│ │ │ │ ├── widget
│ │ │ │ ├── SquareImageView.java
│ │ │ │ ├── StaggerdSpacesItemDecoration.java
│ │ │ │ └── GridSpacingItemDecoration.java
│ │ │ │ ├── listener
│ │ │ │ ├── OnLoadMoreListener.java
│ │ │ │ └── RecyclerViewLoadMoreScroll.java
│ │ │ │ ├── utils
│ │ │ │ └── Constant.java
│ │ │ │ ├── viewholder
│ │ │ │ ├── GridHolder.java
│ │ │ │ ├── LoadingHolder.java
│ │ │ │ ├── LinearHolder.java
│ │ │ │ └── StaggeredHolder.java
│ │ │ │ ├── model
│ │ │ │ └── DataView.java
│ │ │ │ ├── adapter
│ │ │ │ ├── GridAdapter.java
│ │ │ │ ├── LinearAdapter.java
│ │ │ │ └── StaggeredAdapter.java
│ │ │ │ ├── MainActivity.java
│ │ │ │ └── fragment
│ │ │ │ ├── LinearLayoutFragment.java
│ │ │ │ ├── StaggeredGridLayoutFragment.java
│ │ │ │ └── GridLayoutFragment.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── recyclerview
│ │ │ └── loadmore
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── recyclerview
│ │ └── loadmore
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── _config.yml
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── android-recyclerview-loadmore-gif.gif
├── README.markdown
├── gradle.properties
├── Licence.txt
├── .gitignore
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/_config.yml:
--------------------------------------------------------------------------------
1 | theme: jekyll-theme-architect
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pyush/Android-RecyclerView-Loadmore/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Android-RecyclerView-Loadmore
3 |
4 |
--------------------------------------------------------------------------------
/android-recyclerview-loadmore-gif.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pyush/Android-RecyclerView-Loadmore/HEAD/android-recyclerview-loadmore-gif.gif
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pyush/Android-RecyclerView-Loadmore/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pyush/Android-RecyclerView-Loadmore/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pyush/Android-RecyclerView-Loadmore/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pyush/Android-RecyclerView-Loadmore/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pyush/Android-RecyclerView-Loadmore/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Aug 15 14:26:16 IST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
7 |
--------------------------------------------------------------------------------
/README.markdown:
--------------------------------------------------------------------------------
1 | RecyclerView Loadmore and Swipe refresh
2 | - LinearLayout
3 | - GridLayout
4 | - StaggeredLayout
5 |
6 | Master branch has **JAVA** code
7 |
8 | Kotlin branch has **KOTLIN** code.
9 |
10 | Chears
11 |
12 | 
13 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #607D8B
4 | #455A64
5 | #607D8B
6 |
7 | #CFD8DC
8 | #212121
9 | #757575
10 | #FFFFFF
11 | #BDBDBD
12 |
13 |
--------------------------------------------------------------------------------
/app/src/test/java/com/recyclerview/loadmore/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in G:\AndroidStudio\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
12 |
13 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | android.enableJetifier=true
13 | android.useAndroidX=true
14 | org.gradle.jvmargs=-Xmx1536m
15 |
16 | # When configured, Gradle will run in incubating parallel mode.
17 | # This option should only be used with decoupled projects. More details, visit
18 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
19 | # org.gradle.parallel=true
20 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/widget/SquareImageView.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.widget;
2 |
3 | import android.content.Context;
4 | import android.util.AttributeSet;
5 | import android.widget.ImageView;
6 |
7 | public class SquareImageView extends ImageView {
8 |
9 | public SquareImageView(Context context) {
10 | super(context);
11 | }
12 |
13 | public SquareImageView(Context context, AttributeSet attrs) {
14 | super(context, attrs);
15 | }
16 |
17 | public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
18 | super(context, attrs, defStyle);
19 | }
20 |
21 | @Override
22 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
23 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
24 | int width = getMeasuredWidth();
25 | setMeasuredDimension(width, width);
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/recyclerview/loadmore/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore;
2 |
3 | import android.content.Context;
4 | import androidx.test.platform.app.InstrumentationRegistry;
5 | import androidx.test.ext.junit.runners.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.recyclerview.loadmore", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Licence.txt:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Piyush
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 32
5 | buildToolsVersion "30.0.3"
6 | defaultConfig {
7 | applicationId "com.recyclerview.loadmore"
8 | minSdkVersion 14
9 | targetSdkVersion 30
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | testImplementation 'junit:junit:4.12'
24 | androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | implementation fileTree(include: ['*.jar'], dir: 'libs')
28 | implementation 'androidx.appcompat:appcompat:1.0.0'
29 | implementation 'com.google.android.material:material:1.0.0'
30 | implementation 'androidx.recyclerview:recyclerview:1.0.0'
31 | implementation 'androidx.percentlayout:percentlayout:1.0.0'
32 | }
33 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/listener/OnLoadMoreListener.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.listener;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | /**
28 | * Created by Piyush on 1/3/2017.
29 | */
30 | public interface OnLoadMoreListener {
31 | void onLoadMore();
32 | }
33 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/utils/Constant.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.utils;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | /**
28 | * Created by Piyush on 1/10/2017.
29 | */
30 | public class Constant {
31 | public static final int VIEW_TYPE_ITEM = 0;
32 | public static final int VIEW_TYPE_LOADING = 1;
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/viewholder/GridHolder.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.viewholder;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | import androidx.recyclerview.widget.RecyclerView;
28 | import android.view.View;
29 |
30 | /**
31 | * Created by Piyush on 1/3/2017.
32 | */
33 | public class GridHolder extends RecyclerView.ViewHolder {
34 |
35 | public GridHolder(View itemView) {
36 | super(itemView);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/viewholder/LoadingHolder.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.viewholder;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | import androidx.recyclerview.widget.RecyclerView;
28 | import android.view.View;
29 |
30 | /**
31 | * Created by Piyush on 1/3/2017.
32 | */
33 | public class LoadingHolder extends RecyclerView.ViewHolder {
34 |
35 | public LoadingHolder(View itemView) {
36 | super(itemView);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/viewholder/LinearHolder.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.viewholder;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | import androidx.recyclerview.widget.RecyclerView;
28 | import android.view.View;
29 |
30 | /**
31 | * Created by Piyush on 1/3/2017.
32 | */
33 |
34 | public class LinearHolder extends RecyclerView.ViewHolder {
35 |
36 | public LinearHolder(View itemView) {
37 | super(itemView);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/viewholder/StaggeredHolder.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.viewholder;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | import androidx.recyclerview.widget.RecyclerView;
28 | import android.view.View;
29 |
30 | /**
31 | * Created by Piyush on 1/3/2017.
32 | */
33 |
34 | public class StaggeredHolder extends RecyclerView.ViewHolder {
35 |
36 | public StaggeredHolder(View itemView) {
37 | super(itemView);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/grid_row.xml:
--------------------------------------------------------------------------------
1 |
2 |
25 |
26 |
29 |
30 |
36 |
37 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/linear_row.xml:
--------------------------------------------------------------------------------
1 |
2 |
25 |
26 |
31 |
32 |
39 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
25 |
26 |
28 |
29 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/progress_loading.xml:
--------------------------------------------------------------------------------
1 |
2 |
25 |
26 |
30 |
31 |
36 |
37 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/model/DataView.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.model;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | import java.util.ArrayList;
28 | import java.util.List;
29 |
30 | /**
31 | * Created by Piyush on 1/10/2017.
32 | */
33 |
34 | public class DataView {
35 |
36 | String name;
37 |
38 | public String getName() {
39 | return name;
40 | }
41 |
42 | public void setName(String name) {
43 | this.name = name;
44 | }
45 |
46 |
47 | public static List getDataViews(int size) {
48 | List dataViews=new ArrayList<>();
49 | for(int i=0;i
2 |
25 |
26 |
30 |
31 |
35 |
36 |
41 |
42 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/widget/StaggerdSpacesItemDecoration.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.widget;
2 |
3 | /**
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | import android.graphics.Rect;
28 | import androidx.recyclerview.widget.RecyclerView;
29 | import android.view.View;
30 |
31 | /**
32 | * Created by Piyush on 1/11/2017.
33 | */
34 |
35 | public class StaggerdSpacesItemDecoration extends RecyclerView.ItemDecoration {
36 |
37 | private final int mSpace;
38 |
39 | public StaggerdSpacesItemDecoration(int space) {
40 | this.mSpace = space;
41 | }
42 |
43 | @Override
44 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
45 | outRect.left = mSpace;
46 | outRect.right = mSpace;
47 | outRect.bottom = mSpace;
48 | // Add top margin only for the first item to avoid double space between items
49 | if (parent.getChildAdapterPosition(view) == 0)
50 | outRect.top = mSpace;
51 | }
52 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/staggered_row.xml:
--------------------------------------------------------------------------------
1 |
2 |
25 |
26 |
29 |
30 |
36 |
37 |
47 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 | local.properties
16 | # Created by .ignore support plugin (hsz.mobi)
17 | ### NetBeans template
18 | nbproject/private/
19 | build/
20 | nbbuild/
21 | dist/
22 | nbdist/
23 | nbactions.xml
24 | nb-configuration.xml
25 |
26 | # Mac OS X Garbage
27 | .DS_Store
28 | Thumbs.db
29 |
30 | .idea/
31 |
32 | ### Android Studio
33 | /jniLibs
34 | mobile/mobile.iml
35 | mobile/mobile.iml
36 | tv/tv.iml
37 | app/app.iml
38 | app/*.iml
39 | *.iml
40 | mobile/*.iml
41 | tv/*.iml
42 | .idea/workspace.xml
43 | .idea/libraries
44 | .idea/
45 | .idea
46 | /build
47 | /captures
48 | ### Android template
49 | # Built application files
50 | *.apk
51 | *.ap_
52 | /build
53 | /captures
54 | # Files for the Dalvik VM
55 | *.dex
56 |
57 | # Java class files
58 | *.class
59 |
60 | # Generated files
61 | bin/
62 | gen/
63 |
64 | # Gradle files
65 | .gradle/
66 |
67 | # Local configuration file (sdk path, etc)
68 | local.properties
69 |
70 | # Proguard folder generated by Eclipse
71 | proguard/
72 |
73 | # Log Files
74 | *.log
75 |
76 |
77 | ### Java template
78 | *.class
79 |
80 | # Mobile Tools for Java (J2ME)
81 | .mtj.tmp/
82 |
83 | # Package Files #
84 | *.jar
85 | *.war
86 | *.ear
87 |
88 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
89 | hs_err_pid*
90 |
91 |
92 | ### Eclipse template
93 | *.pydevproject
94 | .metadata
95 | .gradle
96 | bin/
97 | tmp/
98 | *.tmp
99 | *.bak
100 | *.swp
101 | *~.nib
102 | .settings/
103 | .loadpath
104 | /build
105 | /captures
106 | # External tool builders
107 | .externalToolBuilders/
108 |
109 | # Locally stored "Eclipse launch configurations"
110 | *.launch
111 |
112 | # CDT-specific
113 | .cproject
114 |
115 | # PDT-specific
116 | .buildpath
117 |
118 | # sbteclipse plugin
119 | .target
120 |
121 | # TeXlipse plugin
122 | .texlipse
123 |
124 | # Android Studio
125 | *.iml
126 |
127 | # Keep external libs
128 | !app/libs/*.jar
129 |
130 | # script output
131 | check-dpi.txt*
132 |
133 | # spring output
134 | target
135 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
25 |
26 |
33 |
34 |
37 |
38 |
47 |
48 |
49 |
50 |
60 |
61 |
62 |
63 |
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/widget/GridSpacingItemDecoration.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.widget;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | import android.graphics.Rect;
28 | import androidx.recyclerview.widget.RecyclerView;
29 | import android.view.View;
30 |
31 | /**
32 | * Created by Piyush on 8/29/2016.
33 | */
34 | public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
35 |
36 | private int spanCount;
37 | private int spacing;
38 | private boolean includeEdge;
39 | private int headerNum;
40 |
41 | public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge, int headerNum) {
42 | this.spanCount = spanCount;
43 | this.spacing = spacing;
44 | this.includeEdge = includeEdge;
45 | this.headerNum = headerNum;
46 | }
47 |
48 | @Override
49 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
50 | int position = parent.getChildAdapterPosition(view) - headerNum; // item position
51 |
52 | if (position >= 0) {
53 | int column = position % spanCount; // item column
54 |
55 | if (includeEdge) {
56 | outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
57 | outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
58 |
59 | if (position < spanCount) { // top edge
60 | outRect.top = spacing;
61 | }
62 | outRect.bottom = spacing; // item bottom
63 | } else {
64 | outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
65 | outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
66 | if (position >= spanCount) {
67 | outRect.top = spacing; // item top
68 | }
69 | }
70 | } else {
71 | outRect.left = 0;
72 | outRect.right = 0;
73 | outRect.top = 0;
74 | outRect.bottom = 0;
75 | }
76 | }
77 | }
78 |
79 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/adapter/GridAdapter.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.adapter;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | import android.os.Handler;
28 | import androidx.recyclerview.widget.RecyclerView;
29 | import android.view.LayoutInflater;
30 | import android.view.View;
31 | import android.view.ViewGroup;
32 |
33 | import com.recyclerview.loadmore.R;
34 | import com.recyclerview.loadmore.model.DataView;
35 | import com.recyclerview.loadmore.utils.Constant;
36 | import com.recyclerview.loadmore.viewholder.GridHolder;
37 | import com.recyclerview.loadmore.viewholder.LoadingHolder;
38 | import java.util.List;
39 |
40 | /**
41 | * Created by Piyush on 1/10/2017.
42 | */
43 |
44 | public class GridAdapter extends RecyclerView.Adapter {
45 |
46 | List dataViews;
47 |
48 | public GridAdapter(List dataViews) {
49 | this.dataViews = dataViews;
50 | }
51 |
52 | public void addData(List dataViews) {
53 | this.dataViews.addAll(dataViews);
54 | notifyDataSetChanged();
55 | }
56 |
57 | public DataView getItemAtPosition(int position) {
58 | return dataViews.get(position);
59 | }
60 |
61 | public void addLoadingView() {
62 | //add loading item
63 | new Handler().post(new Runnable() {
64 | @Override
65 | public void run() {
66 | dataViews.add(null);
67 | notifyItemInserted(dataViews.size() - 1);
68 | }
69 | });
70 | }
71 |
72 | public void removeLoadingView() {
73 | //Remove loading item
74 | dataViews.remove(dataViews.size() - 1);
75 | notifyItemRemoved(dataViews.size());
76 | }
77 |
78 | @Override
79 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
80 | if(viewType== Constant.VIEW_TYPE_ITEM) {
81 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_row, parent, false);
82 | return new GridHolder(view);
83 | } else if(viewType==Constant.VIEW_TYPE_LOADING) {
84 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.progress_loading, parent, false);
85 | return new LoadingHolder(view);
86 | }
87 | return null;
88 | }
89 |
90 | @Override
91 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
92 |
93 | }
94 |
95 | @Override
96 | public int getItemCount() {
97 | return dataViews == null ? 0 : dataViews.size();
98 | }
99 |
100 | @Override
101 | public int getItemViewType(int position) {
102 | return dataViews.get(position) == null ? Constant.VIEW_TYPE_LOADING : Constant.VIEW_TYPE_ITEM;
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/adapter/LinearAdapter.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.adapter;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | import android.os.Handler;
28 | import androidx.recyclerview.widget.RecyclerView;
29 | import android.view.LayoutInflater;
30 | import android.view.View;
31 | import android.view.ViewGroup;
32 | import com.recyclerview.loadmore.R;
33 | import com.recyclerview.loadmore.model.DataView;
34 | import com.recyclerview.loadmore.utils.Constant;
35 | import com.recyclerview.loadmore.viewholder.LinearHolder;
36 | import com.recyclerview.loadmore.viewholder.LoadingHolder;
37 |
38 | import java.util.List;
39 |
40 | /**
41 | * Created by Piyush on 1/10/2017.
42 | */
43 | public class LinearAdapter extends RecyclerView.Adapter {
44 |
45 | List dataViews;
46 |
47 | public LinearAdapter(List dataViews) {
48 | this.dataViews = dataViews;
49 | }
50 |
51 | public void addData(List dataViews) {
52 | this.dataViews.addAll(dataViews);
53 | notifyDataSetChanged();
54 | }
55 |
56 | public DataView getItemAtPosition(int position) {
57 | return dataViews.get(position);
58 | }
59 |
60 | public void addLoadingView() {
61 | //add loading item
62 | new Handler().post(new Runnable() {
63 | @Override
64 | public void run() {
65 | dataViews.add(null);
66 | notifyItemInserted(dataViews.size() - 1);
67 | }
68 | });
69 | }
70 |
71 | public void removeLoadingView() {
72 | //Remove loading item
73 | dataViews.remove(dataViews.size() - 1);
74 | notifyItemRemoved(dataViews.size());
75 | }
76 |
77 | @Override
78 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
79 | if(viewType== Constant.VIEW_TYPE_ITEM) {
80 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.linear_row, parent, false);
81 | return new LinearHolder(view);
82 | } else if(viewType==Constant.VIEW_TYPE_LOADING) {
83 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.progress_loading, parent, false);
84 | return new LoadingHolder(view);
85 | }
86 | return null;
87 | }
88 |
89 | @Override
90 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
91 |
92 | }
93 |
94 | @Override
95 | public int getItemCount() {
96 | return dataViews == null ? 0 : dataViews.size();
97 | }
98 |
99 | @Override
100 | public int getItemViewType(int position) {
101 | return dataViews.get(position) == null ? Constant.VIEW_TYPE_LOADING : Constant.VIEW_TYPE_ITEM;
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/listener/RecyclerViewLoadMoreScroll.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.listener;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | import androidx.recyclerview.widget.GridLayoutManager;
28 | import androidx.recyclerview.widget.LinearLayoutManager;
29 | import androidx.recyclerview.widget.RecyclerView;
30 | import androidx.recyclerview.widget.StaggeredGridLayoutManager;
31 |
32 | /**
33 | * Created by Piyush on 1/3/2017.
34 | */
35 | public class RecyclerViewLoadMoreScroll extends RecyclerView.OnScrollListener {
36 |
37 | private int visibleThreshold = 5;
38 | private OnLoadMoreListener mOnLoadMoreListener;
39 | private boolean isLoading;
40 | private int lastVisibleItem, totalItemCount;
41 | private RecyclerView.LayoutManager mLayoutManager;
42 |
43 | public void setLoaded() {
44 | isLoading = false;
45 | }
46 |
47 | public boolean getLoaded() {
48 | return isLoading;
49 | }
50 |
51 | public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
52 | this.mOnLoadMoreListener = mOnLoadMoreListener;
53 | }
54 |
55 | public RecyclerViewLoadMoreScroll(LinearLayoutManager layoutManager) {
56 | this.mLayoutManager = layoutManager;
57 | }
58 |
59 | public RecyclerViewLoadMoreScroll(GridLayoutManager layoutManager) {
60 | this.mLayoutManager = layoutManager;
61 | visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
62 | }
63 |
64 | public RecyclerViewLoadMoreScroll(StaggeredGridLayoutManager layoutManager) {
65 | this.mLayoutManager = layoutManager;
66 | visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
67 | }
68 |
69 | @Override
70 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
71 | super.onScrolled(recyclerView, dx, dy);
72 |
73 | if (dy <= 0) return;
74 |
75 | totalItemCount = mLayoutManager.getItemCount();
76 |
77 | if (mLayoutManager instanceof StaggeredGridLayoutManager) {
78 | int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
79 | // get maximum element within the list
80 | lastVisibleItem = getLastVisibleItem(lastVisibleItemPositions);
81 | } else if (mLayoutManager instanceof GridLayoutManager) {
82 | lastVisibleItem = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
83 | } else if (mLayoutManager instanceof LinearLayoutManager) {
84 | lastVisibleItem = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
85 | }
86 |
87 | if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
88 | if (mOnLoadMoreListener != null) {
89 | mOnLoadMoreListener.onLoadMore();
90 | }
91 | isLoading = true;
92 | }
93 |
94 | }
95 |
96 | public int getLastVisibleItem(int[] lastVisibleItemPositions) {
97 | int maxSize = 0;
98 | for (int i = 0; i < lastVisibleItemPositions.length; i++) {
99 | if (i == 0) {
100 | maxSize = lastVisibleItemPositions[i];
101 | }
102 | else if (lastVisibleItemPositions[i] > maxSize) {
103 | maxSize = lastVisibleItemPositions[i];
104 | }
105 | }
106 | return maxSize;
107 | }
108 | }
109 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | import android.os.Bundle;
28 | import com.google.android.material.tabs.TabLayout;
29 | import androidx.fragment.app.Fragment;
30 | import androidx.fragment.app.FragmentManager;
31 | import androidx.fragment.app.FragmentPagerAdapter;
32 | import androidx.viewpager.widget.ViewPager;
33 | import androidx.appcompat.app.AppCompatActivity;
34 | import androidx.appcompat.widget.Toolbar;
35 |
36 | import com.recyclerview.loadmore.fragment.GridLayoutFragment;
37 | import com.recyclerview.loadmore.fragment.LinearLayoutFragment;
38 | import com.recyclerview.loadmore.fragment.StaggeredGridLayoutFragment;
39 |
40 | public class MainActivity extends AppCompatActivity {
41 |
42 | private TabLayout tab_layout;
43 | private ViewPager viewpager;
44 | private LoadMorePagerAdapter loadMorePagerAdapter;
45 |
46 | @Override
47 | protected void onCreate(Bundle savedInstanceState) {
48 | super.onCreate(savedInstanceState);
49 | setContentView(R.layout.activity_main);
50 | Initialize();
51 | }
52 |
53 | private void Initialize() {
54 | Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
55 | mToolbar.setTitle(R.string.app_name);
56 | setSupportActionBar(mToolbar);
57 |
58 | tab_layout=(TabLayout)findViewById(R.id.tab_layout);
59 | tab_layout.addTab(tab_layout.newTab().setText("Linear"));
60 | tab_layout.addTab(tab_layout.newTab().setText("Grid"));
61 | tab_layout.addTab(tab_layout.newTab().setText("Staggered"));
62 | tab_layout.setTabGravity(TabLayout.GRAVITY_FILL);
63 |
64 | viewpager=(ViewPager)findViewById(R.id.viewPager);
65 | viewpager.setOffscreenPageLimit(3);
66 | loadMorePagerAdapter=new LoadMorePagerAdapter(getSupportFragmentManager());
67 | viewpager.setAdapter(loadMorePagerAdapter);
68 | viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout));
69 | tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
70 | @Override
71 | public void onTabSelected(TabLayout.Tab tab) {
72 | viewpager.setCurrentItem(tab.getPosition());
73 | }
74 |
75 | @Override
76 | public void onTabUnselected(TabLayout.Tab tab) {
77 |
78 | }
79 |
80 | @Override
81 | public void onTabReselected(TabLayout.Tab tab) {
82 | }
83 | });
84 | }
85 |
86 | private class LoadMorePagerAdapter extends FragmentPagerAdapter {
87 |
88 | final int mNumOfTab = 3;
89 |
90 | public LoadMorePagerAdapter(FragmentManager fm) {
91 | super(fm);
92 | }
93 |
94 | @Override
95 | public Fragment getItem(int position) {
96 | Fragment fragment=null;
97 | switch (position) {
98 | case 0:
99 | fragment = LinearLayoutFragment.newInstance();
100 | break;
101 | case 1:
102 | fragment = GridLayoutFragment.newInstance();
103 | break;
104 | case 2:
105 | fragment= StaggeredGridLayoutFragment.newInstance();
106 | break;
107 | }
108 | return fragment;
109 | }
110 |
111 | @Override
112 | public int getCount() {
113 | return mNumOfTab;
114 | }
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/adapter/StaggeredAdapter.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.adapter;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | import android.os.Handler;
28 | import androidx.recyclerview.widget.RecyclerView;
29 | import androidx.recyclerview.widget.StaggeredGridLayoutManager;
30 | import android.view.LayoutInflater;
31 | import android.view.View;
32 | import android.view.ViewGroup;
33 |
34 | import com.recyclerview.loadmore.R;
35 | import com.recyclerview.loadmore.model.DataView;
36 | import com.recyclerview.loadmore.utils.Constant;
37 | import com.recyclerview.loadmore.viewholder.LoadingHolder;
38 | import com.recyclerview.loadmore.viewholder.StaggeredHolder;
39 |
40 | import java.util.List;
41 |
42 | /**
43 | * Created by Piyush on 1/10/2017.
44 | */
45 | public class StaggeredAdapter extends RecyclerView.Adapter {
46 |
47 | List dataViews;
48 |
49 | public StaggeredAdapter(List dataViews) {
50 | this.dataViews = dataViews;
51 | }
52 |
53 | public void addData(List dataViews) {
54 | this.dataViews.addAll(dataViews);
55 | notifyDataSetChanged();
56 | }
57 |
58 | public DataView getItemAtPosition(int position) {
59 | return dataViews.get(position);
60 | }
61 |
62 | public void addLoadingView() {
63 | //add loading item
64 | new Handler().post(new Runnable() {
65 | @Override
66 | public void run() {
67 | dataViews.add(null);
68 | notifyItemInserted(dataViews.size() - 1);
69 | }
70 | });
71 | }
72 |
73 | public void removeLoadingView() {
74 | //Remove loading item
75 | dataViews.remove(dataViews.size() - 1);
76 | notifyItemRemoved(dataViews.size());
77 | }
78 |
79 | @Override
80 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
81 | if(viewType== Constant.VIEW_TYPE_ITEM) {
82 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.staggered_row, parent, false);
83 | return new StaggeredHolder(view);
84 | } else if(viewType==Constant.VIEW_TYPE_LOADING) {
85 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.progress_loading, parent, false);
86 | return new LoadingHolder(view);
87 | }
88 | return null;
89 | }
90 |
91 | @Override
92 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
93 | DataView dataView=dataViews.get(position);
94 | if(holder instanceof StaggeredHolder) {
95 | StaggeredHolder staggeredHolder=(StaggeredHolder)holder;
96 |
97 | } else if(holder instanceof LoadingHolder) {
98 | LoadingHolder loadingHolder=(LoadingHolder)holder;
99 | }
100 |
101 | if(dataView==null) {
102 | // Span the item if active
103 | final ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
104 | if (lp instanceof StaggeredGridLayoutManager.LayoutParams) {
105 | StaggeredGridLayoutManager.LayoutParams sglp = (StaggeredGridLayoutManager.LayoutParams) lp;
106 | sglp.setFullSpan(true);
107 | holder.itemView.setLayoutParams(sglp);
108 | }
109 | } else {
110 | // Span the item if active
111 | final ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
112 | if (lp instanceof StaggeredGridLayoutManager.LayoutParams) {
113 | StaggeredGridLayoutManager.LayoutParams sglp = (StaggeredGridLayoutManager.LayoutParams) lp;
114 | sglp.setFullSpan(false);
115 | holder.itemView.setLayoutParams(sglp);
116 | }
117 | }
118 | }
119 |
120 | @Override
121 | public int getItemCount() {
122 | return dataViews == null ? 0 : dataViews.size();
123 | }
124 |
125 | @Override
126 | public int getItemViewType(int position) {
127 | return dataViews.get(position) == null ? Constant.VIEW_TYPE_LOADING : Constant.VIEW_TYPE_ITEM;
128 | }
129 | }
130 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/fragment/LinearLayoutFragment.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.fragment;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | import android.os.Bundle;
28 | import android.os.Handler;
29 | import androidx.annotation.Nullable;
30 | import androidx.fragment.app.Fragment;
31 | import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
32 | import androidx.recyclerview.widget.LinearLayoutManager;
33 | import androidx.recyclerview.widget.RecyclerView;
34 | import android.view.LayoutInflater;
35 | import android.view.View;
36 | import android.view.ViewGroup;
37 |
38 | import com.recyclerview.loadmore.R;
39 | import com.recyclerview.loadmore.adapter.LinearAdapter;
40 | import com.recyclerview.loadmore.listener.OnLoadMoreListener;
41 | import com.recyclerview.loadmore.listener.RecyclerViewLoadMoreScroll;
42 | import com.recyclerview.loadmore.model.DataView;
43 |
44 | import java.util.List;
45 |
46 | /**
47 | * Created by Piyush on 1/10/2017.
48 | */
49 | public class LinearLayoutFragment extends Fragment {
50 |
51 | private View rootView;
52 | private SwipeRefreshLayout swipeRefresh;
53 | private RecyclerView recyclerview;
54 | private LinearAdapter linearAdapter;
55 | private RecyclerViewLoadMoreScroll scrollListener;
56 |
57 | public LinearLayoutFragment() {
58 | // Required empty public constructor
59 | }
60 |
61 | public static LinearLayoutFragment newInstance() {
62 | LinearLayoutFragment fragment = new LinearLayoutFragment();
63 | Bundle args = new Bundle();
64 | fragment.setArguments(args);
65 | return fragment;
66 | }
67 |
68 | @Override
69 | public void onCreate(@Nullable Bundle savedInstanceState) {
70 | super.onCreate(savedInstanceState);
71 | }
72 |
73 | @Nullable
74 | @Override
75 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
76 | rootView=inflater.inflate(R.layout.layout_recyclerview, container, false);
77 | Initialize();
78 | return rootView;
79 | }
80 |
81 | @Override
82 | public void onResume() {
83 | super.onResume();
84 |
85 | swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
86 | @Override
87 | public void onRefresh() {
88 | new Handler().postDelayed(new Runnable() {
89 | @Override
90 | public void run() {
91 | List dataViews=DataView.getDataViews(15);
92 | linearAdapter=new LinearAdapter(dataViews);
93 | recyclerview.setAdapter(linearAdapter);
94 | swipeRefresh.setRefreshing(false);
95 | }
96 | },5000);
97 | }
98 | });
99 | }
100 |
101 | private void Initialize() {
102 | swipeRefresh=(SwipeRefreshLayout)rootView.findViewById(R.id.swipeRefresh);
103 | recyclerview=(RecyclerView)rootView.findViewById(R.id.recyclerview);
104 | recyclerview.setHasFixedSize(true);
105 | LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
106 | linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
107 | recyclerview.setLayoutManager(linearLayoutManager);
108 | linearAdapter=new LinearAdapter(DataView.getDataViews(15));
109 | recyclerview.setAdapter(linearAdapter);
110 |
111 | scrollListener=new RecyclerViewLoadMoreScroll(linearLayoutManager);
112 | scrollListener.setOnLoadMoreListener(new OnLoadMoreListener() {
113 | @Override
114 | public void onLoadMore() {
115 | LoadMoreData();
116 | }
117 | });
118 |
119 | recyclerview.addOnScrollListener(scrollListener);
120 | }
121 |
122 | private void LoadMoreData() {
123 | linearAdapter.addLoadingView();
124 | new Handler().postDelayed(new Runnable() {
125 | @Override
126 | public void run() {
127 | List dataViews=DataView.getDataViews(15);
128 | linearAdapter.removeLoadingView();
129 | linearAdapter.addData(dataViews);
130 | linearAdapter.notifyDataSetChanged();
131 | scrollListener.setLoaded();
132 | }
133 | },5000);
134 |
135 | }
136 | }
137 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/fragment/StaggeredGridLayoutFragment.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.fragment;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | import android.os.Bundle;
28 | import android.os.Handler;
29 | import androidx.annotation.Nullable;
30 | import androidx.fragment.app.Fragment;
31 | import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
32 | import androidx.recyclerview.widget.RecyclerView;
33 | import androidx.recyclerview.widget.StaggeredGridLayoutManager;
34 | import android.view.LayoutInflater;
35 | import android.view.View;
36 | import android.view.ViewGroup;
37 |
38 | import com.recyclerview.loadmore.R;
39 | import com.recyclerview.loadmore.adapter.StaggeredAdapter;
40 | import com.recyclerview.loadmore.listener.OnLoadMoreListener;
41 | import com.recyclerview.loadmore.listener.RecyclerViewLoadMoreScroll;
42 | import com.recyclerview.loadmore.model.DataView;
43 | import com.recyclerview.loadmore.widget.StaggerdSpacesItemDecoration;
44 |
45 | import java.util.List;
46 |
47 | /**
48 | * Created by Piyush on 1/10/2017.
49 | */
50 |
51 | public class StaggeredGridLayoutFragment extends Fragment {
52 |
53 | private View rootView;
54 | private SwipeRefreshLayout swipeRefresh;
55 | private RecyclerView recyclerview;
56 | private StaggeredGridLayoutManager gridLayoutManager;
57 | private StaggeredAdapter staggeredAdapter;
58 | private RecyclerViewLoadMoreScroll scrollListener;
59 |
60 | public StaggeredGridLayoutFragment() {
61 | // Required empty public constructor
62 | }
63 |
64 | public static StaggeredGridLayoutFragment newInstance() {
65 | StaggeredGridLayoutFragment fragment = new StaggeredGridLayoutFragment();
66 | Bundle args = new Bundle();
67 | fragment.setArguments(args);
68 | return fragment;
69 | }
70 |
71 | @Nullable
72 | @Override
73 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
74 | rootView=inflater.inflate(R.layout.layout_recyclerview, container, false);
75 | Initialize();
76 | return rootView;
77 | }
78 |
79 | @Override
80 | public void onResume() {
81 | super.onResume();
82 |
83 | swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
84 | @Override
85 | public void onRefresh() {
86 | new Handler().postDelayed(new Runnable() {
87 | @Override
88 | public void run() {
89 | setAdapter();
90 | swipeRefresh.setRefreshing(false);
91 | }
92 | },5000);
93 | }
94 | });
95 | }
96 |
97 | private void Initialize() {
98 | swipeRefresh=(SwipeRefreshLayout)rootView.findViewById(R.id.swipeRefresh);
99 | recyclerview=(RecyclerView)rootView.findViewById(R.id.recyclerview);
100 | recyclerview.setHasFixedSize(true);
101 | int spacingInPixels=5;
102 | recyclerview.addItemDecoration(new StaggerdSpacesItemDecoration(spacingInPixels));
103 |
104 | setAdapter();
105 |
106 | scrollListener=new RecyclerViewLoadMoreScroll(gridLayoutManager);
107 | scrollListener.setOnLoadMoreListener(new OnLoadMoreListener() {
108 | @Override
109 | public void onLoadMore() {
110 | LoadMoreData();
111 | }
112 | });
113 |
114 | recyclerview.addOnScrollListener(scrollListener);
115 | }
116 |
117 | private void setAdapter() {
118 | gridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
119 | //gridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
120 | recyclerview.setLayoutManager(gridLayoutManager);
121 | staggeredAdapter=new StaggeredAdapter(DataView.getDataViews(15));
122 | recyclerview.setAdapter(staggeredAdapter);
123 | }
124 |
125 | private void LoadMoreData() {
126 | staggeredAdapter.addLoadingView();
127 | new Handler().postDelayed(new Runnable() {
128 | @Override
129 | public void run() {
130 | List dataViews=DataView.getDataViews(15);
131 | staggeredAdapter.removeLoadingView();
132 | staggeredAdapter.addData(dataViews);
133 | staggeredAdapter.notifyDataSetChanged();
134 | scrollListener.setLoaded();
135 | }
136 | },5000);
137 |
138 | }
139 | }
140 |
--------------------------------------------------------------------------------
/app/src/main/java/com/recyclerview/loadmore/fragment/GridLayoutFragment.java:
--------------------------------------------------------------------------------
1 | package com.recyclerview.loadmore.fragment;
2 |
3 | /*
4 | * MIT License
5 | *
6 | * Copyright (c) 2017 Piyush
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in all
16 | * copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 |
27 | import android.os.Bundle;
28 | import android.os.Handler;
29 | import androidx.annotation.Nullable;
30 | import androidx.fragment.app.Fragment;
31 | import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
32 | import androidx.recyclerview.widget.GridLayoutManager;
33 | import androidx.recyclerview.widget.RecyclerView;
34 | import android.view.LayoutInflater;
35 | import android.view.View;
36 | import android.view.ViewGroup;
37 |
38 | import com.recyclerview.loadmore.R;
39 | import com.recyclerview.loadmore.adapter.GridAdapter;
40 | import com.recyclerview.loadmore.listener.OnLoadMoreListener;
41 | import com.recyclerview.loadmore.listener.RecyclerViewLoadMoreScroll;
42 | import com.recyclerview.loadmore.model.DataView;
43 | import com.recyclerview.loadmore.widget.GridSpacingItemDecoration;
44 |
45 | import java.util.List;
46 |
47 | import static com.recyclerview.loadmore.utils.Constant.VIEW_TYPE_ITEM;
48 | import static com.recyclerview.loadmore.utils.Constant.VIEW_TYPE_LOADING;
49 |
50 | /**
51 | * Created by Piyush on 1/10/2017.
52 | */
53 |
54 | public class GridLayoutFragment extends Fragment {
55 |
56 | private View rootView;
57 | private SwipeRefreshLayout swipeRefresh;
58 | private RecyclerView recyclerview;
59 | private GridLayoutManager gridLayoutManager;
60 | private GridAdapter gridAdapter;
61 | private RecyclerViewLoadMoreScroll scrollListener;
62 |
63 | public GridLayoutFragment() {
64 | // Required empty public constructor
65 | }
66 |
67 | public static GridLayoutFragment newInstance() {
68 | GridLayoutFragment fragment = new GridLayoutFragment();
69 | Bundle args = new Bundle();
70 | fragment.setArguments(args);
71 | return fragment;
72 | }
73 |
74 | @Override
75 | public void onCreate(@Nullable Bundle savedInstanceState) {
76 | super.onCreate(savedInstanceState);
77 | }
78 |
79 | @Nullable
80 | @Override
81 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
82 | rootView=inflater.inflate(R.layout.layout_recyclerview, container, false);
83 | Initialize();
84 | return rootView;
85 | }
86 |
87 | @Override
88 | public void onResume() {
89 | super.onResume();
90 |
91 | swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
92 | @Override
93 | public void onRefresh() {
94 | new Handler().postDelayed(new Runnable() {
95 | @Override
96 | public void run() {
97 | setAdapter();
98 | swipeRefresh.setRefreshing(false);
99 | }
100 | },5000);
101 | }
102 | });
103 | }
104 |
105 | private void Initialize() {
106 | swipeRefresh=(SwipeRefreshLayout)rootView.findViewById(R.id.swipeRefresh);
107 | recyclerview=(RecyclerView)rootView.findViewById(R.id.recyclerview);
108 | recyclerview.setHasFixedSize(true);
109 | int spacingInPixels=5;
110 | recyclerview.addItemDecoration(new GridSpacingItemDecoration(3, spacingInPixels, true, 0));
111 | setAdapter();
112 |
113 | scrollListener=new RecyclerViewLoadMoreScroll(gridLayoutManager);
114 | scrollListener.setOnLoadMoreListener(new OnLoadMoreListener() {
115 | @Override
116 | public void onLoadMore() {
117 | LoadMoreData();
118 | }
119 | });
120 |
121 | recyclerview.addOnScrollListener(scrollListener);
122 | }
123 |
124 | private void setAdapter() {
125 | gridLayoutManager=new GridLayoutManager(getActivity(),3);
126 | recyclerview.setLayoutManager(gridLayoutManager);
127 | gridAdapter=new GridAdapter(DataView.getDataViews(15));
128 | recyclerview.setAdapter(gridAdapter);
129 |
130 | gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
131 | @Override
132 | public int getSpanSize(int position) {
133 | switch(gridAdapter.getItemViewType(position)) {
134 | case VIEW_TYPE_ITEM:
135 | return 1;
136 | case VIEW_TYPE_LOADING:
137 | return 3; //number of columns of the grid
138 | default:
139 | return -1;
140 | }
141 | }
142 | });
143 | }
144 |
145 | private void LoadMoreData() {
146 | gridAdapter.addLoadingView();
147 | new Handler().postDelayed(new Runnable() {
148 | @Override
149 | public void run() {
150 | List dataViews=DataView.getDataViews(15);
151 | gridAdapter.removeLoadingView();
152 | gridAdapter.addData(dataViews);
153 | gridAdapter.notifyDataSetChanged();
154 | scrollListener.setLoaded();
155 | }
156 | },5000);
157 |
158 | }
159 | }
160 |
--------------------------------------------------------------------------------