0

我有一个我没有找到任何问题的问题。我修改了 Horizo​​ntalScrollLayout 以逐页滚动。对于一个简短的问题,这一切都很好。如果我滚动得很慢,会调用 smoothScrollTo 函数(您可以通过它进行调试),但不起作用,ScrollLayout 会停留在两个页面之间。如果我将其更改为 scrollTo 函数,它可以工作,但看起来很糟糕。我使用 HTC One 来检查功能。有谁知道这个问题?可能是 HTC 的问题,还是其他平台上也存在?是否有另一种聪明的方式来实现该功能?这是我的代码:

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class FullscreenActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_fullscreen);

    final HorizontalScrollView scr = new HorizontalScrollView(this){

        int scrollXBegin = 0;

        private void setScrollXBegin(int beg){
            this.scrollXBegin = beg;
        }

        @Override
        public boolean onTouchEvent(MotionEvent ev){
            switch (ev.getActionMasked()){
            case MotionEvent.ACTION_OUTSIDE:
            case MotionEvent.ACTION_UP:
                this.scrollReady(ev);
                break;
            case MotionEvent.ACTION_DOWN:
                this.setScrollXBegin(this.getScrollX());
                break;
            default:
                break;

            }
            return super.onTouchEvent(ev);
        }

        @Override
        public void fling (int velocityY)
        {
        /*Scroll view is no longer gonna handle scroll velocity.
        * super.fling(velocityY);
         */
        }

        protected boolean scrollReady(MotionEvent ev){

            int actXPos = (int)this.getScrollX();
            int motionDirection = actXPos - this.scrollXBegin;

            int i = 0;
            int sz_act = 0;
            int sz_old = 0;
            for(i = 0; i < ((LinearLayout)this.getChildAt(0)).getChildCount(); i++){
                sz_old = sz_act;
                sz_act += ((LinearLayout)this.getChildAt(0)).getChildAt(i).getWidth();
                if(sz_act > actXPos)
                    break;
            }

            // Fertig scrollen auf den naechsten Child
            if (motionDirection >= 0)
                this.smoothScrollTo(sz_act, this.getScrollY());
            else
                this.smoothScrollTo(sz_old, this.getScrollY());

            computeScroll();

            return true;
        }

    };

    final LinearLayout scrollLayout = new LinearLayout(this);
    scrollLayout.setOrientation(LinearLayout.HORIZONTAL);
    scr.addView(scrollLayout);
    scr.setSmoothScrollingEnabled(true);

    LayoutParams imLP = new LayoutParams();
    imLP.height = 1024;
    imLP.width = 1024;
    ImageView im1 = new ImageView(this);
    im1.setImageResource(R.drawable.ic_launcher);
    im1.setLayoutParams(imLP);
    scrollLayout.addView(im1);
    ImageView im2 = new ImageView(this);
    im2.setImageResource(R.drawable.ic_launcher_r);
    im2.setLayoutParams(imLP);
    scrollLayout.addView(im2);
    im1 = new ImageView(this);
    im1.setImageResource(R.drawable.ic_launcher_b);
    im1.setLayoutParams(imLP);
    scrollLayout.addView(im1);

    ((FrameLayout)findViewById(R.id.parent_layout)).addView(scr);   
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

    // Trigger the initial hide() shortly after the activity has been
    // created, to briefly hint to the user that UI controls
    // are available.

}

}`

这是要测试的完整 Eclipse 项目。

https://www.dropbox.com/sh/m2dedsa3w3avgec/CdTX8cFJhk/TestVerticalScroll.rar

4

0 回答 0