2013-02-02 29 views
0

我想实现滑块菜单就像Facebook的Android应用了。安卓滑盖执行

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

我有两个视图适合framelayout。
1.主要laout(X)
2.菜单布局。(Y)

我滑动X到右示出上滑动和上滑回我移动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); 
     } 
    } 

现在的问题是,当我做在主视图中的某些动作异步主要观点是重新定位(当菜单可见)

+0

为什么你需要自己的实现,如果已经有几个好的? – Evos

回答

0

如果我可以建议的东西,有一个很好的库实现Facebook的/谷歌+风格的滑盖菜单在GitHub上:

https://github.com/bk138/LibSlideMenu

我已经用它的一个或两个项目,它工作得很好。你应该尝试一下。

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> 

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); 

} 

}

同时点击菜单按钮,它会向右滑动动画。然后点击innner按钮来动画。检查屏幕下方

enter image description here enter image description here

1

除了LibSlideMenu也有在github另一个项目超过280+人提交,我已经使用,以及一对夫妇的项目,很容易与一吨的实现功能,你可以检查出来。

https://github.com/jfeinstein10/SlidingMenu