Skip to content

Commit

Permalink
RecyclerView Animation Creation #1
Browse files Browse the repository at this point in the history
*Animate RecyclerView items when scrolling up and down.
  • Loading branch information
rayliverified committed Dec 12, 2017
1 parent 23a67bb commit 29cbf9d
Show file tree
Hide file tree
Showing 7 changed files with 523 additions and 391 deletions.
26 changes: 13 additions & 13 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
compileSdkVersion 27
buildToolsVersion "27.0.1"
defaultConfig {
applicationId "stream.rocketnotes"
minSdkVersion 16
targetSdkVersion 26
versionCode 15
versionName "1.2.2"
targetSdkVersion 27
versionCode 16
versionName "1.3.0"
}
buildTypes {
release {
Expand All @@ -33,15 +33,15 @@ repositories {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.github.searchy2:CustomPermissionsDialogue:1.3'
implementation 'com.github.searchy2:CustomAlertViewDialogue:1.8.1'
implementation 'com.github.searchy2:AndroidCrossPromotion:1.3.3'
implementation 'com.github.searchy2:CustomPermissionsDialogue:1.4'
implementation 'com.github.searchy2:CustomAlertViewDialogue:1.9'
implementation 'com.github.searchy2:AndroidCrossPromotion:1.4'

implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.android.support:cardview-v7:26.1.0'
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:support-v4:27.0.2'
implementation 'com.android.support:design:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'
implementation 'com.android.support:cardview-v7:27.0.2'
implementation 'com.android.volley:volley:1.0.0'

implementation 'com.afollestad:material-camera:0.4.5' //https://github.com/afollestad/material-camera
Expand Down
225 changes: 122 additions & 103 deletions app/src/main/java/stream/rocketnotes/ImageItemViewholder.java
Original file line number Diff line number Diff line change
@@ -1,103 +1,122 @@
package stream.rocketnotes;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.util.List;

import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
import eu.davidea.viewholders.FlexibleViewHolder;

public class ImageItemViewholder extends AbstractFlexibleItem<ImageItemViewholder.ImageViewHolder> {

private String id;
private String image;

public ImageItemViewholder(String id, String image) {
this.id = id;
this.image = image;
}

/**
* When an item is equals to another?
* Write your own concept of equals, mandatory to implement.
* This will be explained in the "Item interfaces" Wiki page.
*/
@Override
public boolean equals(Object inObject) {
if (inObject instanceof ImageItemViewholder) {
ImageItemViewholder inItem = (ImageItemViewholder) inObject;
return this.id.equals(inItem.id);
}
return false;
}

/**
* You should implement also this method if equals() is implemented.
* This method, if implemented, has several implications that Adapter handles better:
* - The Hash increases performance in big list during Update & Filter operations.
* - Collapsing many expandable items is much faster.
* - You might want to activate stable ids via Constructor for RV, if your id
* is unique (read more in the wiki page: "Setting Up Advanced").
*/
@Override
public int hashCode() {
return id.hashCode();
}

/**
* For the item type we need an int value: the layoutResID is sufficient.
*/
@Override
public int getLayoutRes() {
return R.layout.item_image;
}

@Override
public ImageViewHolder createViewHolder(View view, FlexibleAdapter adapter) {
return new ImageViewHolder(view, adapter);
}

/**
* The Adapter and the Payload are provided to get more specific information from it.
*/
@Override
public void bindViewHolder(FlexibleAdapter adapter, ImageViewHolder holder, final int position,
List payloads) {
final Context context = holder.itemView.getContext();
// UXCam.occludeSensitiveView(holder.noteImage);

Picasso.with(context).load(image).transform(ImageTransformer.getSquare(holder.noteImage)).placeholder(R.drawable.image_picture_full).into(holder.noteImage);

holder.noteImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, ImageViewerActivity.class);
intent.setAction(Constants.OPEN_IMAGE_SINGLE);
intent.putExtra(Constants.ID, Integer.valueOf(id));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}

/**
* The ViewHolder used by this item.
* Extending from FlexibleViewHolder is recommended especially when you will use
* more advanced features.
*/
public class ImageViewHolder extends FlexibleViewHolder {

public ImageView noteImage;

public ImageViewHolder(View view, FlexibleAdapter adapter) {
super(view, adapter);
noteImage = view.findViewById(R.id.item_image);
}
}
}
package stream.rocketnotes;

import android.animation.Animator;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.util.List;

import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.davidea.flexibleadapter.helpers.AnimatorHelper;
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
import eu.davidea.viewholders.FlexibleViewHolder;

public class ImageItemViewholder extends AbstractFlexibleItem<ImageItemViewholder.ImageViewHolder> {

private String id;
private String image;

public ImageItemViewholder(String id, String image) {
this.id = id;
this.image = image;
}

/**
* When an item is equals to another?
* Write your own concept of equals, mandatory to implement.
* This will be explained in the "Item interfaces" Wiki page.
*/
@Override
public boolean equals(Object inObject) {
if (inObject instanceof ImageItemViewholder) {
ImageItemViewholder inItem = (ImageItemViewholder) inObject;
return this.id.equals(inItem.id);
}
return false;
}

/**
* You should implement also this method if equals() is implemented.
* This method, if implemented, has several implications that Adapter handles better:
* - The Hash increases performance in big list during Update & Filter operations.
* - Collapsing many expandable items is much faster.
* - You might want to activate stable ids via Constructor for RV, if your id
* is unique (read more in the wiki page: "Setting Up Advanced").
*/
@Override
public int hashCode() {
return id.hashCode();
}

/**
* For the item type we need an int value: the layoutResID is sufficient.
*/
@Override
public int getLayoutRes() {
return R.layout.item_image;
}

@Override
public ImageViewHolder createViewHolder(View view, FlexibleAdapter adapter) {
return new ImageViewHolder(view, adapter);
}

/**
* The Adapter and the Payload are provided to get more specific information from it.
*/
@Override
public void bindViewHolder(FlexibleAdapter adapter, ImageViewHolder holder, final int position,
List payloads) {
final Context context = holder.itemView.getContext();
// UXCam.occludeSensitiveView(holder.noteImage);

Picasso.with(context).load(image).transform(ImageTransformer.getSquare(holder.noteImage)).placeholder(R.drawable.image_picture_full).into(holder.noteImage);

holder.noteImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, ImageViewerActivity.class);
intent.setAction(Constants.OPEN_IMAGE_SINGLE);
intent.putExtra(Constants.ID, Integer.valueOf(id));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}

/**
* The ViewHolder used by this item.
* Extending from FlexibleViewHolder is recommended especially when you will use
* more advanced features.
*/
public class ImageViewHolder extends FlexibleViewHolder {

public ImageView noteImage;

public ImageViewHolder(View view, FlexibleAdapter adapter) {
super(view, adapter);
noteImage = view.findViewById(R.id.item_image);
}

@Override
public void scrollAnimators(@NonNull List<Animator> animators, int position, boolean isForward) {
if (mAdapter.getRecyclerView().getLayoutManager() instanceof GridLayoutManager) {
if (position % 2 != 0)
AnimatorHelper.slideInFromRightAnimator(animators, itemView, mAdapter.getRecyclerView(), 0.5f);
else
AnimatorHelper.slideInFromLeftAnimator(animators, itemView, mAdapter.getRecyclerView(), 0.5f);
} else {
if (isForward)
AnimatorHelper.slideInFromBottomAnimator(animators, itemView, mAdapter.getRecyclerView());
else
AnimatorHelper.slideInFromTopAnimator(animators, itemView, mAdapter.getRecyclerView());
}
}
}
}
29 changes: 20 additions & 9 deletions app/src/main/java/stream/rocketnotes/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.speech.RecognizerIntent;
import android.support.design.internal.NavigationMenu;
Expand All @@ -20,6 +21,7 @@
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
Expand All @@ -41,6 +43,7 @@
import java.util.Map;

import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.davidea.flexibleadapter.common.FlexibleItemAnimator;
import eu.davidea.flexibleadapter.common.SmoothScrollStaggeredLayoutManager;
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
import eu.davidea.flexibleadapter.items.IFlexible;
Expand All @@ -50,6 +53,7 @@
import stream.custompermissionsdialogue.utils.PermissionUtils;
import stream.rocketnotes.filter.FilterMaterialSearchView;
import stream.rocketnotes.filter.model.Filter;
import stream.rocketnotes.ui.LandingItemAnimator;
import stream.rocketnotes.utils.AnalyticsUtils;

public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {
Expand Down Expand Up @@ -98,18 +102,19 @@ public void OnClick(View view, Dialog dialog) {
dbHelper = new DatabaseHelper(mContext);
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
//Checks for first launch
if (sharedPref.getBoolean("prefs_first_start", true)) {

//Start sequence finished
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("prefs_tutorial_intro", false);
editor.apply();
}
// if (sharedPref.getBoolean("prefs_first_start", true)) {
//
// //Start sequence finished
// SharedPreferences.Editor editor = sharedPref.edit();
// editor.putBoolean("prefs_tutorial_intro", false);
// editor.apply();
// }

mAppBar = findViewById(R.id.app_bar);
mAppBar.addOnOffsetChangedListener(this);

InitializeRecyclerView(savedInstanceState);
InitializeRecyclerView();

// checkVoiceRecognition();
mFilterView = findViewById(R.id.sv);
SetupSearchBar();
Expand All @@ -135,17 +140,23 @@ protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}

private void InitializeRecyclerView(Bundle savedInstanceState) {
private void InitializeRecyclerView() {

// Optional but strongly recommended: Compose the initial list
List<IFlexible> myItems = getDatabaseList();

// Initialize the Adapter
mAdapter = new FlexibleAdapter<>(myItems);
mAdapter.setAnimationOnScrolling(true)
.setAnimationEntryStep(true)
.setAnimationOnReverseScrolling(true)
.setAnimationInterpolator(new DecelerateInterpolator())
.setAnimationDuration(300L);
mStaggeredLayoutManager = createNewStaggeredGridLayoutManager();

// Prepare the RecyclerView and attach the Adapter to it
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setItemViewCacheSize(0); //Setting ViewCache to 0 (default=2) will animate items better while scrolling down+up with LinearLayout
mRecyclerView.setLayoutManager(mStaggeredLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
Expand Down
Loading

0 comments on commit 29cbf9d

Please sign in to comment.