Skip to content

Commit a4a5774

Browse files
author
jfeinstein10
committed
Fixed directional sliding.
1 parent bcbd1f1 commit a4a5774

File tree

6 files changed

+89
-36
lines changed

6 files changed

+89
-36
lines changed

example/res/drawable/shadow_right.xml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
3+
4+
<gradient
5+
android:endColor="#00000000"
6+
android:centerColor="#11000000"
7+
android:startColor="#33000000" />
8+
9+
</shape>

example/res/layout/test.xml

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
55
android:orientation="horizontal" >
6-
7-
<ImageView
8-
android:layout_width="2dp"
9-
android:layout_height="match_parent"
10-
android:src="#FF2222" />
116

127
<ImageView
8+
android:id="@+id/test_image"
139
android:layout_width="match_parent"
1410
android:layout_height="match_parent"
1511
android:background="#006699"

example/src/com/slidingmenu/example/BaseActivity.java

+44-15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.support.v4.app.ListFragment;
1313
import android.support.v4.view.ViewPager;
1414
import android.view.MenuItem;
15+
import android.widget.FrameLayout;
1516
import android.widget.LinearLayout;
1617

1718
import com.slidingmenu.lib.SlidingMenu;
@@ -21,37 +22,63 @@ public class BaseActivity extends SlidingFragmentActivity {
2122

2223
private int mTitleRes;
2324
protected ListFragment mFrag;
24-
25+
2526
public BaseActivity(int titleRes) {
2627
mTitleRes = titleRes;
2728
}
28-
29+
2930
@Override
3031
public void onCreate(Bundle savedInstanceState) {
3132
super.onCreate(savedInstanceState);
32-
33-
setTitle(mTitleRes);
3433

35-
// set the Behind View
36-
setBehindLeftContentView(R.layout.menu_frame);
37-
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
38-
mFrag = new SampleListFragment();
39-
t.replace(R.id.menu_frame, mFrag);
40-
t.commit();
41-
setBehindRightContentView(R.layout.test);
34+
setTitle(mTitleRes);
35+
36+
addLeft();
4237

4338
// customize the SlidingMenu
4439
SlidingMenu sm = getSlidingMenu();
4540
sm.setShadowWidthRes(R.dimen.shadow_width);
46-
sm.setShadowDrawable(R.drawable.shadow);
47-
sm.setBehindOffsetRes(R.dimen.actionbar_home_width, SlidingMenu.LEFT | SlidingMenu.RIGHT);
4841

4942
// customize the ActionBar
5043
if (Build.VERSION.SDK_INT >= 11) {
5144
getActionBar().setDisplayHomeAsUpEnabled(true);
5245
}
5346
}
5447

48+
private void addLeft() {
49+
FrameLayout left = new FrameLayout(this);
50+
left.setId("LEFT".hashCode());
51+
setBehindLeftContentView(left);
52+
getSupportFragmentManager()
53+
.beginTransaction()
54+
.replace("LEFT".hashCode(), new SampleListFragment())
55+
.commit();
56+
57+
SlidingMenu sm = getSlidingMenu();
58+
sm.setShadowDrawable(R.drawable.shadow, SlidingMenu.LEFT);
59+
sm.setBehindOffsetRes(R.dimen.actionbar_home_width, SlidingMenu.LEFT);
60+
}
61+
62+
private void addRight() {
63+
FrameLayout right = new FrameLayout(this);
64+
right.setId("RIGHT".hashCode());
65+
this.setBehindRightContentView(right);
66+
getSupportFragmentManager()
67+
.beginTransaction()
68+
.replace("RIGHT".hashCode(), new SampleListFragment())
69+
.commit();
70+
71+
SlidingMenu sm = getSlidingMenu();
72+
sm.setShadowDrawable(R.drawable.shadow_right, SlidingMenu.RIGHT);
73+
sm.setBehindOffsetRes(R.dimen.actionbar_home_width, SlidingMenu.RIGHT);
74+
}
75+
76+
// @Override
77+
// public void onResume() {
78+
// super.onResume();
79+
// getSlidingMenu().showAbove();
80+
// }
81+
5582
@Override
5683
public boolean onOptionsItemSelected(MenuItem item) {
5784
switch (item.getItemId()) {
@@ -61,7 +88,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
6188
}
6289
return super.onOptionsItemSelected(item);
6390
}
64-
91+
6592
public class PagerAdapter extends FragmentPagerAdapter {
6693
private List<Fragment> mFragments = new ArrayList<Fragment>();
6794
private ViewPager mPager;
@@ -70,6 +97,8 @@ public PagerAdapter(FragmentManager fm, ViewPager vp) {
7097
super(fm);
7198
mPager = vp;
7299
mPager.setAdapter(this);
100+
for (int i = 0; i < 3; i++)
101+
addTab(new SampleListFragment());
73102
}
74103

75104
public void addTab(Fragment frag) {
@@ -86,5 +115,5 @@ public int getCount() {
86115
return mFragments.size();
87116
}
88117
}
89-
118+
90119
}

example/src/com/slidingmenu/example/CustomAnimation.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public void onCreate(Bundle savedInstanceState) {
2727

2828
SlidingMenu sm = getSlidingMenu();
2929
setSlidingActionBarEnabled(true);
30-
sm.setBehindScrollScale(0.0f, SlidingMenu.LEFT | SlidingMenu.RIGHT);
31-
sm.setBehindCanvasTransformer(mTransformer, SlidingMenu.LEFT | SlidingMenu.RIGHT);
30+
sm.setBehindScrollScale(0.0f, SlidingMenu.BOTH);
31+
sm.setBehindCanvasTransformer(mTransformer, SlidingMenu.BOTH);
3232
}
3333

3434
@Override

example/src/com/slidingmenu/example/SlidingTitleBar.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.slidingmenu.example;
22

3+
import com.slidingmenu.lib.SlidingMenu;
4+
35
import android.os.Bundle;
6+
import android.support.v4.view.ViewPager;
47

58

69
public class SlidingTitleBar extends BaseActivity {
@@ -14,11 +17,17 @@ public void onCreate(Bundle savedInstanceState) {
1417
super.onCreate(savedInstanceState);
1518

1619
// set the Above View
17-
setContentView(R.layout.content_frame);
18-
getSupportFragmentManager()
19-
.beginTransaction()
20-
.replace(R.id.content_frame, new SampleListFragment())
21-
.commit();
20+
// setContentView(R.layout.content_frame);
21+
// getSupportFragmentManager()
22+
// .beginTransaction()
23+
// .replace(R.id.content_frame, new SampleListFragment())
24+
// .commit();
25+
26+
ViewPager vp = new ViewPager(this);
27+
vp.setId("VP".hashCode());
28+
vp.setAdapter(new PagerAdapter(getSupportFragmentManager(), vp));
29+
setContentView(vp);
30+
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
2231

2332
setSlidingActionBarEnabled(true);
2433
}

library/src/com/slidingmenu/lib/CustomViewAbove.java

+19-9
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import android.graphics.Canvas;
66
import android.graphics.Color;
77
import android.graphics.Paint;
8-
import android.graphics.Rect;
9-
import android.graphics.drawable.BitmapDrawable;
108
import android.graphics.drawable.Drawable;
119
import android.os.Build;
1210
import android.support.v4.view.KeyEventCompat;
@@ -97,7 +95,7 @@ public float getInterpolation(float t) {
9795
private int mFlingDistance;
9896

9997
private boolean mLastTouchAllowed = false;
100-
private final int mSlidingMenuThreshold = 10;
98+
private final int mSlidingMenuThreshold = 16;
10199
private CustomViewBehind mViewBehindLeft;
102100
private CustomViewBehind mViewBehindRight;
103101
private boolean mEnabled = true;
@@ -772,6 +770,22 @@ private boolean thisTouchAllowed(MotionEvent ev) {
772770
}
773771
}
774772
}
773+
774+
private boolean thisSlideAllowed(float dx) {
775+
boolean allowed = false;
776+
if (isLeftOpen()) {
777+
allowed = dx < 0;
778+
} else if (isRightOpen()) {
779+
allowed = dx > 0;
780+
} else {
781+
if (mViewBehindLeft != null)
782+
allowed |= dx > 0;
783+
if (mViewBehindRight != null)
784+
allowed |= dx < 0;
785+
}
786+
Log.v(TAG, "this slide allowed " + allowed);
787+
return allowed;
788+
}
775789

776790
private boolean mIsUnableToDrag;
777791

@@ -814,7 +828,7 @@ else if (mIsUnableToDrag)
814828
final float xDiff = Math.abs(dx);
815829
final float y = MotionEventCompat.getY(ev, pointerIndex);
816830
final float yDiff = Math.abs(y - mLastMotionY);
817-
if (xDiff > mTouchSlop && xDiff > yDiff) {
831+
if (xDiff > mTouchSlop && xDiff > yDiff && thisSlideAllowed(dx)) {
818832
if (DEBUG) Log.v(TAG, "Starting drag! from onInterceptTouch");
819833
mIsBeingDragged = true;
820834
mLastMotionX = x;
@@ -825,8 +839,7 @@ else if (mIsUnableToDrag)
825839
break;
826840

827841
case MotionEvent.ACTION_DOWN:
828-
mActivePointerId = ev.getAction() & ((Build.VERSION.SDK_INT >= 8) ? MotionEvent.ACTION_POINTER_INDEX_MASK :
829-
MotionEvent.ACTION_POINTER_ID_MASK);
842+
mActivePointerId = ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK;
830843
mLastMotionX = mInitialMotionX = MotionEventCompat.getX(ev, mActivePointerId);
831844
mLastMotionY = MotionEventCompat.getY(ev, mActivePointerId);
832845
if (thisTouchAllowed(ev)) {
@@ -936,7 +949,6 @@ public boolean onTouchEvent(MotionEvent ev) {
936949
velocityTracker, mActivePointerId);
937950
final int widthWithMargin = getChildWidth(mCurItem);
938951
final int scrollX = getScrollX();
939-
final int currentPage = scrollX / widthWithMargin;
940952
final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin;
941953
final int activePointerIndex =
942954
MotionEventCompat.findPointerIndex(ev, mActivePointerId);
@@ -1044,15 +1056,13 @@ public void scrollTo(int x, int y) {
10441056
private int determineTargetPage(int currentPage, float pageOffset, int velocity, int deltaX) {
10451057
int targetPage;
10461058
if (Math.abs(deltaX) > (float)mFlingDistance && Math.abs(velocity) > mMinimumVelocity) {
1047-
Log.v(TAG, "in here!");
10481059
targetPage = velocity > 0 ? currentPage - 1 : currentPage + 1;
10491060
} else {
10501061
targetPage = (int) ((deltaX > 0) ?
10511062
(currentPage + pageOffset - 0.5f) : (currentPage - pageOffset + 0.5f));
10521063
if ((deltaX > 0 && targetPage > currentPage) || (deltaX < 0 && targetPage < currentPage))
10531064
targetPage = currentPage;
10541065
}
1055-
if (DEBUG) Log.v(TAG, "targetPage : " + targetPage);
10561066
return targetPage;
10571067
}
10581068

0 commit comments

Comments
 (0)