调用Horizo​​ntalScrollLayout中的平滑滚动,但不起作用

时间:2013-10-14 11:41:37

标签: android scrollview

我有一个问题,我没有找到任何东西。我修改了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

0 个答案:

没有答案