Skip to content

Commit 9003db5

Browse files
Animations added
Shapes for HomeMenu are added
1 parent 652371d commit 9003db5

13 files changed

Lines changed: 258 additions & 40 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ dependencies {
3434

3535
implementation project(':homemenu')
3636

37-
//implementation 'com.github.MuhammadTouseeq:MaterialHomeMenu-Library:1.0'
37+
//implementation 'com.github.MuhammadTouseeq:MaterialHomeMenu-Library:1.0.1'
3838

3939

4040

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.pakdev.materialhomemenu;
22

33
import android.os.Bundle;
4+
import android.view.View;
45
import android.widget.Toast;
56

67
import com.pakdev.homemenu.BaseMenuActivity;
@@ -13,33 +14,37 @@ public class MaterialHomeMenu extends BaseMenuActivity {
1314

1415
@Override
1516
protected void onCreate(Bundle savedInstanceState) {
17+
18+
//to show /hide icon
1619
setShowMenuIcon(true);
17-
setGridView(true);
18-
super.onCreate(savedInstanceState);
20+
//For List View
21+
setGridView(false);
22+
//For Grid View
23+
// setGridView(false, HomeMenu.DRAWABLE_SHAPE.ROUNDED);
24+
//setAnimation(HomeMenu.MENU_ANIMATION.FADE_IN, 500);
1925

26+
super.onCreate(savedInstanceState);
2027

2128
}
2229

23-
2430
@Override
2531
public ArrayList<HomeMenu> getHomeMenuList() {
2632

2733

28-
29-
ArrayList<HomeMenu> arrData=new ArrayList<>();
30-
arrData.add(new HomeMenu().setMenuTitle("Home").setMenuDesc("Description").setMenuIcon(R.drawable.ic_home));
31-
arrData.add(new HomeMenu().setMenuTitle("Home").setMenuDesc("Description").setMenuIcon(R.drawable.ic_home));
32-
arrData.add(new HomeMenu().setMenuTitle("Home").setMenuDesc("Description").setMenuIcon(R.drawable.ic_home));
33-
arrData.add(new HomeMenu().setMenuTitle("Home").setMenuDesc("Description").setMenuIcon(R.drawable.ic_home));
34-
arrData.add(new HomeMenu().setMenuTitle("Home").setMenuDesc("Description").setMenuIcon(R.drawable.ic_home));
34+
ArrayList<HomeMenu> arrData = new ArrayList<>();
3535
arrData.add(new HomeMenu().setMenuTitle("Home").setMenuDesc("Description").setMenuIcon(R.drawable.ic_home));
36+
arrData.add(new HomeMenu().setMenuTitle("About us").setMenuDesc("Description").setMenuIcon(R.drawable.ic_home));
37+
arrData.add(new HomeMenu().setMenuTitle("Sports").setMenuDesc("Description").setMenuIcon(R.drawable.ic_home));
38+
arrData.add(new HomeMenu().setMenuTitle("News").setMenuDesc("Description").setMenuIcon(R.drawable.ic_home));
39+
arrData.add(new HomeMenu().setMenuTitle("Help").setMenuDesc("Description").setMenuIcon(R.drawable.ic_home));
40+
arrData.add(new HomeMenu().setMenuTitle("Entertainments").setMenuDesc("Description").setMenuIcon(R.drawable.ic_home));
3641

3742
return arrData;
3843
}
3944

4045
@Override
4146
public void onMenuClick(HomeMenu model, int position) {
4247

43-
Toast.makeText(this, "Click item is "+model.getMenuTitle(), Toast.LENGTH_SHORT).show();
48+
Toast.makeText(this, "Click item is " + model.getMenuTitle(), Toast.LENGTH_SHORT).show();
4449
}
4550
}

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
6+
android:orientation="vertical"
67
android:layout_height="match_parent">
78

9+
810
<androidx.recyclerview.widget.RecyclerView
911
android:id="@+id/recycleView"
1012
android:layout_width="match_parent"
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.pakdev.homemenu;
2+
3+
import android.animation.AnimatorSet;
4+
import android.animation.ObjectAnimator;
5+
import android.view.View;
6+
7+
public class AnimationUtils {
8+
9+
10+
11+
public static void setFadeAnimation(View itemView, int i, long DURATION) {
12+
// if(!on_attach){
13+
// i = -1;
14+
// }
15+
boolean isNotFirstItem = i == -1;
16+
i++;
17+
itemView.setAlpha(0.f);
18+
AnimatorSet animatorSet = new AnimatorSet();
19+
ObjectAnimator animator = ObjectAnimator.ofFloat(itemView, "alpha", 0.f, 0.5f, 1.0f);
20+
ObjectAnimator.ofFloat(itemView, "alpha", 0.f).start();
21+
animator.setStartDelay(isNotFirstItem ? DURATION / 2 : (i * DURATION / 3));
22+
animator.setDuration(500);
23+
animatorSet.play(animator);
24+
animator.start();
25+
}
26+
27+
public static void FromLeftToRight(View itemView, int i, long DURATION) {
28+
// if(!on_attach){
29+
// i = -1;
30+
// }
31+
boolean not_first_item = i == -1;
32+
i = i + 1;
33+
itemView.setTranslationX(-400f);
34+
itemView.setAlpha(0.f);
35+
AnimatorSet animatorSet = new AnimatorSet();
36+
ObjectAnimator animatorTranslateY = ObjectAnimator.ofFloat(itemView, "translationX", -400f, 0);
37+
ObjectAnimator animatorAlpha = ObjectAnimator.ofFloat(itemView, "alpha", 1.f);
38+
ObjectAnimator.ofFloat(itemView, "alpha", 0.f).start();
39+
animatorTranslateY.setStartDelay(not_first_item ? DURATION : (i * DURATION));
40+
animatorTranslateY.setDuration((not_first_item ? 2 : 1) * DURATION);
41+
animatorSet.playTogether(animatorTranslateY, animatorAlpha);
42+
animatorSet.start();
43+
}
44+
45+
46+
public static void FromRightToLeft(View itemView, int i,long duration) {
47+
// if(!on_attach){
48+
// i = -1;
49+
// }
50+
boolean not_first_item = i == -1;
51+
i = i + 1;
52+
itemView.setTranslationX(itemView.getX() +1000);
53+
itemView.setAlpha(0.f);
54+
AnimatorSet animatorSet = new AnimatorSet();
55+
ObjectAnimator animatorTranslateY = ObjectAnimator.ofFloat(itemView, "translationX", itemView.getX() + 400, 0);
56+
ObjectAnimator animatorAlpha = ObjectAnimator.ofFloat(itemView, "alpha", 1.f);
57+
ObjectAnimator.ofFloat(itemView, "alpha", 0.f).start();
58+
animatorTranslateY.setStartDelay(not_first_item ? duration : (i * duration));
59+
animatorTranslateY.setDuration((not_first_item ? 2 : 1) * duration);
60+
animatorSet.playTogether(animatorTranslateY, animatorAlpha);
61+
animatorSet.start();
62+
}
63+
}

homemenu/src/main/java/com/pakdev/homemenu/BaseMenuActivity.java

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import androidx.recyclerview.widget.RecyclerView;
1010

1111
import android.graphics.Point;
12+
import android.graphics.drawable.Drawable;
1213
import android.os.Bundle;
1314
import android.view.Display;
1415
import android.view.View;
@@ -20,58 +21,75 @@ public abstract class BaseMenuActivity extends AppCompatActivity {
2021
RecyclerView recyclerView;
2122
private boolean showMenuIcon;
2223
private boolean isGridView;
24+
private long ANIMATION_DURATION;
25+
private HomeMenu.DRAWABLE_SHAPE MenuShape;
26+
private HomeMenu.MENU_ANIMATION MenuAnimation;
27+
2328

2429
@Override
2530
protected void onCreate(Bundle savedInstanceState) {
2631
super.onCreate(savedInstanceState);
2732
setContentView(R.layout.activity_main);
2833

2934

35+
final HomeMenuAdapter adapter = new HomeMenuAdapter(getApplicationContext(), getHomeMenuList());
36+
recyclerView = findViewById(R.id.recycleView);
3037

31-
32-
33-
34-
final HomeMenuAdapter adapter=new HomeMenuAdapter(getApplicationContext(),getHomeMenuList());
35-
recyclerView=findViewById(R.id.recycleView);
36-
37-
38+
//attach the recycle view for drag and drop
3839
ItemTouchHelper.Callback callback =
3940
new SimpleItemTouchHelperCallback(adapter);
4041
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
4142
touchHelper.attachToRecyclerView(recyclerView);
43+
//binding the layout managers to recycleview
4244

43-
if(isGridView)
44-
{
45+
if (isGridView) {
4546
bindGridLayoutManager(adapter);
46-
}
47-
else {
47+
} else {
4848

4949
bindLinearLayoutManager();
50-
adapter.setItemSize(0);
50+
5151
}
52+
recyclerView.setAdapter(adapter);
5253

5354

54-
recyclerView.setAdapter(adapter);
55+
//setting the home menu binding
5556
adapter.setMenuIcon(showMenuIcon);
57+
adapter.setGridMenu(isGridView, MenuShape);
58+
59+
adapter.setAnimationType(MenuAnimation, ANIMATION_DURATION);
5660

5761
adapter.setClickListener(new HomeMenuAdapter.ItemClickListener() {
5862
@Override
5963
public void onItemClick(View view, int position) {
6064

6165
HomeMenu model = adapter.getMenumAt(position);
6266

63-
onMenuClick(model,position);
67+
onMenuClick(model, position);
6468
}
6569
});
6670

6771

72+
}
73+
74+
//set gridview and its shape
75+
protected void setGridView(boolean isGridMenu, HomeMenu.DRAWABLE_SHAPE menuShape) {
6876

77+
isGridView = isGridMenu;
78+
MenuShape = menuShape;
6979
}
7080

71-
public void setGridView(boolean gridView) {
81+
//set grid view with default shape
82+
protected void setGridView(boolean gridView) {
7283
isGridView = gridView;
84+
MenuShape = HomeMenu.DRAWABLE_SHAPE.DEFAULT;
85+
}
86+
87+
protected void setAnimation(HomeMenu.MENU_ANIMATION animation, long Duration) {
88+
MenuAnimation = animation;
89+
ANIMATION_DURATION = Duration;
7390
}
7491

92+
7593
private void bindLinearLayoutManager() {
7694
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
7795
}
@@ -80,8 +98,8 @@ private void bindGridLayoutManager(HomeMenuAdapter adapter) {
8098
Display display = BaseMenuActivity.this.getWindowManager().getDefaultDisplay();
8199
Point size = new Point();
82100
display.getSize(size);
83-
int y = size.y;
84-
y=(int)y/4;
101+
int y = size.y;
102+
y = (int) y / 4;
85103
GridLayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 2);
86104
recyclerView.setLayoutManager(layoutManager);
87105
adapter.setItemSize(y);
@@ -92,8 +110,7 @@ protected void setShowMenuIcon(boolean showMenuIcon) {
92110
}
93111

94112
public abstract ArrayList<HomeMenu> getHomeMenuList();
95-
public abstract void onMenuClick(HomeMenu model, int position);
96-
97113

114+
public abstract void onMenuClick(HomeMenu model, int position);
98115

99116
}

homemenu/src/main/java/com/pakdev/homemenu/HomeMenu.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
public class HomeMenu {
44

5+
public enum DRAWABLE_SHAPE{
6+
CIRCLE,ROUNDED,DEFAULT
7+
}
8+
9+
public enum MENU_ANIMATION{
10+
FADE_IN,LEFT_TO_RIGHT,RIGHT_TO_LEFT
11+
}
12+
513
private String mMenuTitle;
614
private String mMenuDesc;
715
private int mMenuIcon;

homemenu/src/main/java/com/pakdev/homemenu/HomeMenuAdapter.java

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.pakdev.homemenu;
22

3+
import android.animation.AnimatorSet;
4+
import android.animation.ObjectAnimator;
35
import android.content.Context;
46

5-
import android.graphics.Color;
67
import android.view.LayoutInflater;
78
import android.view.View;
89
import android.view.ViewGroup;
@@ -11,15 +12,14 @@
1112

1213
import androidx.annotation.NonNull;
1314
import androidx.recyclerview.widget.GridLayoutManager;
14-
import androidx.recyclerview.widget.ItemTouchHelper;
1515
import androidx.recyclerview.widget.RecyclerView;
1616

1717

1818
import java.util.ArrayList;
1919
import java.util.Collections;
2020

2121

22-
public class HomeMenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements ItemTouchHelperAdapter {
22+
public class HomeMenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements ItemTouchHelperCallback {
2323

2424

2525
private final LayoutInflater mInflater;
@@ -28,7 +28,13 @@ public class HomeMenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
2828
public static ItemClickListener mClickListener;
2929
public boolean isMenuIcon;
3030
public boolean isGridMenu;
31+
3132
public int itemSize;
33+
long DURATION = 0;
34+
private boolean on_attach = true;
35+
36+
public HomeMenu.DRAWABLE_SHAPE drawable_shape;
37+
public HomeMenu.MENU_ANIMATION animationType;
3238

3339

3440
// data is passed into the constructor
@@ -44,12 +50,23 @@ public class HomeMenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
4450
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
4551
View view=null;
4652

47-
if(itemSize!=0) {
53+
if(isGridMenu&&drawable_shape.equals(HomeMenu.DRAWABLE_SHAPE.DEFAULT)) {
4854
view = mInflater.inflate(R.layout.rv_item_grid_menu, parent, false);
4955
GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) view.getLayoutParams();
5056
params.height = itemSize;
5157
view.setLayoutParams(params);
52-
}
58+
59+
}
60+
else if(isGridMenu&&drawable_shape.equals(HomeMenu.DRAWABLE_SHAPE.CIRCLE))
61+
{
62+
63+
view = mInflater.inflate(R.layout.rv_item_circle_grid_menu, parent, false);
64+
}
65+
else if(isGridMenu&&drawable_shape.equals(HomeMenu.DRAWABLE_SHAPE.ROUNDED))
66+
{
67+
68+
view = mInflater.inflate(R.layout.rv_item_rounded_grid_menu, parent, false);
69+
}
5370
else{
5471
view = mInflater.inflate(R.layout.rv_item_menu, parent, false);
5572
}
@@ -69,14 +86,39 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi
6986

7087
viewHolder.imgMenuIcon.setVisibility(isMenuIcon?View.VISIBLE:View.GONE);
7188

89+
90+
91+
if(animationType!=null&&animationType.equals(HomeMenu.MENU_ANIMATION.FADE_IN)) {
92+
AnimationUtils.setFadeAnimation(holder.itemView, position, DURATION);
93+
}
94+
else if(animationType!=null&&animationType.equals(HomeMenu.MENU_ANIMATION.LEFT_TO_RIGHT)) {
95+
AnimationUtils.FromLeftToRight(holder.itemView, position, DURATION);
96+
}
97+
else if(animationType!=null&&animationType.equals(HomeMenu.MENU_ANIMATION.RIGHT_TO_LEFT)) {
98+
AnimationUtils.FromRightToLeft(holder.itemView, position, DURATION);
99+
}
100+
}
101+
102+
103+
public void setGridMenu(boolean gridMenu,HomeMenu.DRAWABLE_SHAPE shape) {
104+
isGridMenu = gridMenu;
105+
drawable_shape=shape;
106+
notifyDataSetChanged();
72107
}
73108

74109
public void setItemSize(int itemSize) {
75110
this.itemSize = itemSize;
76111
}
77112

113+
public void setAnimationType(HomeMenu.MENU_ANIMATION animationType,long duration) {
114+
this.animationType = animationType;
115+
this.DURATION=duration;
116+
notifyDataSetChanged();
117+
}
118+
78119
public void setMenuIcon(boolean menuIcon) {
79120
isMenuIcon = menuIcon;
121+
notifyDataSetChanged();
80122
}
81123

82124
public HomeMenu getMenumAt(int positon)
@@ -147,4 +189,5 @@ public interface ItemClickListener {
147189
void onItemClick(View view, int position);
148190
}
149191

192+
150193
}

homemenu/src/main/java/com/pakdev/homemenu/ItemTouchHelperAdapter.java renamed to homemenu/src/main/java/com/pakdev/homemenu/ItemTouchHelperCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
* @author Paul Burke (ipaulpro)
2323
*/
24-
public interface ItemTouchHelperAdapter {
24+
public interface ItemTouchHelperCallback {
2525

2626
/**
2727
* Called when an item has been dragged far enough to trigger a move. This is called every time

0 commit comments

Comments
 (0)