如何在滑动时显示和隐藏视图

时间:2014-03-17 10:43:29

标签: android

我有一个封面流,下面是我有相机,当用户向上滑动封面流时我想要隐藏它并在全屏显示相机,当他向下滑动时我想显示40%的封面流量屏幕和相机在剩下的部分我怎么能实现这一点 任何帮助表示赞赏

1 个答案:

答案 0 :(得分:1)

最后,我使用此库中的代码完成了它 https://github.com/gunasiet/SlidingMenuExample

它有像facebook类型的滑出菜单,水平滑出并显示菜单项我希望它以coverflow替换菜单的方式

我在菜单布局上按changing x,y coordinates操纵了代码,该代码用于水平滑出,并在用户滑动时从上到下垂直滑动

protected void onLayout(boolean changed, int left, int top, int right, int bottom)
    {
        // System.out.println(" ~~~onLayout changed="+changed);
        if (changed)
        {
            //
            LayoutParams contentLayoutParams = (LayoutParams) content.getLayoutParams();
            contentLayoutParams.height = this.getHeight();
            contentLayoutParams.width = this.getWidth();
            LayoutParams menuLayoutParams = (LayoutParams) menu.getLayoutParams();
            // menuLayoutParams.height = this.getHeight();
            // menuLayoutParams.width = this.getWidth() - menuRightMargin;
            menuLayoutParams.height = this.getHeight() - menuRightMargin;
            menuLayoutParams.width = this.getWidth();
        }
        // menu.layout(left, top, right- menuRightMargin , bottom );
        // content.layout(left + contentXOffset, top, right +
        // contentXOffset,bottom);

        menu.layout(left, top, right, bottom - menuRightMargin);
        content.layout(left, top + contentXOffset, right, bottom + contentXOffset);
        if (changed)
            if(!CameraFragment.isCoverFlowClosed)
                toggleMenu();
}

public void toggleMenu()
    {

        if (currentMenuState == MenuState.HIDING || currentMenuState == MenuState.SHOWING)
            return;

        // System.out.println(" ~~~toggleMenu currentMenuState="+currentMenuState);
        switch (currentMenuState)
        {
        case HIDDEN:
            currentMenuState = MenuState.SHOWING;
            menu.setVisibility(View.VISIBLE);
            //slideIcon.setBackground(context.getResources().getDrawable(R.drawable.down_arrow));
            //slideIcon.setBackgroundResource(R.drawable.down_arrow);
            /*
             * menuScroller.startScroll(0, 0, 0, menu.getLayoutParams().height,
             * SLIDING_DURATION);
             */
            menuScroller.startScroll(0, contentXOffset, 0, menu.getLayoutParams().height - contentXOffset, SLIDING_DURATION);
            break;
        case SHOWN:
            currentMenuState = MenuState.HIDING;
            menuScroller.startScroll(0, contentXOffset, 0, -contentXOffset, SLIDING_DURATION);
            //slideIcon.setBackground(context.getResources().getDrawable(R.drawable.up_arrow));
            //slideIcon.setBackgroundResource(R.drawable.up_arrow);
            break;
        default:
            break;
        }
        menuHandler.postDelayed(menuRunnable, QUERY_INTERVAL);
        this.invalidate();
    }

private void adjustContentPosition(boolean isScrolling)
    {
        int scrollerXOffset = menuScroller.getCurrY();

        // System.out.println("scrollerXOffset=="+scrollerXOffset+" contentXOffset="+contentXOffset
        // +" scrollerXOffset - contentXOffset="+(scrollerXOffset -
        // contentXOffset));
        // content.offsetLeftAndRight(scrollerXOffset - contentXOffset);
        content.offsetTopAndBottom(scrollerXOffset - contentXOffset);
        contentXOffset = scrollerXOffset;
        this.invalidate();
        fancyCoverFlow.bringToFront();
        if (isScrolling)
            menuHandler.postDelayed(menuRunnable, QUERY_INTERVAL);
        else
            this.onMenuSlidingComplete();
    }

public boolean onContentTouch(View v, MotionEvent event)
    {
        if (currentMenuState == MenuState.HIDING || currentMenuState == MenuState.SHOWING)
            return false;
        int curX = (int) event.getRawY();
        int diffX = 0;

        switch (event.getAction())
        {
        case MotionEvent.ACTION_DOWN:

            // System.out.println("~~~ ACTION_DOWN ~~~ curX="+curX);
            prevX = curX;
            return true;

        case MotionEvent.ACTION_MOVE:
            if (!isDragging)
            {
                // System.out.println("~~ currentMenuState=="+currentMenuState+" isDragging=="+isDragging);
                isDragging = true;
                menu.setVisibility(View.VISIBLE);

            }
            diffX = curX - prevX;
            // System.out.println("~~~ ACTION_MOVE ~~~ curX=="+curX+" prevX=="+prevX+" contentXOffset="+contentXOffset);
            if (contentXOffset + diffX <= 0)
            {
                diffX = -contentXOffset;
            } else if (contentXOffset + diffX > mainLayoutWidth - menuRightMargin)
            {
                diffX = mainLayoutWidth - menuRightMargin - contentXOffset;
            }
            // content.offsetLeftAndRight(diffX);
            content.offsetTopAndBottom(diffX);
            fancyCoverFlow.bringToFront();
            contentXOffset += diffX;
            this.invalidate();

            prevX = curX;
            lastDiffX = diffX;
            // System.out.println("~~~ ACTION_MOVE ~~~ contentXOffset="+contentXOffset+" diffX="+diffX);
            return true;

        case MotionEvent.ACTION_UP:
            Log.d("MainLayout.java onContentTouch()", "Up lastDiffX " + lastDiffX);

            if (lastDiffX == 0)
            {
                toggleMenu();
                isDragging = false;
                prevX = 0;
                lastDiffX = 0;
            }
            if (lastDiffX > 0)
            {
                currentMenuState = MenuState.SHOWING;
                // menuScroller.startScroll(contentXOffset, 0,
                // menu.getLayoutParams().width - contentXOffset, 0,
                // SLIDING_DURATION);
                menuScroller.startScroll(0, contentXOffset, 0, menu.getLayoutParams().height - contentXOffset, SLIDING_DURATION);
            } else if (lastDiffX < 0)
            {
                currentMenuState = MenuState.HIDING;
                menuScroller.startScroll(0, contentXOffset, 0, -contentXOffset, SLIDING_DURATION);
            }
            menuHandler.postDelayed(menuRunnable, QUERY_INTERVAL);
            this.invalidate();
            isDragging = false;
            prevX = 0;
            lastDiffX = 0;
            // System.out.println("~~~  ACTION_UP ~~~");
            return true;

        default:
            break;
        }

        return false;
    }