我想实现一个像FB或G + app这样的滑动菜单,我找到了一些来自FB Menu Demo和https://github.com/jfeinstein10/SlidingMenu的示例代码
这些都是好的开始,但我需要额外的东西。就像这里一样,它仅适用于单击菜单按钮,但我也希望通过手势移动它。 我希望有一个中心视图的行为,并且在向右移动该中心时,将出现一个视图,并且在向左移动时,菜单将出现。假设有三个视图A,B,C,当我向左滑动C然后出现A时,当我向右滑动C然后出现B. C位于A和B的中间。
1.中间视图向右移动
向右移动
2.将中间视图移向左侧
向左移动
现在我的问题是:开发类似观点的最佳做法是什么。我从某人那里听说我应该使用片段和View寻呼机。那么我该如何开发呢?是否有人做过任何样本实施?任何帮助和建议表示赞赏。
供参考,请参阅此应用使用此类型的滑动黑白视图Skout app
答案 0 :(得分:17)
最简单的解决方案可能是使用内置边框滑动的android-undergarment,基于项目自述文件:
用户还可以通过从屏幕左侧滑动的挡板控制抽屉打开抽屉,并从右侧执行相同操作以关闭抽屉。如果要阻止此触摸功能,可以调用setDrawerEnabled(false)。
答案 1 :(得分:12)
您只需在要移动的视图上使用TranslateAnimation
,然后弹出淡入淡出窗口,另一个弹出窗口显示您的菜单。我已经在我的应用程序中实现了它,它就像一个魅力。
的代码强>:
public class SlidingOptionsMenuActivity extends Activity {
/**
* Signifies that the menu is already visible
*/
boolean alreadyShowing = false;
/**
* Width of the current window
*/
private int windowWidth;
/**
* Height of the current window
*/
private int windowHeight;
/**
* Reference of the {@link PopupWindow} which dims the screen
*/
private PopupWindow fadePopup;
/**
* The translate animation
*/
private Animation ta;
/**
* The view which needs to be translated
*/
private RelativeLayout baseView;
/**
* Reference of the {@link LayoutInflater}
*/
LayoutInflater inflater;
/*
* (non-Javadoc)
*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Display display = getWindowManager().getDefaultDisplay();
windowWidth = display.getWidth();
windowHeight = display.getHeight();
inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
findViewById(R.id.btnOptions).setOnClickListener(new OnClickListener() {
/*
* (non-Javadoc)
*
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
if(!alreadyShowing){
alreadyShowing = true;
openSlidingMenu();
}
}
});
}
/**
* Fades the entire screen, gives a dim background
*/
private void showFadePopup() {
final View layout = inflater.inflate(R.layout.fadepopup, (ViewGroup) findViewById(R.id.fadePopup));
fadePopup = new PopupWindow(layout, windowWidth, windowHeight, false);
fadePopup.showAtLocation(layout, Gravity.NO_GRAVITY, 0, 0);
}
/**
* Opens the sliding Menu
*/
private void openSlidingMenu() {
showFadePopup();
// The amount of view which needs to be moved out. equivalent to the
// width of the menu
int width = (int) (windowWidth * 0.6f);
translateView((float) (width));
int height = LayoutParams.FILL_PARENT;
// creating a popup
final View layout = inflater.inflate(R.layout.option_popup_layout,(ViewGroup) findViewById(R.id.popup_element));
final PopupWindow optionsPopup = new PopupWindow(layout, width, height, true);
optionsPopup.setBackgroundDrawable(new PaintDrawable());
optionsPopup.showAtLocation(layout, Gravity.NO_GRAVITY, 0, 0);
optionsPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
public void onDismiss() {
//Removing the fade effect
fadePopup.dismiss();
//to clear the previous animation transition in
cleanUp();
//move the view out
translateView(0);
//to clear the latest animation transition out
cleanUp();
//resetting the variable
alreadyShowing = false;
}
});
}
/**
* This method is responsible for view translation. It applies a translation
* animation on the root view of the activity
*
* @param right The position to translate to
*/
private void translateView(float right) {
ta = new TranslateAnimation(0f, right, 0f, 0f);
ta.setDuration(300);
ta.setFillEnabled(true);
ta.setFillAfter(true);
baseView = (RelativeLayout) findViewById(R.id.baseView);
baseView.startAnimation(ta);
baseView.setVisibility(View.VISIBLE);
}
/**
* Basic cleanup to avoid memory issues. Not everything is release after
* animation, so to immediately release it doing it manually
*/
private void cleanUp(){
if (null != baseView) {
baseView.clearAnimation();
baseView = null;
}
if (null != ta) {
ta.cancel();
ta = null;
}
fadePopup = null;
}
} //END of Class
//END of file
希望这会有所帮助。
答案 2 :(得分:10)
另一个我发现非常好的开源库SlidingMenu。它应该适合您的需要,因为您可以通过“菜单”单击和挡板滑动来打开和关闭抽屉。我发现将它与像ActionBarSherlock或johannilsson's android-actionbar库这样的Actionbar库集成只需更改库项目中的一行或两行代码。 SlidingMenu库的自述文件解释了如何与ABSherlock库集成。
值得注意的是,SlidingMenu示例项目演示了许多不同的抽屉打开关闭动画。这些是我在这种菜单/导航方式中看到的一些最好的动画。
答案 3 :(得分:-1)
有一种官方方式......有用且轻便(使用v4支持库):
https://developer.android.com/training/implementing-navigation/nav-drawer.html