Search code examples
androidhorizontalscrollview

horizontalscrollview gravity


How to make horizontalscrollview that move relatively object that is closer to the screen center? Like in the picture:

enter image description here


Solution

  • I find solution in the news-parser example. My code:

    ....

    gestureDetector = new GestureDetector(new MyGestureDetector());
            hs = (HorizontalScrollView) findViewById(R.id.hs);
            hs.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (gestureDetector.onTouchEvent(event)) {
                        return true;
                    }
                    if (event.getAction() == 1)
                    {
                        if (hs.getScrollX() > mWidth * 8 + mWidth / 2)
                        {
                            hs.smoothScrollBy(mWidth * 9 - hs.getScrollX(), 0);
                        }
                        else if (hs.getScrollX() > mWidth * (cN - 2)  + mWidth / 2)
                        {
                            hs.smoothScrollBy(mWidth * (cN - 1) - hs.getScrollX(), 0);
                        }
                        else if (hs.getScrollX() > mWidth / 2)
                        {
                            hs.smoothScrollBy(mWidth - hs.getScrollX(), 0);
                        }
                        else
                        {
                            hs.smoothScrollBy(-hs.getScrollX(), 0);
                        }
                        return true;
                    }
                    return false;
                }
            });
        }
    
    
        class MyGestureDetector extends SimpleOnGestureListener {
    
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                    float velocityY) {
                //Log.v("e1.getX()", Float.toString(e1.getX()));
                //Log.v("e2.getX()", Float.toString(e2.getX()));
                //Log.v("velocityX", Float.toString(velocityX));
                if (velocityX > 0) {
                    moveLeft();
                } else {
                    moveRight();
                }
                return true;
            }
        }
    
        private void moveRight() {
            if (hs.getScrollX() > 0 && hs.getScrollX() < mWidth * cN)
            {
                hs.smoothScrollBy(mWidth * cN - hs.getScrollX(), 0);
                cN++;
            }
            //Log.v("cN", Integer.toString(cN));
        }
    
        private void moveLeft() {
    
            if (hs.getScrollX() > 0 && hs.getScrollX() < mWidth)
            {
                hs.smoothScrollBy( -hs.getScrollX(), 0);
                cN = 1;
            }
            else if (hs.getScrollX() > mWidth && hs.getScrollX() < mWidth * (cN - 1) )
            {
                hs.smoothScrollBy(mWidth * (cN - 2) - hs.getScrollX(), 0);
                cN--;
            }
            //Log.v("cN", Integer.toString(cN));
        }