Android 在安卓系统中,通过触摸,菜单可以从一个视图切换到另一个视图

Android 在安卓系统中,通过触摸,菜单可以从一个视图切换到另一个视图,android,android-layout,android-fragments,android-viewpager,slidingdrawer,Android,Android Layout,Android Fragments,Android Viewpager,Slidingdrawer,我想实现一个滑动菜单,比如FB或G+应用程序,我从和中找到了一些示例代码 这些是好的开始,但我需要一些额外的东西从他们。就像这里一样,它只在点击菜单按钮时起作用,但我也想通过手势来移动它。 我希望有一个中心视图,当中心向右移动时,会出现一个视图,当中心向左移动时,会出现菜单。假设有三个视图A、B、C,当我向左滑动C时,A出现;当我向右滑动C时,B出现。C位于A和B的中间 1.中间视图向右移动 向右移动 2.将中间视图移向左侧 向左转 现在我的问题是:开发这样的视图的最佳实践是什么。我从某人那里听

我想实现一个滑动菜单,比如FB或G+应用程序,我从和中找到了一些示例代码

这些是好的开始,但我需要一些额外的东西从他们。就像这里一样,它只在点击菜单按钮时起作用,但我也想通过手势来移动它。 我希望有一个中心视图,当中心向右移动时,会出现一个视图,当中心向左移动时,会出现菜单。假设有三个视图A、B、C,当我向左滑动C时,A出现;当我向右滑动C时,B出现。C位于A和B的中间

1.中间视图向右移动

向右移动

2.将中间视图移向左侧

向左转

现在我的问题是:开发这样的视图的最佳实践是什么。我从某人那里听说我应该使用片段和查看寻呼机。那么我如何开发这个呢?有没有人做过任何示例实现?感谢您的帮助和建议


有关参考信息,请参阅此应用程序,该应用程序使用这种类型的滑动黑白视图。最简单的解决方案可能是根据项目自述使用内置挡板滑动:

用户还可以通过从屏幕左侧滑动挡板来打开抽屉,然后从右侧进行同样的操作来关闭抽屉,从而控制抽屉。如果要阻止此触摸功能,可以调用setDrawerEnabled(false)


我发现另一个非常好的开源库是。它应该适合您的需要,因为您可以通过“菜单”点击和面板滑动来打开和关闭抽屉。我发现将其与Actionbar库(如or库)集成只需更改库项目中的一两行代码即可。滑动菜单库的自述文件解释了如何与ABSherlock库集成


值得注意的是,SlidingMenu示例项目演示了许多不同的抽屉打开-关闭动画。这些是我见过的这种菜单/导航风格的一些最好的动画。

您只需在希望移动的视图上使用
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
希望这会有所帮助


有一种官方的方式。。。有用且轻便(通过使用v4支持库):

创建导航抽屉:

谢谢您的回复。我必须把它用作一个库项目。我想做的是有三个视图a,B,C。当我把C视图移到左边时,a会出现,如果我把C移到右边,B会出现。到目前为止,我所发现的只是两种观点。所以,请引导这是一个完美的解决方案。。。简单oneNOTE:不推荐使用内衣。请使用v4支持库中的官方抽屉布局实现。你应该读,这本书的作者GreenDroid@shkschneider谢谢你的建议好吧,想一想,把你的左视图和右视图加在一起。在它们上面,添加一个包含3个项目的ViewPager,中间一个是您的中心视图(其他为空)。一旦用户向左/向右滑动,根据需要切换
visibility
s,我猜,您想要的视图就会出现。滑动菜单是上述要求的核心android API。请看,我必须像Skout应用程序一样实现。通过手势和单击按钮,可以左右移动视图。你能给我演示一下吗?我会创建一个演示,然后再给你回复。@Naresh:如果你在这方面有问题,请告诉我。欢迎提供解决方案的链接,但请确保你的答案在没有它的情况下是有用的:这样你的其他用户就会知道它是什么以及它为什么存在,然后引用你链接到的页面最相关的部分,以防目标页面不可用。