Skip to content

Commit

Permalink
- added layout animation to animate RecyclerView items smoothly in
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Zawadzki committed Sep 30, 2016
1 parent b4cb311 commit 1cb3cc2
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 15 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ This library tries to mimic the activity chooser version you can see on Android

## Download (from JCenter)
```groovy
compile 'com.github.zawadz88:material-activity-chooser:0.1.1'
compile 'com.github.zawadz88:material-activity-chooser:0.2.0'
```

## Supported features
- showing the activity chooser as a bottom sheet dialog on all OS versions starting from Jelly Bean (API 16)+
- a smooth animation when the items appear in the bottom sheet
- setting custom bottom sheet titles
- setting different intents per activity, e.g. to have different messages for different applications when sharing
- use custom styles for the bottom sheet
Expand Down Expand Up @@ -152,7 +153,16 @@ You can inflate an entirely custom empty view as well, e.g.

## Styling the dialog to your own needs
You can override the default styling of the Activity Chooser to your own needs.
The easiest way is to override the
The easiest way is to override the ```MACTheme``` and replace the styles in the custom attributes. For more info see the sample app.

## Disabling the item animation
By default there is a layout animation on the RecyclerView which animates the items as they show up.
It can be however disabled by extending the ```MACTheme``` and overriding ```mac_bottomSheetRecyclerViewStyle``` with a style with this animation disabled, e.g.
```java
<style name="NoAnimationRecyclerViewStyle" parent="MACBottomSheetRecyclerView">
<item name="android:layoutAnimation">@null</item>
</style>
```

## Tracking when an activity was clicked
You might want to track/log when an activity has been selected to handle your intent. To do so you can extend the default MaterialActivityChooserActivity.
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ org.gradle.jvmargs=-Xmx1536m

POM_GROUP_ID=com.github.zawadz88
POM_ARTIFACT_ID=material-activity-chooser
POM_VERSION=0.1.1
POM_VERSION=0.2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2014 Freddie (Musenkishi) Lust-Hed
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.zawadz88.activitychooser.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.GridLayoutAnimationController;

/**
* An extension of RecyclerView, focused more on resembling a GridView.
* Unlike {@link android.support.v7.widget.RecyclerView}, this view can handle
* {@code <gridLayoutAnimation>} as long as you provide it a
* {@link android.support.v7.widget.GridLayoutManager} in
* {@code setLayoutManager(LayoutManager layout)}.<br>
*
* Created by Freddie (Musenkishi) Lust-Hed.<br>
*
* animationParams.column and animationParams.row values were corrected.<br>
*
* Source: <a href="https://github.com/antoniolg/MaterializeYourApp/blob/master/app/src/main/java/com/antonioleiva/materializeyourapp/widgets/GridRecyclerView.java">GitHub</a>
*/
public class GridRecyclerView extends RecyclerView {

public GridRecyclerView(Context context) {
super(context);
}

public GridRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public GridRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public void setLayoutManager(LayoutManager layout) {
if (layout instanceof GridLayoutManager) {
super.setLayoutManager(layout);
} else {
throw new ClassCastException("You should only use a GridLayoutManager with GridRecyclerView.");
}
}

@Override
protected void attachLayoutAnimationParameters(View child, @NonNull ViewGroup.LayoutParams params, int index, int count) {

if (getAdapter() != null && getLayoutManager() instanceof GridLayoutManager) {

GridLayoutAnimationController.AnimationParameters animationParams =
(GridLayoutAnimationController.AnimationParameters) params.layoutAnimationParameters;

if (animationParams == null) {
animationParams = new GridLayoutAnimationController.AnimationParameters();
params.layoutAnimationParameters = animationParams;
}

int columns = ((GridLayoutManager) getLayoutManager()).getSpanCount();


animationParams.count = count;
animationParams.index = index;
animationParams.columnsCount = columns;
animationParams.rowsCount = count / columns + 1;

animationParams.column = index % columns;
animationParams.row = index % columns + index / columns;

} else {
super.attachLayoutAnimationParameters(child, params, index, count);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- Attributes -->
<public type="attr" name="mac_bottomSheetStyle" />
<public type="attr" name="mac_bottomSheetTitleStyle" />
<public type="attr" name="mac_bottomSheetRecyclerViewStyle" />
<public type="attr" name="mac_emptyViewStyle" />
<public type="attr" name="mac_emptyViewTitleStyle" />
<public type="attr" name="mac_emptyViewButtonStyle" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<gridLayoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/mac_slide_up_with_fade_in"
android:animationOrder="normal"
android:columnDelay="10%"
android:direction="top_to_bottom|left_to_right"
android:rowDelay="5%" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime"
android:interpolator="@android:anim/decelerate_interpolator">

<translate
android:fromYDelta="100%"
android:toYDelta="0" />

<alpha
android:fromAlpha="0"
android:toAlpha="1" />

</set>
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,34 @@
style="?attr/mac_bottomSheetTitleStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"/>
android:clickable="true" />

<android.support.v7.widget.RecyclerView
<com.github.zawadz88.activitychooser.widget.GridRecyclerView
android:id="@+id/mac_recycler_view"
style="?attr/mac_bottomSheetRecyclerViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingBottom="@dimen/mac_recycler_view_padding_bottom" />
android:layout_height="wrap_content" />

<LinearLayout
android:id="@+id/mac_empty_view"
style="?attr/mac_emptyViewStyle"
android:visibility="gone"
android:clickable="true">
android:clickable="true"
android:visibility="gone">

<TextView
android:id="@+id/mac_empty_view_title"
style="?attr/mac_emptyViewTitleStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mac_default_no_activities_found_message"
style="?attr/mac_emptyViewTitleStyle" />
android:text="@string/mac_default_no_activities_found_message" />

<Button
android:id="@+id/mac_empty_view_button"
style="?attr/mac_emptyViewButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mac_default_empty_view_button_message"
style="?attr/mac_emptyViewButtonStyle"
android:visibility="gone"/>
android:visibility="gone" />

<ViewStub
android:id="@+id/mac_empty_view_custom_view_stub"
Expand Down
1 change: 1 addition & 0 deletions material-activity-chooser/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

<attr name="mac_bottomSheetStyle" format="reference" />
<attr name="mac_bottomSheetTitleStyle" format="reference" />
<attr name="mac_bottomSheetRecyclerViewStyle" format="reference" />
<attr name="mac_emptyViewStyle" format="reference" />
<attr name="mac_emptyViewTitleStyle" format="reference" />
<attr name="mac_emptyViewButtonStyle" format="reference" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<item name="android:background">@drawable/mac_title_background_selector</item>
</style>

<style name="MACBottomSheetRecyclerView">
<item name="android:clipToPadding">false</item>
<item name="android:layoutAnimation">@anim/mac_grid_layout_animation</item>
<item name="android:paddingBottom">@dimen/mac_recycler_view_padding_bottom</item>
</style>

<style name="MACActivityItem">
<item name="android:paddingLeft">@dimen/mac_activity_item_lateral_padding</item>
<item name="android:paddingRight">@dimen/mac_activity_item_lateral_padding</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<item name="mac_bottomSheetStyle">@style/MACBottomSheet</item>
<item name="mac_bottomSheetTitleStyle">@style/MACBottomSheetTitle</item>
<item name="mac_bottomSheetRecyclerViewStyle">@style/MACBottomSheetRecyclerView</item>

<item name="mac_emptyViewStyle">@style/MACEmptyView</item>
<item name="mac_emptyViewTitleStyle">@style/MACEmptyViewTitle</item>
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<string name="share_text_with_title">Share text with title</string>
<string name="share_text_with_title_from_resource">Share text with title from resource</string>
<string name="share_text_with_secondary_intents">Share text with secondary intents</string>
<string name="share_text_styled">Share text with custom styled activity</string>
<string name="share_text_styled">Share text with custom style without item animation</string>
<string name="show_empty_default">Show default empty view</string>
<string name="show_empty_custom_text_from_resource">Show empty view with custom text from resource</string>
<string name="show_empty_custom_text">Show empty view with custom text</string>
Expand Down
5 changes: 5 additions & 0 deletions sample/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<item name="mac_bottomSheetTitleStyle">@style/CustomBottomSheetTitle</item>
<item name="mac_activityItemLabelStyle">@style/CustomActivityItemLabel</item>
<item name="mac_activityItemSpanCount">2</item>
<item name="mac_bottomSheetRecyclerViewStyle">@style/NoAnimationRecyclerViewStyle</item>
</style>

<style name="CustomBottomSheet" parent="MACBottomSheet">
Expand All @@ -24,6 +25,10 @@
<item name="android:background">@android:color/black</item>
</style>

<style name="NoAnimationRecyclerViewStyle" parent="MACBottomSheetRecyclerView">
<item name="android:layoutAnimation">@null</item>
</style>

<style name="CustomActivityItemLabel" parent="MACActivityItemLabel">
<item name="android:textColor">@android:color/white</item>
</style>
Expand Down

0 comments on commit 1cb3cc2

Please sign in to comment.