سؤال

So I'm trying my crack at the infamous slide out menu, like in G+ and Youtube. In this cause I'm setting an ActionBar UP button that I want to use to open the Side Menu. I have most everything laid out correctly, but my HorizontalScrollView is not sliding when I ask.

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

}

My call to smoothScroll doesn't seem to be working.

هل كانت مفيدة؟

المحلول

I tweaked the example some and ended up getting it working. Much more barebones than the other examples I have seen out there. This works correctly, however I do not have a smooth animation for the MenuSliding. And I didn't need the HorizontalScrollView. As a side effect, users will have to hit the button to pop the menu in and out. This will not have the slide feature.

However, it is a BARE BONES example to get things rolling. Enjoy!

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

Here is my Activity

@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;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top