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();
/**
* 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
*/
@@ -60,8 +60,8 @@ import org.ligi.snackengage.snacks.BaseSnack;
import java.net.URLDecoder;
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";
@@ -111,10 +111,7 @@ public class MainActivity extends QKActivity implements SlidingMenu.OnOpenListen
mSlidingMenu = (SlidingMenu) findViewById(R.id.sliding_menu);
setSlidingTabEnabled(mPrefs.getBoolean(SettingsFragment.SLIDING_TAB, false));
mSlidingMenu.setOnOpenListener(this);
mSlidingMenu.setOnCloseListener(this);
mSlidingMenu.setOnOpenedListener(this);
mSlidingMenu.setOnClosedListener(this);
mSlidingMenu.setListener(this);
mSlidingMenu.setContent();
mSlidingMenu.setMenu();
mSlidingMenu.showContent(false);
@@ -604,6 +601,11 @@ public class MainActivity extends QKActivity implements SlidingMenu.OnOpenListen
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
* 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
public void inflateToolbar(Menu menu, MenuInflater inflater, Context context) {
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
public void inflateToolbar(Menu menu, MenuInflater inflater, Context context) {
inflater.inflate(R.menu.conversation, menu);
@@ -135,6 +135,11 @@ public class SearchFragment extends QKContentFragment implements RecyclerCursorA
KeyboardUtils.showAndFocus(mContext, mQuery);
}
@Override
public void onMenuChanging(float percentOpen) {
}
@Override
public void inflateToolbar(Menu menu, MenuInflater inflater, Context context) {
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
public void inflateToolbar(Menu menu, MenuInflater inflater, Context context) {
if (mContext == null) {
@@ -2,7 +2,6 @@ package com.moez.QKSMS.ui.view.slidingmenu;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.support.v4.view.KeyEventCompat;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.VelocityTrackerCompat;
@@ -75,8 +74,7 @@ public class CustomViewAbove extends ViewGroup {
private OnPageChangeListener mOnPageChangeListener;
private OnPageChangeListener mInternalPageChangeListener;
private SlidingMenu.OnClosedListener mClosedListener;
private SlidingMenu.OnOpenedListener mOpenedListener;
private SlidingMenu.SlidingMenuListener mListener;
/**
* Callback interface for responding to changing state of the selected page.
@@ -211,11 +209,11 @@ public class CustomViewAbove extends ViewGroup {
if (notify) {
// Notify the listeners here, because the client has switched content.
if (isMenuOpen()) {
if (mOpenedListener != null)
mOpenedListener.onOpened();
if (mListener != null)
mListener.onOpened();
} else {
if (mClosedListener != null)
mClosedListener.onClosed();
if (mListener != null)
mListener.onClosed();
}
}
@@ -251,12 +249,8 @@ public class CustomViewAbove extends ViewGroup {
mOnPageChangeListener = listener;
}
public void setOnOpenedListener(SlidingMenu.OnOpenedListener l) {
mOpenedListener = l;
}
public void setOnClosedListener(SlidingMenu.OnClosedListener l) {
mClosedListener = l;
public void setListener(SlidingMenu.SlidingMenuListener listener) {
mListener = listener;
}
/**
@@ -332,11 +326,11 @@ public class CustomViewAbove extends ViewGroup {
if (dx == 0 && dy == 0) {
completeScroll();
if (isMenuOpen()) {
if (mOpenedListener != null)
mOpenedListener.onOpened();
if (mListener != null)
mListener.onOpened();
} else {
if (mClosedListener != null)
mClosedListener.onClosed();
if (mListener != null)
mListener.onClosed();
}
return;
}
@@ -478,11 +472,11 @@ public class CustomViewAbove extends ViewGroup {
scrollTo(x, y);
}
if (isMenuOpen()) {
if (mOpenedListener != null)
mOpenedListener.onOpened();
if (mListener != null)
mListener.onOpened();
} else {
if (mClosedListener != null)
mClosedListener.onClosed();
if (mListener != null)
mListener.onClosed();
}
}
mScrolling = false;
@@ -545,7 +539,7 @@ public class CustomViewAbove extends ViewGroup {
if (thisTouchAllowed(ev)) {
mIsBeingDragged = false;
mIsUnableToDrag = false;
if (isMenuOpen() && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
if (isMenuOpen() && mViewBehind.menuTouchInQuickReturn(mContent, ev.getX() + mScrollX)) {
mQuickReturn = true;
}
} else {
@@ -641,7 +635,7 @@ public class CustomViewAbove extends ViewGroup {
}
mActivePointerId = INVALID_POINTER;
endDrag();
} else if (mQuickReturn && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
} else if (mQuickReturn && mViewBehind.menuTouchInQuickReturn(mContent, ev.getX() + mScrollX)) {
// close the menu
setCurrentItem(1);
endDrag();
@@ -696,9 +690,13 @@ public class CustomViewAbove extends ViewGroup {
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
float percentOpen = getPercentOpen();
mScrollX = x;
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) {
@@ -803,14 +801,12 @@ public class CustomViewAbove extends ViewGroup {
handled = arrowScroll(FOCUS_RIGHT);
break;
case KeyEvent.KEYCODE_TAB:
if (Build.VERSION.SDK_INT >= 11) {
// The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
// before Android 3.0. Ignore the tab key on those devices.
if (KeyEventCompat.hasNoModifiers(event)) {
handled = arrowScroll(FOCUS_FORWARD);
} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
handled = arrowScroll(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.
if (KeyEventCompat.hasNoModifiers(event)) {
handled = arrowScroll(FOCUS_FORWARD);
} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
handled = arrowScroll(FOCUS_BACKWARD);
}
break;
}
@@ -16,6 +16,10 @@ public class CustomViewBehind extends ViewGroup {
private int mWidthOffset;
private boolean mChildrenEnabled;
private float mScrollScale;
private Drawable mShadowDrawable;
private int mShadowWidth;
public CustomViewBehind(Context context) {
this(context, null);
}
@@ -87,10 +91,6 @@ public class CustomViewBehind extends ViewGroup {
mContent.measure(contentWidth, contentHeight);
}
private float mScrollScale;
private Drawable mShadowDrawable;
private int mShadowWidth;
public void setScrollScale(float scrollScale) {
mScrollScale = scrollScale;
}
@@ -138,10 +138,9 @@ public class CustomViewBehind extends ViewGroup {
* Returns whether x is on a menu.
*
* @param content content
* @param currPage current page
* @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();
}
@@ -12,7 +12,6 @@ import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import com.moez.QKSMS.R;
import com.moez.QKSMS.common.utils.Units;
import com.moez.QKSMS.ui.view.slidingmenu.CustomViewAbove.OnPageChangeListener;
/**
@@ -26,83 +25,18 @@ public class SlidingMenu extends RelativeLayout {
private CustomViewAbove mViewAbove;
private CustomViewBehind mViewBehind;
private OnOpenListener mOpenListener;
private OnOpenedListener mOpenedListener;
private OnCloseListener mCloseListener;
private OnClosedListener mClosedListener;
private SlidingMenuListener mListener;
/**
* 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.
*/
public interface SlidingMenuListener {
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();
}
/**
* 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();
}
/**
* 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 onChanging(float percentOpen);
}
/**
@@ -151,14 +85,14 @@ public class SlidingMenu extends RelativeLayout {
}
public void onPageSelected(int position, boolean anim) {
if (position == POSITION_OPEN && mOpenListener != null && anim) {
mOpenListener.onOpen();
} else if (position == POSITION_OPEN && mOpenedListener != null) {
mOpenedListener.onOpened();
} else if (position == POSITION_CLOSE && mCloseListener != null && anim) {
mCloseListener.onClose();
} else if (position == POSITION_CLOSE && mClosedListener != null) {
mClosedListener.onClosed();
if (position == POSITION_OPEN && mListener != null && anim) {
mListener.onOpen();
} else if (position == POSITION_OPEN && mListener != null) {
mListener.onOpened();
} else if (position == POSITION_CLOSE && mListener != null && anim) {
mListener.onClose();
} else if (position == POSITION_CLOSE && mListener != null) {
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
* to the above view.
*
* @param res the new content
*/
public void setContent() {
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
* to the behind view.
*
* @param res the new content
*/
public void setMenu() {
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.
*/
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);
}
/**
* Sets the OnOpenListener. {@link OnOpenListener#onOpen() OnOpenListener.onOpen()} will be called when the SlidingMenu is opened
*
* @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 void setListener(SlidingMenuListener listener) {
mListener = listener;
mViewAbove.setListener(listener);
}
public static class SavedState extends BaseSavedState {