Skip to content
This repository was archived by the owner on Jun 8, 2024. It is now read-only.

Commit 93d3bc6

Browse files
committed
HomeFragment: Animate list item when showing
Signed-off-by: Fung <fython@163.com>
1 parent 213e963 commit 93d3bc6

5 files changed

Lines changed: 78 additions & 3 deletions

File tree

mobile/src/main/java/info/papdt/express/helper/ui/fragment/home/BaseFragment.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
import info.papdt.express.helper.ui.adapter.HomePackageListAdapter;
2323
import info.papdt.express.helper.ui.callback.OnDataRemovedCallback;
2424
import info.papdt.express.helper.ui.common.AbsFragment;
25+
import info.papdt.express.helper.view.AnimatedRecyclerView;
2526
import info.papdt.express.helper.widget.SwipeRefreshLayout;
2627

2728
public abstract class BaseFragment extends AbsFragment implements SwipeRefreshLayout.OnRefreshListener {
2829

2930
private SwipeRefreshLayout mRefreshLayout;
30-
private RecyclerView mRecyclerView;
31+
private AnimatedRecyclerView mRecyclerView;
3132
private LinearLayout mEmptyView;
3233

3334
private RecyclerView.Adapter mAdapter;
@@ -36,6 +37,7 @@ public abstract class BaseFragment extends AbsFragment implements SwipeRefreshLa
3637
private PackageDatabase mDatabase;
3738
private static Context sInstance = null;
3839
private final static int FLAG_REFRESH_LIST = 0, FLAG_UPDATE_ADAPTER_ONLY = 1;
40+
private boolean hasPlayedAnimation = false;
3941

4042
int eggCount = 0, bigEggCount = 0;
4143

@@ -124,6 +126,13 @@ protected PackageDatabase getDatabase() {
124126
return mDatabase;
125127
}
126128

129+
protected void playListAnimation() {
130+
if (!hasPlayedAnimation) {
131+
hasPlayedAnimation = true;
132+
mRecyclerView.scheduleLayoutAnimation();
133+
}
134+
}
135+
127136
protected void setAdapter(RecyclerView.Adapter adapter) {
128137
this.mAdapter = adapter;
129138
mRecyclerView.setAdapter(mSwipeManager.createWrappedAdapter(mAdapter));
@@ -176,6 +185,7 @@ public void handleMessage(Message msg) {
176185
case FLAG_UPDATE_ADAPTER_ONLY:
177186
if (mAdapter != null) {
178187
mAdapter.notifyDataSetChanged();
188+
playListAnimation();
179189
mEmptyView.setVisibility(mAdapter != null && mAdapter.getItemCount() > 0 ? View.GONE : View.VISIBLE);
180190
}
181191
break;
@@ -193,6 +203,7 @@ protected Void doInBackground(Void... voids) {
193203

194204
@Override
195205
protected void onPostExecute(Void msg) {
206+
hasPlayedAnimation = false;
196207
mRefreshLayout.setRefreshing(false);
197208
mHandler.sendEmptyMessage(FLAG_UPDATE_ADAPTER_ONLY);
198209
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package info.papdt.express.helper.view;
2+
3+
import android.content.Context;
4+
import android.support.annotation.Nullable;
5+
import android.support.v7.widget.LinearLayoutManager;
6+
import android.support.v7.widget.RecyclerView;
7+
import android.util.AttributeSet;
8+
import android.view.View;
9+
import android.view.ViewGroup;
10+
import android.view.animation.LayoutAnimationController;
11+
12+
public class AnimatedRecyclerView extends RecyclerView {
13+
14+
public AnimatedRecyclerView(Context context) {
15+
super(context);
16+
}
17+
18+
public AnimatedRecyclerView(Context context, @Nullable AttributeSet attrs) {
19+
super(context, attrs);
20+
}
21+
22+
public AnimatedRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
23+
super(context, attrs, defStyle);
24+
}
25+
26+
@Override
27+
public void setLayoutManager(LayoutManager layoutManager) {
28+
if (layoutManager instanceof LinearLayoutManager) {
29+
super.setLayoutManager(layoutManager);
30+
} else {
31+
throw new ClassCastException("You should only use a LinearLayoutManager with this RecyclerView.");
32+
}
33+
}
34+
35+
@Override
36+
protected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params, int index, int count) {
37+
if (getAdapter() != null && getLayoutManager() instanceof LinearLayoutManager) {
38+
LayoutAnimationController.AnimationParameters animationParameters = params.layoutAnimationParameters;
39+
if (animationParameters == null) {
40+
animationParameters = new LayoutAnimationController.AnimationParameters();
41+
params.layoutAnimationParameters = animationParameters;
42+
}
43+
animationParameters.index = index;
44+
animationParameters.count = count;
45+
} else {
46+
super.attachLayoutAnimationParameters(child, params, index, count);
47+
}
48+
}
49+
50+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<set xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:duration="@android:integer/config_shortAnimTime">
4+
5+
<alpha android:fromAlpha="0" android:toAlpha="1"/>
6+
<scale android:pivotX="50%" android:pivotY="50%" android:fromXScale="75%" android:fromYScale="75%" android:toXScale="100%" android:toYScale="100%"/>
7+
8+
</set>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:animation="@anim/anim_item_fade_in"
4+
android:animationOrder="normal"
5+
android:delay="15%"/>

mobile/src/main/res/layout/fragment_home.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
android:layout_width="match_parent"
1010
android:layout_height="match_parent">
1111

12-
<android.support.v7.widget.RecyclerView
12+
<info.papdt.express.helper.view.AnimatedRecyclerView
1313
android:layout_width="match_parent"
1414
android:layout_height="match_parent"
1515
android:id="@+id/recycler_view"
16-
android:clipToPadding="false"/>
16+
android:clipToPadding="false"
17+
android:layoutAnimation="@anim/item_layout_animation"/>
1718

1819
<LinearLayout
1920
android:layout_width="match_parent"

0 commit comments

Comments
 (0)