質問

I'm not sure I'm approaching this problem the right way and was hoping to get some advice from the friendly SO community. Basically, I want to get an effect like a sliding menu coming out from the left-side of the screen, but I want both the menu and the main screen to share the same background. So when the menu comes out, it just pushes the main content to the right, but they share the same background.

I've tried using a ViewPager, putting the menu in position 0 and the main content in position 1. It looks fine except for I don't know how to stop the ViewPager from either snapping back to its original position or completely going to the next page. I'd like it stop roughly half way. I tried overriding ViewPager's onTouch() method, and I can recognize when the view has been dragged half way, but I don't know how to lock it into place.

Any ideas?

役に立ちましたか?

解決

After some digging, I figured out one way to do this. For your pager adapter, extend one of them. In my case, I extended FragmentStatePagerAdapter. Then, overwrite it's getPageWidth() method and explicitly set the width for each page.

/**
 * This method should return a percentage of the measured screen width, booya
 *
 * @param position
 * @return
 */
@Override
public float getPageWidth(int position)
{
    switch (position)
    {
        case 0:
            return .33f;
        case 1:
            return 1;
        case 2:
            return .8f;
        default:
            return 1;
    }
}

So in this example, there's 3 pages. The page on the left will be 1/3 of the screen, the page in the middle will be full screen, and the page on the right will be 4/5 of the screen. This gives the effect of a sliding menu, but with a shared background.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top