Permalink
Browse files

FeedContextMenuManager implementation

  • Loading branch information...
frogermcs committed Dec 18, 2014
1 parent 5d56f83 commit 510f4d8ae88afeb9996513de077c0983bba686d8
@@ -14,12 +14,16 @@
import android.view.animation.OvershootInterpolator;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import butterknife.ButterKnife;
import butterknife.InjectView;
import io.github.froger.instamaterial.view.FeedContextMenu;
import io.github.froger.instamaterial.view.FeedContextMenuManager;
public class MainActivity extends ActionBarActivity implements FeedAdapter.OnFeedItemClickListener {
public class MainActivity extends ActionBarActivity implements FeedAdapter.OnFeedItemClickListener,
FeedContextMenu.OnFeedContextMenuItemClickListener {
private static final int ANIM_DURATION_TOOLBAR = 300;
private static final int ANIM_DURATION_FAB = 400;
@@ -70,6 +74,12 @@ protected int getExtraLayoutSpace(RecyclerView.State state) {
feedAdapter = new FeedAdapter(this);
feedAdapter.setOnFeedItemClickListener(this);
rvFeed.setAdapter(feedAdapter);
rvFeed.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
FeedContextMenuManager.getInstance().onScrolled(recyclerView, dx, dy);
}
});
}
@Override
@@ -135,6 +145,27 @@ public void onCommentsClick(View v, int position) {
@Override
public void onMoreClick(View v, int itemPosition) {
FeedContextMenuManager.getInstance().toggleContextMenuFromView(v, itemPosition, this);
}
@Override
public void onReportClick(int feedItem) {
FeedContextMenuManager.getInstance().hideContextMenu();
}
@Override
public void onSharePhotoClick(int feedItem) {
FeedContextMenuManager.getInstance().hideContextMenu();
}
@Override
public void onCopyShareUrlClick(int feedItem) {
FeedContextMenuManager.getInstance().hideContextMenu();
}
@Override
public void onCancelClick(int feedItem) {
FeedContextMenuManager.getInstance().hideContextMenu();
}
}
@@ -0,0 +1,134 @@
package io.github.froger.instamaterial.view;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import io.github.froger.instamaterial.Utils;
/**
* Created by froger_mcs on 16.12.14.
*/
public class FeedContextMenuManager extends RecyclerView.OnScrollListener implements View.OnAttachStateChangeListener {
private static FeedContextMenuManager instance;
private FeedContextMenu contextMenuView;
private boolean isContextMenuDismissing;
private boolean isContextMenuShowing;
public static FeedContextMenuManager getInstance() {
if (instance == null) {
instance = new FeedContextMenuManager();
}
return instance;
}
private FeedContextMenuManager() {
}
public void toggleContextMenuFromView(View openingView, int feedItem, FeedContextMenu.OnFeedContextMenuItemClickListener listener) {
if (contextMenuView == null) {
showContextMenuFromView(openingView, feedItem, listener);
} else {
hideContextMenu();
}
}
private void showContextMenuFromView(final View openingView, int feedItem, FeedContextMenu.OnFeedContextMenuItemClickListener listener) {
if (!isContextMenuShowing) {
isContextMenuShowing = true;
contextMenuView = new FeedContextMenu(openingView.getContext());
contextMenuView.bindToItem(feedItem);
contextMenuView.addOnAttachStateChangeListener(this);
contextMenuView.setOnFeedMenuItemClickListener(listener);
((ViewGroup) openingView.getRootView().findViewById(android.R.id.content)).addView(contextMenuView);
contextMenuView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
contextMenuView.getViewTreeObserver().removeOnPreDrawListener(this);
setupContextMenuInitialPosition(openingView);
performShowAnimation();
return false;
}
});
}
}
private void setupContextMenuInitialPosition(View openingView) {
final int[] openingViewLocation = new int[2];
openingView.getLocationOnScreen(openingViewLocation);
int additionalBottomMargin = Utils.dpToPx(16);
contextMenuView.setTranslationX(openingViewLocation[0] - contextMenuView.getWidth() / 3);
contextMenuView.setTranslationY(openingViewLocation[1] - contextMenuView.getHeight() - additionalBottomMargin);
}
private void performShowAnimation() {
contextMenuView.setPivotX(contextMenuView.getWidth() / 2);
contextMenuView.setPivotY(contextMenuView.getHeight());
contextMenuView.setScaleX(0.1f);
contextMenuView.setScaleY(0.1f);
contextMenuView.animate()
.scaleX(1f).scaleY(1f)
.setDuration(150)
.setInterpolator(new OvershootInterpolator())
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
isContextMenuShowing = false;
}
});
}
public void hideContextMenu() {
if (!isContextMenuDismissing) {
isContextMenuDismissing = true;
performDismissAnimation();
}
}
private void performDismissAnimation() {
contextMenuView.setPivotX(contextMenuView.getWidth() / 2);
contextMenuView.setPivotY(contextMenuView.getHeight());
contextMenuView.animate()
.scaleX(0.1f).scaleY(0.1f)
.setDuration(150)
.setInterpolator(new AccelerateInterpolator())
.setStartDelay(100)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if (contextMenuView != null) {
contextMenuView.dismiss();
}
isContextMenuDismissing = false;
}
});
}
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (contextMenuView != null) {
hideContextMenu();
contextMenuView.setTranslationY(contextMenuView.getTranslationY() - dy);
}
}
@Override
public void onViewAttachedToWindow(View v) {
}
@Override
public void onViewDetachedFromWindow(View v) {
contextMenuView = null;
}
}

0 comments on commit 510f4d8

Please sign in to comment.