我正在尝试实现滑动菜单,就像facebook的android应用程序一样。
我已经成功地滑动菜单并将其重新滑动回来。
我有两个适合框架布局的视图。
我向右滑动X,在滑动时显示Y,在向后滑动时,我将X移回0,这会隐藏菜单。
@Override
public void setContentView(int layout) {
FrameLayout frame = new FrameLayout(getBaseContext());
frame.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT));
LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mainLayout = inflator.inflate(layout, null);
menuList = inflator.inflate(R.layout.menu_page, null);
fakeView= inflator.inflate(R.layout.fake_transparent_view, null);
menuAnimator = new MenuAnimation(mainLayout,menuList,fakeView);
frame.addView(menuList);
frame.addView(mainLayout);
frame.addView(fakeView);
fakeView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mainLayout.getLeft()>0)
{
menuAnimator.moveMenu("left");
}
}
});
super.setContentView(frame);
}
public void moveMenu(String direction) {
int width=(int) (QuikrApplication.getWidth()*.85);
if (direction.equals("right")) {
this.direction="right";
fakeView.setVisibility(View.VISIBLE);
TranslateAnimation moveRight = new TranslateAnimation(0, width, 0, 0);
moveRight.setDuration(500);
parentLayout.setAnimation(moveRight);
moveRight.setAnimationListener(animationListner);
parentLayout.startAnimation(moveRight);
fakeView.startAnimation(moveRight);
}
else if (direction.equals("left")){
this.direction="left";
TranslateAnimation moveLeft = new TranslateAnimation(0, -width, 0, 0);
moveLeft.setDuration(500);
parentLayout.setAnimation(moveLeft);
moveLeft.setAnimationListener(animationListner);
parentLayout.startAnimation(moveLeft);
fakeView.startAnimation(moveLeft);
}
}现在的问题是,当我在主视图中异步地执行一些操作时,主视图是重新定位的(当菜单可见时)。
发布于 2013-02-02 18:00:47
尝尝这个,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/menu_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/black"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/fake_layouy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone" >
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="inner button" />
</FrameLayout>
<RelativeLayout
android:id="@+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="menu" />
</RelativeLayout>
java代码
public class MainActivity extends Activity {
private View toplayout;
private View sublayout;
private View fakeLayout;
private int screenWidth;
private int animToPosition;
private boolean menuOpen = false;
private int oldLeft;
private int oldTop;
private int newleft;
private int newTop;
private Button button1;
private Button button2;
private AnimationListener AL;
private DisplayMetrics metrics;
private Display display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_ugsimply_beta);
toplayout = (View) findViewById(R.id.main_layout);
sublayout = (View) findViewById(R.id.menu_layout);
fakeLayout = (View) findViewById(R.id.fake_layouy);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
metrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
int calcAnimatePosition = (screenWidth / 4);
animToPosition = screenWidth - calcAnimatePosition;
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
animToPosition, RelativeLayout.LayoutParams.FILL_PARENT);
sublayout.setLayoutParams(parms);
/** Animatio Listner */
AL = new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
button2.setClickable(false);
toplayout.setEnabled(false);
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
if (menuOpen) {
toplayout.layout(oldLeft, oldTop,
oldLeft + toplayout.getMeasuredWidth(), oldTop
+ toplayout.getMeasuredHeight());
menuOpen = false;
// sublayout.setEnabled(false);
button2.setClickable(true);
toplayout.setEnabled(true);
} else if (!menuOpen) {
toplayout.layout(newleft, newTop,
newleft + toplayout.getMeasuredWidth(), newTop
+ toplayout.getMeasuredHeight());
button2.setClickable(true);
menuOpen = true;
toplayout.setEnabled(true);
}
}
};
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!menuOpen) {
animSlideRight();
}
}
});
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (menuOpen) {
animSlideLeft();
}
}
});
}
/** Animation right */
private void animSlideRight() {
fakeLayout.setVisibility(View.VISIBLE);
newleft = toplayout.getLeft() + animToPosition;
newTop = toplayout.getTop();
TranslateAnimation slideRight = new TranslateAnimation(0, newleft, 0, 0);
slideRight.setDuration(500);
slideRight.setFillEnabled(true);
slideRight.setAnimationListener(AL);
toplayout.startAnimation(slideRight);
}
/** Animation left */
private void animSlideLeft() {
// TODO Auto-generated method stub
fakeLayout.setVisibility(View.GONE);
oldLeft = toplayout.getLeft() - animToPosition;
oldTop = toplayout.getTop();
TranslateAnimation slideLeft = new TranslateAnimation(newleft, oldLeft,
0, 0);
slideLeft.setDuration(500);
slideLeft.setFillEnabled(true);
slideLeft.setAnimationListener(AL);
toplayout.setAnimation(slideLeft);
}}
当单击菜单按钮时,它会向右滑动动画。并点击内部按钮,它来动画左。检查屏幕下方


发布于 2013-02-03 18:50:35
除了LibSlideMenu之外,github上还有另一个带有280+提交的项目,现在我已经在几个项目中使用过了,非常容易实现,有很多特性,你也可以试试。
发布于 2013-02-02 17:12:54
如果我可以推荐一些东西,在github上有一个非常好的facebook/google+样式滑块菜单的库实现:
https://github.com/bk138/LibSlideMenu
我已经在一两个项目中使用过它,它工作得很好。你应该试一试。
https://stackoverflow.com/questions/14660328
复制相似问题