0

我正在尝试像 facebook android 应用程序一样实现滑块菜单。

我已成功滑动菜单并将其重新滑回。

我有两个适合框架布局的视图。
1. 主布局 (X)
2. 菜单布局。(Y)

我将 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);
        }
    }

现在的问题是当我在主视图中异步执行一些操作时,主视图是重新定位(当菜单可见时)

4

3 回答 3

1

除了 LibSlideMenu 之外,github 上还有另一个项目,现在我已经在几个非常容易实现的项目上使用了超过 280 多个提交,您也可以查看它。

https://github.com/jfeinstein10/SlidingMenu

于 2013-02-03T10:50:35.423 回答
1

尝试这个,

 <?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>

爪哇代码

   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-02T10:00:47.170 回答
0

如果我可以提出一些建议,在 github 上有一个非常好的用于 facebook/google+ 样式滑块菜单的库实现:

https://github.com/bk138/LibSlideMenu

我用过一两个项目,效果很好。你应该试试看。

于 2013-02-02T09:12:54.300 回答