Skip to content

Commit e7b36b0

Browse files
committed
Added ActionBar translation in DemoActivity
1 parent a90fa73 commit e7b36b0

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

demo/src/com/sothree/slidinguppanel/demo/DemoActivity.java

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package com.sothree.slidinguppanel.demo;
22

3+
import com.nineoldandroids.view.animation.AnimatorProxy;
34
import com.sothree.slidinguppanel.SlidingUpPanelLayout;
45
import com.sothree.slidinguppanel.SlidingUpPanelLayout.PanelSlideListener;
5-
66
import android.net.Uri;
7+
import android.os.Build;
78
import android.os.Bundle;
9+
import android.annotation.SuppressLint;
810
import android.app.Activity;
911
import android.content.Intent;
1012
import android.text.Html;
1113
import android.text.method.LinkMovementMethod;
1214
import android.util.Log;
15+
import android.util.TypedValue;
1316
import android.view.Menu;
1417
import android.view.View;
18+
import android.view.ViewGroup;
1519
import android.view.View.OnClickListener;
1620
import android.view.Window;
1721
import android.widget.Button;
@@ -28,20 +32,21 @@ protected void onCreate(Bundle savedInstanceState) {
2832
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
2933
setContentView(R.layout.activity_demo);
3034

31-
SlidingUpPanelLayout layout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
35+
final SlidingUpPanelLayout layout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
3236
layout.setPanelSlideListener(new PanelSlideListener() {
3337
@Override
3438
public void onPanelSlide(View panel, float slideOffset) {
3539
Log.i(TAG, "onPanelSlide, offset " + slideOffset);
36-
if (slideOffset < 0.2) {
37-
if (getActionBar().isShowing()) {
38-
getActionBar().hide();
39-
}
40-
} else {
41-
if (!getActionBar().isShowing()) {
42-
getActionBar().show();
43-
}
44-
}
40+
setActionBarTranslation(layout.getCurrentParalaxOffset());
41+
// if (slideOffset < 0.2) {
42+
// if (getActionBar().isShowing()) {
43+
// getActionBar().hide();
44+
// }
45+
// } else {
46+
// if (!getActionBar().isShowing()) {
47+
// getActionBar().show();
48+
// }
49+
// }
4550
}
4651

4752
@Override
@@ -108,4 +113,31 @@ public boolean onCreateOptionsMenu(Menu menu) {
108113
return true;
109114
}
110115

116+
@SuppressLint("NewApi")
117+
public void setActionBarTranslation(float y) {
118+
// Figure out the actionbar height
119+
int actionBarHeight = 0;
120+
TypedValue tv = new TypedValue();
121+
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
122+
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
123+
}
124+
// A hack to add the translation to the action bar
125+
ViewGroup content = ((ViewGroup) findViewById(android.R.id.content).getParent());
126+
int children = content.getChildCount();
127+
for (int i = 0; i < children; i++) {
128+
View child = content.getChildAt(i);
129+
if (child.getId() != android.R.id.content) {
130+
if (y <= -actionBarHeight) {
131+
child.setVisibility(View.GONE);
132+
} else {
133+
child.setVisibility(View.VISIBLE);
134+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
135+
child.setTranslationY(y);
136+
} else {
137+
AnimatorProxy.wrap(child).setTranslationY(y);
138+
}
139+
}
140+
}
141+
}
142+
}
111143
}

0 commit comments

Comments
 (0)