Add ability to listen for updates to content fragment position

This commit is contained in:
Moez Bhatti
2015-12-24 13:56:40 -05:00
parent 1f6d320b67
commit cdd964c5d3
9 changed files with 84 additions and 168 deletions
@@ -32,6 +32,14 @@ public interface ContentFragment {
*/ */
void onContentClosed(); void onContentClosed();
/**
* Called multiple times as the content is opening or closing. Allows it to receive updates
* of the position. Currently used to animate to the new theme color
*
* @param percentOpen
*/
void onMenuChanging(float percentOpen);
/** /**
* Allows the MainActivity to delegate setting of the Toolbar title and menu * Allows the MainActivity to delegate setting of the Toolbar title and menu
*/ */
@@ -60,8 +60,8 @@ import org.ligi.snackengage.snacks.BaseSnack;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.Collection; import java.util.Collection;
public class MainActivity extends QKActivity implements SlidingMenu.OnOpenListener, SlidingMenu.OnCloseListener,
SlidingMenu.OnOpenedListener, SlidingMenu.OnClosedListener, LiveView { public class MainActivity extends QKActivity implements LiveView, SlidingMenu.SlidingMenuListener {
private final String TAG = "MainActivity"; private final String TAG = "MainActivity";
@@ -111,10 +111,7 @@ public class MainActivity extends QKActivity implements SlidingMenu.OnOpenListen
mSlidingMenu = (SlidingMenu) findViewById(R.id.sliding_menu); mSlidingMenu = (SlidingMenu) findViewById(R.id.sliding_menu);
setSlidingTabEnabled(mPrefs.getBoolean(SettingsFragment.SLIDING_TAB, false)); setSlidingTabEnabled(mPrefs.getBoolean(SettingsFragment.SLIDING_TAB, false));
mSlidingMenu.setOnOpenListener(this); mSlidingMenu.setListener(this);
mSlidingMenu.setOnCloseListener(this);
mSlidingMenu.setOnOpenedListener(this);
mSlidingMenu.setOnClosedListener(this);
mSlidingMenu.setContent(); mSlidingMenu.setContent();
mSlidingMenu.setMenu(); mSlidingMenu.setMenu();
mSlidingMenu.showContent(false); mSlidingMenu.showContent(false);
@@ -604,6 +601,11 @@ public class MainActivity extends QKActivity implements SlidingMenu.OnOpenListen
if (mContent != null) mContent.onContentOpened(); if (mContent != null) mContent.onContentOpened();
} }
@Override
public void onChanging(float percentOpen) {
if (mContent != null) mContent.onMenuChanging(percentOpen);
}
/** /**
* Build and show the proper delete thread dialog. The UI is slightly different * Build and show the proper delete thread dialog. The UI is slightly different
* depending on whether there are locked messages in the thread(s) and whether we're * depending on whether there are locked messages in the thread(s) and whether we're
@@ -168,6 +168,11 @@ public class ComposeFragment extends QKContentFragment implements ActivityLaunch
} }
} }
@Override
public void onMenuChanging(float percentOpen) {
}
@Override @Override
public void inflateToolbar(Menu menu, MenuInflater inflater, Context context) { public void inflateToolbar(Menu menu, MenuInflater inflater, Context context) {
inflater.inflate(R.menu.compose, menu); inflater.inflate(R.menu.compose, menu);
@@ -773,6 +773,11 @@ public class MessageListFragment extends QKContentFragment implements ActivityLa
} }
} }
@Override
public void onMenuChanging(float percentOpen) {
Log.d(TAG, "onChanging: " + percentOpen);
}
@Override @Override
public void inflateToolbar(Menu menu, MenuInflater inflater, Context context) { public void inflateToolbar(Menu menu, MenuInflater inflater, Context context) {
inflater.inflate(R.menu.conversation, menu); inflater.inflate(R.menu.conversation, menu);
@@ -135,6 +135,11 @@ public class SearchFragment extends QKContentFragment implements RecyclerCursorA
KeyboardUtils.showAndFocus(mContext, mQuery); KeyboardUtils.showAndFocus(mContext, mQuery);
} }
@Override
public void onMenuChanging(float percentOpen) {
}
@Override @Override
public void inflateToolbar(Menu menu, MenuInflater inflater, Context context) { public void inflateToolbar(Menu menu, MenuInflater inflater, Context context) {
inflater.inflate(R.menu.search, menu); inflater.inflate(R.menu.search, menu);
@@ -725,6 +725,11 @@ public class SettingsFragment extends PreferenceFragment implements Preference.O
} }
@Override
public void onMenuChanging(float percentOpen) {
}
@Override @Override
public void inflateToolbar(Menu menu, MenuInflater inflater, Context context) { public void inflateToolbar(Menu menu, MenuInflater inflater, Context context) {
if (mContext == null) { if (mContext == null) {
@@ -2,7 +2,6 @@ package com.moez.QKSMS.ui.view.slidingmenu;
import android.content.Context; import android.content.Context;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.os.Build;
import android.support.v4.view.KeyEventCompat; import android.support.v4.view.KeyEventCompat;
import android.support.v4.view.MotionEventCompat; import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.VelocityTrackerCompat; import android.support.v4.view.VelocityTrackerCompat;
@@ -75,8 +74,7 @@ public class CustomViewAbove extends ViewGroup {
private OnPageChangeListener mOnPageChangeListener; private OnPageChangeListener mOnPageChangeListener;
private OnPageChangeListener mInternalPageChangeListener; private OnPageChangeListener mInternalPageChangeListener;
private SlidingMenu.OnClosedListener mClosedListener; private SlidingMenu.SlidingMenuListener mListener;
private SlidingMenu.OnOpenedListener mOpenedListener;
/** /**
* Callback interface for responding to changing state of the selected page. * Callback interface for responding to changing state of the selected page.
@@ -211,11 +209,11 @@ public class CustomViewAbove extends ViewGroup {
if (notify) { if (notify) {
// Notify the listeners here, because the client has switched content. // Notify the listeners here, because the client has switched content.
if (isMenuOpen()) { if (isMenuOpen()) {
if (mOpenedListener != null) if (mListener != null)
mOpenedListener.onOpened(); mListener.onOpened();
} else { } else {
if (mClosedListener != null) if (mListener != null)
mClosedListener.onClosed(); mListener.onClosed();
} }
} }
@@ -251,12 +249,8 @@ public class CustomViewAbove extends ViewGroup {
mOnPageChangeListener = listener; mOnPageChangeListener = listener;
} }
public void setOnOpenedListener(SlidingMenu.OnOpenedListener l) { public void setListener(SlidingMenu.SlidingMenuListener listener) {
mOpenedListener = l; mListener = listener;
}
public void setOnClosedListener(SlidingMenu.OnClosedListener l) {
mClosedListener = l;
} }
/** /**
@@ -332,11 +326,11 @@ public class CustomViewAbove extends ViewGroup {
if (dx == 0 && dy == 0) { if (dx == 0 && dy == 0) {
completeScroll(); completeScroll();
if (isMenuOpen()) { if (isMenuOpen()) {
if (mOpenedListener != null) if (mListener != null)
mOpenedListener.onOpened(); mListener.onOpened();
} else { } else {
if (mClosedListener != null) if (mListener != null)
mClosedListener.onClosed(); mListener.onClosed();
} }
return; return;
} }
@@ -478,11 +472,11 @@ public class CustomViewAbove extends ViewGroup {
scrollTo(x, y); scrollTo(x, y);
} }
if (isMenuOpen()) { if (isMenuOpen()) {
if (mOpenedListener != null) if (mListener != null)
mOpenedListener.onOpened(); mListener.onOpened();
} else { } else {
if (mClosedListener != null) if (mListener != null)
mClosedListener.onClosed(); mListener.onClosed();
} }
} }
mScrolling = false; mScrolling = false;
@@ -545,7 +539,7 @@ public class CustomViewAbove extends ViewGroup {
if (thisTouchAllowed(ev)) { if (thisTouchAllowed(ev)) {
mIsBeingDragged = false; mIsBeingDragged = false;
mIsUnableToDrag = false; mIsUnableToDrag = false;
if (isMenuOpen() && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) { if (isMenuOpen() && mViewBehind.menuTouchInQuickReturn(mContent, ev.getX() + mScrollX)) {
mQuickReturn = true; mQuickReturn = true;
} }
} else { } else {
@@ -641,7 +635,7 @@ public class CustomViewAbove extends ViewGroup {
} }
mActivePointerId = INVALID_POINTER; mActivePointerId = INVALID_POINTER;
endDrag(); endDrag();
} else if (mQuickReturn && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) { } else if (mQuickReturn && mViewBehind.menuTouchInQuickReturn(mContent, ev.getX() + mScrollX)) {
// close the menu // close the menu
setCurrentItem(1); setCurrentItem(1);
endDrag(); endDrag();
@@ -696,9 +690,13 @@ public class CustomViewAbove extends ViewGroup {
@Override @Override
public void scrollTo(int x, int y) { public void scrollTo(int x, int y) {
super.scrollTo(x, y); super.scrollTo(x, y);
float percentOpen = getPercentOpen();
mScrollX = x; mScrollX = x;
mViewBehind.scrollBehindTo(mContent, x, y); mViewBehind.scrollBehindTo(mContent, x, y);
((SlidingMenu) getParent()).manageLayers(getPercentOpen()); ((SlidingMenu) getParent()).manageLayers(percentOpen);
if (mListener != null) {
mListener.onChanging(percentOpen);
}
} }
private int determineTargetPage(float pageOffset, int velocity, int deltaX) { private int determineTargetPage(float pageOffset, int velocity, int deltaX) {
@@ -803,14 +801,12 @@ public class CustomViewAbove extends ViewGroup {
handled = arrowScroll(FOCUS_RIGHT); handled = arrowScroll(FOCUS_RIGHT);
break; break;
case KeyEvent.KEYCODE_TAB: case KeyEvent.KEYCODE_TAB:
if (Build.VERSION.SDK_INT >= 11) { // The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
// The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD // before Android 3.0. Ignore the tab key on those devices.
// before Android 3.0. Ignore the tab key on those devices. if (KeyEventCompat.hasNoModifiers(event)) {
if (KeyEventCompat.hasNoModifiers(event)) { handled = arrowScroll(FOCUS_FORWARD);
handled = arrowScroll(FOCUS_FORWARD); } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) { handled = arrowScroll(FOCUS_BACKWARD);
handled = arrowScroll(FOCUS_BACKWARD);
}
} }
break; break;
} }
@@ -16,6 +16,10 @@ public class CustomViewBehind extends ViewGroup {
private int mWidthOffset; private int mWidthOffset;
private boolean mChildrenEnabled; private boolean mChildrenEnabled;
private float mScrollScale;
private Drawable mShadowDrawable;
private int mShadowWidth;
public CustomViewBehind(Context context) { public CustomViewBehind(Context context) {
this(context, null); this(context, null);
} }
@@ -87,10 +91,6 @@ public class CustomViewBehind extends ViewGroup {
mContent.measure(contentWidth, contentHeight); mContent.measure(contentWidth, contentHeight);
} }
private float mScrollScale;
private Drawable mShadowDrawable;
private int mShadowWidth;
public void setScrollScale(float scrollScale) { public void setScrollScale(float scrollScale) {
mScrollScale = scrollScale; mScrollScale = scrollScale;
} }
@@ -138,10 +138,9 @@ public class CustomViewBehind extends ViewGroup {
* Returns whether x is on a menu. * Returns whether x is on a menu.
* *
* @param content content * @param content content
* @param currPage current page
* @param x the position of the touch * @param x the position of the touch
*/ */
public boolean menuTouchInQuickReturn(View content, int currPage, float x) { public boolean menuTouchInQuickReturn(View content, float x) {
return x >= content.getLeft(); return x >= content.getLeft();
} }
@@ -12,7 +12,6 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import com.moez.QKSMS.R; import com.moez.QKSMS.R;
import com.moez.QKSMS.common.utils.Units;
import com.moez.QKSMS.ui.view.slidingmenu.CustomViewAbove.OnPageChangeListener; import com.moez.QKSMS.ui.view.slidingmenu.CustomViewAbove.OnPageChangeListener;
/** /**
@@ -26,83 +25,18 @@ public class SlidingMenu extends RelativeLayout {
private CustomViewAbove mViewAbove; private CustomViewAbove mViewAbove;
private CustomViewBehind mViewBehind; private CustomViewBehind mViewBehind;
private OnOpenListener mOpenListener; private SlidingMenuListener mListener;
private OnOpenedListener mOpenedListener;
private OnCloseListener mCloseListener;
private OnClosedListener mClosedListener;
/** public interface SlidingMenuListener {
* The listener interface for receiving onOpen events.
* The class that is interested in processing a onOpen
* event implements this interface, and the object created
* with that class is registered with a component using the
* component's <code>addOnOpenListener<code> method. When
* the onOpen event occurs, that object's appropriate
* method is invoked
*/
public interface OnOpenListener {
/**
* On open.
*/
void onOpen(); void onOpen();
}
/**
* The listener interface for receiving onOpened events.
* The class that is interested in processing a onOpened
* event implements this interface, and the object created
* with that class is registered with a component using the
* component's <code>addOnOpenedListener<code> method. When
* the onOpened event occurs, that object's appropriate
* method is invoked.
*
* @see OnOpenedEvent
*/
public interface OnOpenedListener {
/**
* On opened.
*/
void onOpened(); void onOpened();
}
/**
* The listener interface for receiving onClose events.
* The class that is interested in processing a onClose
* event implements this interface, and the object created
* with that class is registered with a component using the
* component's <code>addOnCloseListener<code> method. When
* the onClose event occurs, that object's appropriate
* method is invoked.
*
* @see OnCloseEvent
*/
public interface OnCloseListener {
/**
* On close.
*/
void onClose(); void onClose();
}
/**
* The listener interface for receiving onClosed events.
* The class that is interested in processing a onClosed
* event implements this interface, and the object created
* with that class is registered with a component using the
* component's <code>addOnClosedListener<code> method. When
* the onClosed event occurs, that object's appropriate
* method is invoked.
*
* @see OnClosedEvent
*/
public interface OnClosedListener {
/**
* On closed.
*/
void onClosed(); void onClosed();
void onChanging(float percentOpen);
} }
/** /**
@@ -151,14 +85,14 @@ public class SlidingMenu extends RelativeLayout {
} }
public void onPageSelected(int position, boolean anim) { public void onPageSelected(int position, boolean anim) {
if (position == POSITION_OPEN && mOpenListener != null && anim) { if (position == POSITION_OPEN && mListener != null && anim) {
mOpenListener.onOpen(); mListener.onOpen();
} else if (position == POSITION_OPEN && mOpenedListener != null) { } else if (position == POSITION_OPEN && mListener != null) {
mOpenedListener.onOpened(); mListener.onOpened();
} else if (position == POSITION_CLOSE && mCloseListener != null && anim) { } else if (position == POSITION_CLOSE && mListener != null && anim) {
mCloseListener.onClose(); mListener.onClose();
} else if (position == POSITION_CLOSE && mClosedListener != null) { } else if (position == POSITION_CLOSE && mListener != null) {
mClosedListener.onClosed(); mListener.onClosed();
} }
} }
}); });
@@ -168,8 +102,6 @@ public class SlidingMenu extends RelativeLayout {
/** /**
* Set the above view content from a layout resource. The resource will be inflated, adding all top-level views * Set the above view content from a layout resource. The resource will be inflated, adding all top-level views
* to the above view. * to the above view.
*
* @param res the new content
*/ */
public void setContent() { public void setContent() {
mViewAbove.setContent(LayoutInflater.from(getContext()).inflate(R.layout.content_frame, null)); mViewAbove.setContent(LayoutInflater.from(getContext()).inflate(R.layout.content_frame, null));
@@ -187,8 +119,6 @@ public class SlidingMenu extends RelativeLayout {
/** /**
* Set the behind view (menu) content from a layout resource. The resource will be inflated, adding all top-level views * Set the behind view (menu) content from a layout resource. The resource will be inflated, adding all top-level views
* to the behind view. * to the behind view.
*
* @param res the new content
*/ */
public void setMenu() { public void setMenu() {
mViewBehind.setContent(LayoutInflater.from(getContext()).inflate(R.layout.menu_frame, null)); mViewBehind.setContent(LayoutInflater.from(getContext()).inflate(R.layout.menu_frame, null));
@@ -270,51 +200,12 @@ public class SlidingMenu extends RelativeLayout {
* @param i The margin, in pixels, on the right of the screen that the behind view scrolls to. * @param i The margin, in pixels, on the right of the screen that the behind view scrolls to.
*/ */
public void setBehindOffset(int i) { public void setBehindOffset(int i) {
// RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)mViewBehind.getLayoutParams());
// int bottom = params.bottomMargin;
// int top = params.topMargin;
// int left = params.leftMargin;
// params.setMargins(left, top, i, bottom);
mViewBehind.setWidthOffset(i); mViewBehind.setWidthOffset(i);
} }
/** public void setListener(SlidingMenuListener listener) {
* Sets the OnOpenListener. {@link OnOpenListener#onOpen() OnOpenListener.onOpen()} will be called when the SlidingMenu is opened mListener = listener;
* mViewAbove.setListener(listener);
* @param listener the new OnOpenListener
*/
public void setOnOpenListener(OnOpenListener listener) {
mOpenListener = listener;
}
/**
* Sets the OnCloseListener. {@link OnCloseListener#onClose() OnCloseListener.onClose()} will be called when any one of the SlidingMenu is closed
*
* @param listener the new setOnCloseListener
*/
public void setOnCloseListener(OnCloseListener listener) {
mCloseListener = listener;
}
/**
* Sets the OnOpenedListener. {@link OnOpenedListener#onOpened() OnOpenedListener.onOpened()} will be called after the SlidingMenu is opened
*
* @param listener the new OnOpenedListener
*/
public void setOnOpenedListener(OnOpenedListener listener) {
mOpenedListener = listener;
mViewAbove.setOnOpenedListener(listener);
}
/**
* Sets the OnClosedListener. {@link OnClosedListener#onClosed() OnClosedListener.onClosed()} will be called after the SlidingMenu is closed
*
* @param listener the new OnClosedListener
*/
public void setOnClosedListener(OnClosedListener listener) {
mClosedListener = listener;
mViewAbove.setOnClosedListener(listener);
} }
public static class SavedState extends BaseSavedState { public static class SavedState extends BaseSavedState {