【问题标题】:Horizontal Menu Slide Out ScrollTo Not Being called水平菜单滑出 ScrollTo 未被调用
【发布时间】:2025-12-06 11:05:01
【问题描述】:

所以我正在尝试破解臭名昭著的滑出式菜单,例如在 G+ 和 Youtube 中。 由于这个原因,我设置了一个 ActionBar UP 按钮,我想用它来打开侧边菜单。 我几乎把所有东西都布置好了,但是当我问的时候我的 Horizo​​ntalScrollView 没有滑动。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <include
        android:layout_width="wrap_content"
        layout="@layout/side_menu" />

    <HorizontalScrollView
        android:id="@+id/menu_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="horizontal" >

    <include
        layout="@layout/main_content" />

    </HorizontalScrollView>
</FrameLayout>


private void toggleSideMenu() {
    mMenuScrollView.postDelayed(new Runnable() {

        @Override
        public void run() {
            int menuWidth = mSideMenu.getMeasuredWidth();
            if (!mIsMenuVisible) {
                // Scroll to 0 to reveal menu
                int left = 0;
                mScrollView.smoothScrollTo(left, 0);
            } else {
                // Scroll to menuWidth so menu isn't on screen.
                int left = menuWidth;
                mScrollView.smoothScrollTo(left, 0);
            }
            mIsMenuVisible = !mIsMenuVisible;

        }
    }, 50);

}

我对 smoothScroll 的调用似乎不起作用。

【问题讨论】:

    标签: android android-actionbar android-scrollview android-sliding


    【解决方案1】:

    我对示例进行了一些调整,并最终使其正常工作。比我在那里看到的其他例子要简单得多。 这可以正常工作,但是我没有 MenuSliding 的平滑动画。而且我不需要Horizo​​ntalScrollView。作为副作用,用户将不得不点击按钮来弹出菜单。这将没有幻灯片功能。

    但是,这是一个让事情顺利进行的 BARE BONES 示例。尽情享受吧!

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <!-- Base layout has id/side_menu -->
        <include layout="@layout/side_menu" />
    
    
        <!-- Base layout has id/content -->
        <include layout="@layout/content" />
    
    
    </FrameLayout>
    

    这是我的活动

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.main);
    
        mContentView = findViewById(R.id.content);
        mSideMenu = findViewById(R.id.side_menu);
        mIsMenuVisible = false;
    }
    
    private void toggleSideMenu() {
        if (mIsMenuVisible) {
            mSideMenu.setVisibility(View.INVISIBLE);
            mContent.scrollTo(0, 0);
        } else {
            int menuWidth = mSideMenu.getMeasuredWidth() * -1;
            mSideMenu.setVisibility(View.VISIBLE);
            mContentView.scrollTo(menuWidth, 0);
        }
        mIsMenuVisible = !mIsMenuVisible;
    }
    

    【讨论】: