├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── ids.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── dimens.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
│ │ │ ├── layout
│ │ │ │ ├── layout_photo.xml
│ │ │ │ ├── layout_header.xml
│ │ │ │ ├── activity_main.xml
│ │ │ │ └── layout_post.xml
│ │ │ ├── menu
│ │ │ │ └── menu_main.xml
│ │ │ └── values-w820dp
│ │ │ │ └── dimens.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── samvidmistry
│ │ │ │ └── recyclerviewadditions
│ │ │ │ ├── App.java
│ │ │ │ ├── BindingAdapters.java
│ │ │ │ ├── PostViewHolder.java
│ │ │ │ ├── PhotoViewHolder.java
│ │ │ │ ├── RecyclerViewHolder.java
│ │ │ │ ├── Post.java
│ │ │ │ ├── Photo.java
│ │ │ │ ├── PhotoListAdapter.java
│ │ │ │ ├── PhotoItemDecoration.java
│ │ │ │ ├── TextItemDecoration.java
│ │ │ │ ├── MainActivity.java
│ │ │ │ ├── MainListAdapter.java
│ │ │ │ └── GravitySnapHelper.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── samvidmistry
│ │ │ └── recyclerviewadditions
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── samvidmistry
│ │ └── recyclerviewadditions
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── .idea
├── copyright
│ └── profiles_settings.xml
├── modules.xml
├── runConfigurations.xml
├── compiler.xml
├── gradle.xml
├── misc.xml
└── codeStyleSettings.xml
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── README.md
├── .gitignore
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | RecyclerViewAdditions
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samvidmistry/RecyclerViewAdditions/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samvidmistry/RecyclerViewAdditions/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samvidmistry/RecyclerViewAdditions/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # RecyclerViewAdditions
2 | The project showing use of different RecyclerView components and use of them. Also used as demo in the medium article.
3 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samvidmistry/RecyclerViewAdditions/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samvidmistry/RecyclerViewAdditions/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samvidmistry/RecyclerViewAdditions/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
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-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_photo.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/samvidmistry/recyclerviewadditions/App.java:
--------------------------------------------------------------------------------
1 | package com.example.samvidmistry.recyclerviewadditions;
2 |
3 | import android.app.Application;
4 |
5 | import com.bumptech.glide.request.target.ViewTarget;
6 |
7 | /**
8 | * Created by samvidmistry on 11/19/16.
9 | */
10 |
11 | public class App extends Application {
12 | @Override
13 | public void onCreate() {
14 | super.onCreate();
15 | ViewTarget.setTagId(R.id.glide_tag);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_header.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/test/java/com/example/samvidmistry/recyclerviewadditions/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.example.samvidmistry.recyclerviewadditions;
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/src/main/java/com/example/samvidmistry/recyclerviewadditions/BindingAdapters.java:
--------------------------------------------------------------------------------
1 | package com.example.samvidmistry.recyclerviewadditions;
2 |
3 | import android.databinding.BindingAdapter;
4 | import android.widget.ImageView;
5 |
6 | import com.bumptech.glide.Glide;
7 |
8 | /**
9 | * Created by samvidmistry on 11/19/16.
10 | */
11 |
12 | public class BindingAdapters {
13 |
14 | @BindingAdapter("imageUrl")
15 | public static void setImageFromUrl(ImageView imageView, String url) {
16 | Glide.with(imageView.getContext()).load(url).asBitmap().into(imageView);
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/samvidmistry/recyclerviewadditions/PostViewHolder.java:
--------------------------------------------------------------------------------
1 | package com.example.samvidmistry.recyclerviewadditions;
2 |
3 | import android.support.v7.widget.RecyclerView;
4 | import android.view.View;
5 |
6 | import com.example.samvidmistry.recyclerviewadditions.databinding.LayoutPostBinding;
7 |
8 | /**
9 | * Created by samvidmistry on 11/19/16.
10 | */
11 |
12 | public class PostViewHolder extends RecyclerView.ViewHolder {
13 | LayoutPostBinding mBinding;
14 |
15 | public PostViewHolder(LayoutPostBinding binding) {
16 | super(binding.getRoot());
17 | mBinding = binding;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/samvidmistry/recyclerviewadditions/PhotoViewHolder.java:
--------------------------------------------------------------------------------
1 | package com.example.samvidmistry.recyclerviewadditions;
2 |
3 | import android.support.v7.widget.RecyclerView;
4 | import android.view.View;
5 |
6 | import com.example.samvidmistry.recyclerviewadditions.databinding.LayoutPhotoBinding;
7 |
8 | /**
9 | * Created by samvidmistry on 11/19/16.
10 | */
11 |
12 | public class PhotoViewHolder extends RecyclerView.ViewHolder {
13 | LayoutPhotoBinding mBinding;
14 |
15 | public PhotoViewHolder(LayoutPhotoBinding binding) {
16 | super(binding.getRoot());
17 | mBinding = binding;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/samvidmistry/recyclerviewadditions/RecyclerViewHolder.java:
--------------------------------------------------------------------------------
1 | package com.example.samvidmistry.recyclerviewadditions;
2 |
3 | import android.support.v7.widget.RecyclerView;
4 | import android.view.View;
5 |
6 | import com.example.samvidmistry.recyclerviewadditions.databinding.LayoutHeaderBinding;
7 |
8 | /**
9 | * Created by samvidmistry on 11/19/16.
10 | */
11 |
12 | public class RecyclerViewHolder extends RecyclerView.ViewHolder {
13 | LayoutHeaderBinding mBinding;
14 |
15 | public RecyclerViewHolder(LayoutHeaderBinding binding) {
16 | super(binding.getRoot());
17 | mBinding = binding;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/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 /home/samvidmistry/Android/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 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
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 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/samvidmistry/recyclerviewadditions/Post.java:
--------------------------------------------------------------------------------
1 | package com.example.samvidmistry.recyclerviewadditions;
2 |
3 | import com.google.gson.annotations.SerializedName;
4 | import com.orm.SugarRecord;
5 |
6 | /**
7 | * Created by samvidmistry on 11/19/16.
8 | */
9 |
10 | public class Post extends SugarRecord {
11 | @SerializedName("userId")
12 | private int mUserId;
13 | @SerializedName("title")
14 | private String mTitle;
15 | @SerializedName("body")
16 | private String mBody;
17 |
18 | public int getUserId() {
19 | return mUserId;
20 | }
21 |
22 | public String getTitle() {
23 | return mTitle;
24 | }
25 |
26 | public String getBody() {
27 | return mBody;
28 | }
29 |
30 | @Override
31 | public String toString() {
32 | return mUserId + " " + mTitle;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/example/samvidmistry/recyclerviewadditions/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.example.samvidmistry.recyclerviewadditions;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.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.example.samvidmistry.recyclerviewadditions", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/samvidmistry/recyclerviewadditions/Photo.java:
--------------------------------------------------------------------------------
1 | package com.example.samvidmistry.recyclerviewadditions;
2 |
3 | import com.google.gson.annotations.SerializedName;
4 | import com.orm.SugarRecord;
5 |
6 | /**
7 | * Created by samvidmistry on 11/19/16.
8 | */
9 |
10 | public class Photo extends SugarRecord {
11 | @SerializedName("albumId")
12 | private int mAlbumId;
13 | @SerializedName("title")
14 | private String mTitle;
15 | @SerializedName("url")
16 | private String mUrl;
17 | @SerializedName("thumbnailUrl")
18 | private String mThumbUrl;
19 |
20 | public int getAlbumId() {
21 | return mAlbumId;
22 | }
23 |
24 | public String getTitle() {
25 | return mTitle;
26 | }
27 |
28 | public String getUrl() {
29 | return mUrl;
30 | }
31 |
32 | public String getThumbUrl() {
33 | return mThumbUrl;
34 | }
35 |
36 | public void setThumbUrl(String thumbUrl) {
37 | mThumbUrl = thumbUrl;
38 | }
39 |
40 | public void setUrl(String url) {
41 | mUrl = url;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.0"
6 | defaultConfig {
7 | applicationId "com.example.samvidmistry.recyclerviewadditions"
8 | minSdkVersion 9
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | dataBinding {
21 | enabled true
22 | }
23 | }
24 |
25 | dependencies {
26 | compile fileTree(include: ['*.jar'], dir: 'libs')
27 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
28 | exclude group: 'com.android.support', module: 'support-annotations'
29 | })
30 | compile 'com.android.support:appcompat-v7:25.0.1'
31 | testCompile 'junit:junit:4.12'
32 | compile 'com.android.support:recyclerview-v7:25.0.1'
33 | compile 'com.github.satyan:sugar:1.5'
34 | compile 'com.github.bumptech.glide:glide:3.7.0'
35 | compile 'com.squareup.picasso:picasso:2.5.2'
36 | compile 'com.squareup.okhttp3:okhttp:3.4.2'
37 | compile 'com.google.code.gson:gson:2.8.0'
38 | }
39 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/samvidmistry/recyclerviewadditions/PhotoListAdapter.java:
--------------------------------------------------------------------------------
1 | package com.example.samvidmistry.recyclerviewadditions;
2 |
3 | import android.net.Uri;
4 | import android.support.v7.widget.RecyclerView;
5 | import android.view.LayoutInflater;
6 | import android.view.ViewGroup;
7 |
8 | import com.bumptech.glide.Glide;
9 | import com.example.samvidmistry.recyclerviewadditions.databinding.LayoutPhotoBinding;
10 | import com.squareup.picasso.Picasso;
11 | import com.squareup.picasso.UrlConnectionDownloader;
12 |
13 | import java.util.List;
14 |
15 | /**
16 | * Created by samvidmistry on 11/19/16.
17 | */
18 |
19 | public class PhotoListAdapter extends RecyclerView.Adapter {
20 | private List mPhotos;
21 |
22 | public PhotoListAdapter(List photos) {
23 | mPhotos = photos;
24 | }
25 |
26 | @Override
27 | public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
28 | return new PhotoViewHolder(LayoutPhotoBinding.inflate(
29 | LayoutInflater.from(parent.getContext()),
30 | parent,
31 | false
32 | ));
33 | }
34 |
35 | @Override
36 | public void onBindViewHolder(PhotoViewHolder holder, int position) {
37 | Picasso.with(holder.mBinding.image.getContext()).load(mPhotos.get(position).getThumbUrl())
38 | .into(holder.mBinding.image);
39 | }
40 |
41 | @Override
42 | public int getItemCount() {
43 | return mPhotos.size();
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
11 |
12 |
19 |
20 |
24 |
25 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_post.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
10 |
11 |
12 |
17 |
18 |
29 |
30 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/samvidmistry/recyclerviewadditions/PhotoItemDecoration.java:
--------------------------------------------------------------------------------
1 | package com.example.samvidmistry.recyclerviewadditions;
2 |
3 | import android.graphics.Canvas;
4 | import android.graphics.Color;
5 | import android.graphics.Paint;
6 | import android.graphics.Rect;
7 | import android.support.v7.widget.RecyclerView;
8 | import android.view.View;
9 |
10 | /**
11 | * Created by samvidmistry on 11/20/16.
12 | */
13 |
14 | public class PhotoItemDecoration extends RecyclerView.ItemDecoration {
15 | private Rect mRect = new Rect();
16 | private final Paint mPaint;
17 |
18 | public PhotoItemDecoration() {
19 | mPaint = new Paint();
20 | mPaint.setAntiAlias(true);
21 | mPaint.setColor(Color.GRAY);
22 | mPaint.setStyle(Paint.Style.STROKE);
23 | mPaint.setStrokeWidth(5f);
24 | }
25 |
26 | @Override
27 | public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
28 | int save = c.save();
29 |
30 | int top = 0;
31 | int bottom = parent.getHeight();
32 |
33 | for (int i = 0, size = parent.getChildCount(); i < size; i++) {
34 | View child = parent.getChildAt(i);
35 | parent.getDecoratedBoundsWithMargins(child, mRect);
36 | mRect.left += 10;
37 | mRect.right -= 10;
38 | mRect.top += 2;
39 | mRect.bottom -= 2;
40 | c.drawRect(mRect, mPaint);
41 | }
42 |
43 | c.restoreToCount(save);
44 | }
45 |
46 | @Override
47 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
48 | outRect.set(10, 0, 10, 0);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/samvidmistry/recyclerviewadditions/TextItemDecoration.java:
--------------------------------------------------------------------------------
1 | package com.example.samvidmistry.recyclerviewadditions;
2 |
3 | import android.graphics.Canvas;
4 | import android.graphics.Color;
5 | import android.graphics.Paint;
6 | import android.graphics.Path;
7 | import android.graphics.Rect;
8 | import android.support.v7.widget.RecyclerView;
9 | import android.view.View;
10 |
11 | /**
12 | * Created by samvidmistry on 11/20/16.
13 | */
14 |
15 | public class TextItemDecoration extends RecyclerView.ItemDecoration {
16 | private Rect mRect = new Rect();
17 | private final Paint mPaint;
18 | private Path mPath;
19 |
20 | public TextItemDecoration() {
21 | mPaint = new Paint();
22 | mPaint.setAntiAlias(true);
23 | mPaint.setColor(Color.GRAY);
24 | mPaint.setStyle(Paint.Style.STROKE);
25 | mPaint.setStrokeWidth(15f);
26 | }
27 |
28 | @Override
29 | public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
30 | int save = c.save();
31 |
32 | for (int i = 0, size = parent.getChildCount(); i < size; i++) {
33 | View child = parent.getChildAt(i);
34 | if (parent.getChildAdapterPosition(child) == 0) {
35 | continue;
36 | }
37 | parent.getDecoratedBoundsWithMargins(child, mRect);
38 | mRect.left += 10;
39 | mRect.right -= 10;
40 | mRect.top += 20;
41 | mRect.bottom -= 20;
42 | c.drawRect(mRect, mPaint);
43 | }
44 |
45 | c.restoreToCount(save);
46 | }
47 |
48 | @Override
49 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
50 | if (parent.getChildAdapterPosition(view) == 0) {
51 | return;
52 | }
53 | outRect.set(25, 25, 25, 25);
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 | 1.8
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/samvidmistry/recyclerviewadditions/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.samvidmistry.recyclerviewadditions;
2 |
3 | import android.databinding.DataBindingUtil;
4 | import android.os.AsyncTask;
5 | import android.support.v4.widget.SwipeRefreshLayout;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.os.Bundle;
8 | import android.view.Menu;
9 | import android.view.MenuItem;
10 |
11 | import com.example.samvidmistry.recyclerviewadditions.databinding.ActivityMainBinding;
12 | import com.google.gson.Gson;
13 |
14 | import java.io.IOException;
15 | import java.util.ArrayList;
16 | import java.util.Arrays;
17 | import java.util.List;
18 |
19 | import okhttp3.OkHttpClient;
20 | import okhttp3.Request;
21 | import okhttp3.Response;
22 |
23 | public class MainActivity extends AppCompatActivity {
24 | ActivityMainBinding mBinding;
25 | OkHttpClient mOkHttpClient;
26 | Gson mGson;
27 | MainListAdapter mAdapter;
28 |
29 | @Override
30 | protected void onCreate(Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 | mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
33 |
34 | mBinding.swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
35 | @Override
36 | public void onRefresh() {
37 | new PostAsyncTask().execute();
38 | }
39 | });
40 |
41 | mBinding.textList.addItemDecoration(new TextItemDecoration());
42 |
43 | setSupportActionBar(mBinding.toolbar);
44 |
45 | new PostAsyncTask().execute();
46 | }
47 |
48 | @Override
49 | public boolean onCreateOptionsMenu(Menu menu) {
50 | getMenuInflater().inflate(R.menu.menu_main, menu);
51 | return true;
52 | }
53 |
54 | @Override
55 | public boolean onOptionsItemSelected(MenuItem item) {
56 | switch (item.getItemId()) {
57 | case R.id.delete:
58 | if (mAdapter != null) {
59 | mAdapter.deleteItems(10);
60 | }
61 | }
62 | return super.onOptionsItemSelected(item);
63 | }
64 |
65 | private class PostAsyncTask extends AsyncTask {
66 | private List mPhotos;
67 | private List mPosts;
68 |
69 | @Override
70 | protected void onPreExecute() {
71 | mBinding.swipeRefresh.setRefreshing(true);
72 | }
73 |
74 | @Override
75 | protected Void doInBackground(Void... voids) {
76 | if (mOkHttpClient == null) {
77 | mOkHttpClient = new OkHttpClient();
78 | }
79 |
80 | Request request = new Request.Builder()
81 | .url("https://jsonplaceholder.typicode.com/photos")
82 | .build();
83 |
84 | Response response;
85 | try {
86 | response = mOkHttpClient.newCall(request).execute();
87 | } catch (IOException e) {
88 | e.printStackTrace();
89 | response = null;
90 | }
91 |
92 | if (response == null) {
93 | return null;
94 | }
95 |
96 | if (mGson == null) {
97 | mGson = new Gson();
98 | }
99 |
100 | Photo[] photos = new Photo[0];
101 | try {
102 | photos = mGson.fromJson(response.body().string(), Photo[].class);
103 | } catch (IOException e) {
104 | e.printStackTrace();
105 | }
106 | for (Photo photo : photos) {
107 | photo.setThumbUrl(photo.getThumbUrl().replace("http", "https"));
108 | photo.setUrl(photo.getUrl().replace("http", "https"));
109 | }
110 | mPhotos = new ArrayList<>(Arrays.asList(photos));
111 |
112 | request = new Request.Builder()
113 | .url("https://jsonplaceholder.typicode.com/posts")
114 | .build();
115 |
116 | try {
117 | response = mOkHttpClient.newCall(request).execute();
118 | } catch (IOException e) {
119 | e.printStackTrace();
120 | response = null;
121 | }
122 |
123 | if (response == null) {
124 | return null;
125 | }
126 |
127 | Post[] posts = new Post[0];
128 | try {
129 | posts = mGson.fromJson(response.body().string(), Post[].class);
130 | } catch (IOException e) {
131 | e.printStackTrace();
132 | }
133 | mPosts = new ArrayList<>(Arrays.asList(posts));
134 |
135 | return null;
136 | }
137 |
138 | @Override
139 | protected void onPostExecute(Void aVoid) {
140 | mBinding.swipeRefresh.setRefreshing(false);
141 | mBinding.textList.setHasFixedSize(true);
142 | mAdapter = new MainListAdapter(mPosts, mPhotos);
143 | mBinding.textList.setAdapter(mAdapter);
144 | }
145 | }
146 |
147 | }
148 |
--------------------------------------------------------------------------------
/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/example/samvidmistry/recyclerviewadditions/MainListAdapter.java:
--------------------------------------------------------------------------------
1 | package com.example.samvidmistry.recyclerviewadditions;
2 |
3 | import android.os.AsyncTask;
4 | import android.support.v4.view.GravityCompat;
5 | import android.support.v7.util.DiffUtil;
6 | import android.support.v7.widget.LinearLayoutManager;
7 | import android.support.v7.widget.LinearSnapHelper;
8 | import android.support.v7.widget.OrientationHelper;
9 | import android.support.v7.widget.RecyclerView;
10 | import android.support.v7.widget.SnapHelper;
11 | import android.view.LayoutInflater;
12 | import android.view.ViewGroup;
13 |
14 | import com.example.samvidmistry.recyclerviewadditions.databinding.LayoutHeaderBinding;
15 | import com.example.samvidmistry.recyclerviewadditions.databinding.LayoutPostBinding;
16 |
17 | import java.util.ArrayList;
18 | import java.util.List;
19 |
20 | /**
21 | * Created by samvidmistry on 11/19/16.
22 | */
23 |
24 | public class MainListAdapter extends RecyclerView.Adapter {
25 | private List mPosts;
26 | private List mPhotos;
27 | private int mFirstPhoto = 0;
28 |
29 | public MainListAdapter(List posts, List photos) {
30 | mPosts = posts;
31 | mPhotos = photos;
32 | }
33 |
34 | @Override
35 | public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
36 | if (holder instanceof RecyclerViewHolder) {
37 | ((RecyclerViewHolder) holder).mBinding.photolist.scrollToPosition(mFirstPhoto);
38 | }
39 | }
40 |
41 | @Override
42 | public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
43 | if (holder instanceof RecyclerViewHolder) {
44 | mFirstPhoto = ((LinearLayoutManager) ((RecyclerViewHolder) holder).mBinding.photolist
45 | .getLayoutManager()).findFirstVisibleItemPosition();
46 | }
47 | }
48 |
49 | @Override
50 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
51 | RecyclerView.ViewHolder viewHolder;
52 | if (viewType == R.layout.layout_header) {
53 | viewHolder = new RecyclerViewHolder(LayoutHeaderBinding.inflate(
54 | LayoutInflater.from(parent.getContext()),
55 | parent,
56 | false
57 | ));
58 | } else {
59 | viewHolder = new PostViewHolder(LayoutPostBinding.inflate(
60 | LayoutInflater.from(parent.getContext()), parent, false));
61 | }
62 |
63 | return viewHolder;
64 | }
65 |
66 | @Override
67 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
68 | if (holder instanceof RecyclerViewHolder) {
69 | if (((RecyclerViewHolder) holder).mBinding.photolist.getAdapter() == null) {
70 | ((RecyclerViewHolder) holder).mBinding.photolist.setHasFixedSize(true);
71 | ((RecyclerViewHolder) holder).mBinding.photolist.setAdapter(
72 | new PhotoListAdapter(mPhotos)
73 | );
74 | LinearLayoutManager layoutManager = (LinearLayoutManager)
75 | ((RecyclerViewHolder) holder).mBinding.photolist.getLayoutManager();
76 | layoutManager.setOrientation(OrientationHelper.HORIZONTAL);
77 | ((RecyclerViewHolder) holder).mBinding.photolist.
78 | addItemDecoration(new PhotoItemDecoration());
79 | new GravitySnapHelper(GravityCompat.START).
80 | attachToRecyclerView(((RecyclerViewHolder) holder).mBinding.photolist);
81 | }
82 | } else if (holder instanceof PostViewHolder) {
83 | ((PostViewHolder) holder).mBinding.setPost(mPosts.get(holder.getAdapterPosition() - 1));
84 | ((PostViewHolder) holder).mBinding.executePendingBindings();
85 | }
86 | }
87 |
88 | public void deleteItems(int count) {
89 | new DiffTask(count).execute();
90 | }
91 |
92 | @Override
93 | public int getItemCount() {
94 | return mPosts == null ? 0 : mPosts.size() + 1;
95 | }
96 |
97 | @Override
98 | public int getItemViewType(int position) {
99 | if (position == 0) {
100 | return R.layout.layout_header;
101 | }
102 |
103 | return R.layout.layout_post;
104 | }
105 |
106 | private class DiffTask extends AsyncTask {
107 | private int mCount;
108 |
109 | private DiffTask(int count) {
110 | mCount = count;
111 | }
112 |
113 | @Override
114 | protected DiffUtil.DiffResult doInBackground(Void... voids) {
115 | final ArrayList posts = new ArrayList<>(mPosts);
116 | for (int i = 0; i < mCount; i++) {
117 | posts.remove(i);
118 | }
119 |
120 | return DiffUtil.calculateDiff(new DiffUtil.Callback() {
121 | @Override
122 | public int getOldListSize() {
123 | return mPosts.size();
124 | }
125 |
126 | @Override
127 | public int getNewListSize() {
128 | return posts.size();
129 | }
130 |
131 | @Override
132 | public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
133 | return mPosts.get(oldItemPosition).getTitle().equals(
134 | posts.get(newItemPosition).getTitle()
135 | );
136 | }
137 |
138 | @Override
139 | public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
140 | return mPosts.get(oldItemPosition).getBody().equals(
141 | posts.get(newItemPosition).getBody()
142 | );
143 | }
144 | });
145 | }
146 |
147 | @Override
148 | protected void onPreExecute() {
149 |
150 | }
151 |
152 | @Override
153 | protected void onPostExecute(DiffUtil.DiffResult result) {
154 | result.dispatchUpdatesTo(MainListAdapter.this);
155 | }
156 | }
157 |
158 | }
159 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/samvidmistry/recyclerviewadditions/GravitySnapHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 The Android Open Source Project
3 | * Copyright (C) 2016 Rúben Sousa
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.example.samvidmistry.recyclerviewadditions;
18 |
19 | import android.annotation.SuppressLint;
20 | import android.support.annotation.NonNull;
21 | import android.support.annotation.Nullable;
22 | import android.support.v7.widget.LinearLayoutManager;
23 | import android.support.v7.widget.LinearSnapHelper;
24 | import android.support.v7.widget.OrientationHelper;
25 | import android.support.v7.widget.RecyclerView;
26 | import android.view.Gravity;
27 | import android.view.View;
28 |
29 |
30 | public class GravitySnapHelper extends LinearSnapHelper {
31 |
32 | private OrientationHelper verticalHelper;
33 | private OrientationHelper horizontalHelper;
34 | private int gravity;
35 | private boolean isSupportRtL;
36 |
37 | @SuppressLint("RtlHardcoded")
38 | public GravitySnapHelper(int gravity) {
39 | this.gravity = gravity;
40 | if (this.gravity == Gravity.LEFT) {
41 | this.gravity = Gravity.START;
42 | } else if (this.gravity == Gravity.RIGHT) {
43 | this.gravity = Gravity.END;
44 | }
45 | }
46 |
47 | @Override
48 | public void attachToRecyclerView(@Nullable RecyclerView recyclerView)
49 | throws IllegalStateException {
50 | super.attachToRecyclerView(recyclerView);
51 | }
52 |
53 | @Override
54 | public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager,
55 | @NonNull View targetView) {
56 | int[] out = new int[2];
57 |
58 | if (layoutManager.canScrollHorizontally()) {
59 | if (gravity == Gravity.START) {
60 | out[0] = distanceToStart(targetView, getHorizontalHelper(layoutManager));
61 | } else { // END
62 | out[0] = distanceToEnd(targetView, getHorizontalHelper(layoutManager));
63 | }
64 | } else {
65 | out[0] = 0;
66 | }
67 |
68 | if (layoutManager.canScrollVertically()) {
69 | if (gravity == Gravity.TOP) {
70 | out[1] = distanceToStart(targetView, getVerticalHelper(layoutManager));
71 | } else { // BOTTOM
72 | out[1] = distanceToEnd(targetView, getVerticalHelper(layoutManager));
73 | }
74 | } else {
75 | out[1] = 0;
76 | }
77 | return out;
78 | }
79 |
80 | @Override
81 | public View findSnapView(RecyclerView.LayoutManager layoutManager) {
82 | if (layoutManager instanceof LinearLayoutManager) {
83 | switch (gravity) {
84 | case Gravity.START:
85 | return findStartView(layoutManager, getHorizontalHelper(layoutManager));
86 | case Gravity.TOP:
87 | return findStartView(layoutManager, getVerticalHelper(layoutManager));
88 | case Gravity.END:
89 | return findEndView(layoutManager, getHorizontalHelper(layoutManager));
90 | case Gravity.BOTTOM:
91 | return findEndView(layoutManager, getVerticalHelper(layoutManager));
92 | }
93 | }
94 |
95 | return super.findSnapView(layoutManager);
96 | }
97 |
98 | private int distanceToStart(View targetView, OrientationHelper helper) {
99 | if (isSupportRtL) {
100 | return distanceToEnd(targetView, helper);
101 | }
102 | return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
103 | }
104 |
105 | private int distanceToEnd(View targetView, OrientationHelper helper) {
106 | if (isSupportRtL) {
107 | return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
108 | }
109 | return helper.getDecoratedEnd(targetView) - helper.getEndAfterPadding();
110 | }
111 |
112 | private View findStartView(RecyclerView.LayoutManager layoutManager,
113 | OrientationHelper helper) {
114 |
115 | if (layoutManager instanceof LinearLayoutManager) {
116 | int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
117 |
118 | if (firstChild == RecyclerView.NO_POSITION) {
119 | return null;
120 | }
121 |
122 | View child = layoutManager.findViewByPosition(firstChild);
123 |
124 | if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2
125 | && helper.getDecoratedEnd(child) > 0) {
126 | return child;
127 | } else {
128 | if (((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition()
129 | == layoutManager.getItemCount() - 1) {
130 | return null;
131 | } else {
132 | return layoutManager.findViewByPosition(firstChild + 1);
133 | }
134 | }
135 | }
136 |
137 | return super.findSnapView(layoutManager);
138 | }
139 |
140 | private View findEndView(RecyclerView.LayoutManager layoutManager,
141 | OrientationHelper helper) {
142 |
143 | if (layoutManager instanceof LinearLayoutManager) {
144 | int lastChild = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
145 |
146 | if (lastChild == RecyclerView.NO_POSITION) {
147 | return null;
148 | }
149 |
150 | View child = layoutManager.findViewByPosition(lastChild);
151 |
152 | if (helper.getDecoratedStart(child) + helper.getDecoratedMeasurement(child) / 2
153 | <= helper.getTotalSpace()) {
154 | return child;
155 | } else {
156 | if (((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition()
157 | == 0) {
158 | return null;
159 | } else {
160 | return layoutManager.findViewByPosition(lastChild - 1);
161 | }
162 | }
163 | }
164 |
165 | return super.findSnapView(layoutManager);
166 | }
167 |
168 | private OrientationHelper getVerticalHelper(RecyclerView.LayoutManager layoutManager) {
169 | if (verticalHelper == null) {
170 | verticalHelper = OrientationHelper.createVerticalHelper(layoutManager);
171 | }
172 | return verticalHelper;
173 | }
174 |
175 | private OrientationHelper getHorizontalHelper(RecyclerView.LayoutManager layoutManager) {
176 | if (horizontalHelper == null) {
177 | horizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager);
178 | }
179 | return horizontalHelper;
180 | }
181 |
182 | }
183 |
--------------------------------------------------------------------------------
/.idea/codeStyleSettings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 | xmlns:android
82 | ^$
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 | xmlns:.*
92 | ^$
93 |
94 |
95 | BY_NAME
96 |
97 |
98 |
99 |
100 |
101 |
102 | .*:id
103 | http://schemas.android.com/apk/res/android
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 | .*:name
113 | http://schemas.android.com/apk/res/android
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 | name
123 | ^$
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 | style
133 | ^$
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 | .*
143 | ^$
144 |
145 |
146 | BY_NAME
147 |
148 |
149 |
150 |
151 |
152 |
153 | .*:layout_width
154 | http://schemas.android.com/apk/res/android
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 | .*:layout_height
164 | http://schemas.android.com/apk/res/android
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 | .*:layout_.*
174 | http://schemas.android.com/apk/res/android
175 |
176 |
177 | BY_NAME
178 |
179 |
180 |
181 |
182 |
183 |
184 | .*:width
185 | http://schemas.android.com/apk/res/android
186 |
187 |
188 | BY_NAME
189 |
190 |
191 |
192 |
193 |
194 |
195 | .*:height
196 | http://schemas.android.com/apk/res/android
197 |
198 |
199 | BY_NAME
200 |
201 |
202 |
203 |
204 |
205 |
206 | .*
207 | http://schemas.android.com/apk/res/android
208 |
209 |
210 | BY_NAME
211 |
212 |
213 |
214 |
215 |
216 |
217 | .*
218 | .*
219 |
220 |
221 | BY_NAME
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
--------------------------------------------------------------------------------