水平ScrollView图库。滚动两个方向或从头开始

时间:2014-09-02 13:13:33

标签: android scrollview

我正在制作水平滚动视图库,我想自动滚动它。现在它从左向右滚动,但当我到达列表末尾时,我只是跳到第一个,但它看起来非常糟糕,所以我想从开始滚动,避免只跳到第一个,或者如果当我到达右边的最后一个视图(也许是更好的选项)时,不可能只是开始滚动到另一边。有人可以帮我怎么做吗?

private LinearLayout horizontalOuterLayout;
private HorizontalScrollView horizontalScrollview;
private int scrollMax;
private int scrollPos = 0;
private TimerTask clickSchedule;
private TimerTask scrollerSchedule;
private TimerTask faceAnimationSchedule;
private Timer scrollTimer = null;
private Timer faceTimer = null;
private String[] imageNameArray ={ "sponsors_czarnykot", "sponsors_estradarzeszow","sponsors_klubp","sponsors_kula","sponsors_czarnykot", "sponsors_estradarzeszow","sponsors_klubp","sponsors_kula" };


@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.horizontal_layout);
    horizontalScrollview = (HorizontalScrollView) findViewById(R.id.horiztonal_scrollview_id);
    horizontalOuterLayout = (LinearLayout) findViewById(R.id.horiztonal_outer_layout_id);       

    horizontalScrollview.setHorizontalScrollBarEnabled(false);
    addImagesToView();

    ViewTreeObserver vto = horizontalOuterLayout.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
    {
        @Override
        public void onGlobalLayout()
        {
            horizontalOuterLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            getScrollMaxAmount();
            startAutoScrolling();
        }
    });
}

public void getScrollMaxAmount()
{
    int actualWidth = (horizontalOuterLayout.getMeasuredWidth() - 512);
    scrollMax = actualWidth;
}

public void startAutoScrolling()
{
    if (scrollTimer == null)
    {
        scrollTimer = new Timer();
        final Runnable Timer_Tick = new Runnable()
        {
            public void run()
            {
                moveScrollViewRight();
            }
        };

        if (scrollerSchedule != null)
        {
            scrollerSchedule.cancel();
            scrollerSchedule = null;
        }
        scrollerSchedule = new TimerTask()
        {
            @Override
            public void run()
            {
                runOnUiThread(Timer_Tick);
            }
        };
        scrollTimer.schedule(scrollerSchedule, 30, 30);
    }
}

public void moveScrollViewRight()
{
    scrollPos = (int) (horizontalScrollview.getScrollX() + 1.0);
    if (scrollPos >= scrollMax)
    {
        scrollPos = 0;
    }
    horizontalScrollview.scrollTo(scrollPos, 0);
}

/** Adds the images to view. */
public void addImagesToView()
{
    for (int i = 0; i < imageNameArray.length; i++)
    {
        final ImageView imageView = new ImageView(this);
        int imageResourceId = getResources().getIdentifier(imageNameArray[i], "drawable", getPackageName());
        Drawable image = this.getResources().getDrawable(imageResourceId);
        imageView.setBackgroundDrawable(image);
        imageView.setTag(i);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(180, 123);
        params.setMargins(0, 25, 0, 25);
        imageView.setLayoutParams(params);
        horizontalOuterLayout.addView(imageView);
    }
}
public void stopAutoScrolling()
{
    if (scrollTimer != null)
    {
        scrollTimer.cancel();
        scrollTimer = null;
    }
}

public void onBackPressed()
{
    super.onBackPressed();
    finish();
}

public void onPause()
{
    super.onPause();
    finish();
}

public void onDestroy()
{
    clearTimerTaks(clickSchedule);
    clearTimerTaks(scrollerSchedule);
    clearTimerTaks(faceAnimationSchedule);
    clearTimers(scrollTimer);
    clearTimers(faceTimer);
    clickSchedule = null;
    scrollerSchedule = null;
    faceAnimationSchedule = null;
    scrollTimer = null;
    faceTimer = null;
    super.onDestroy();
}

private void clearTimers(Timer timer)
{
    if (timer != null)
    {
        timer.cancel();
        timer = null;
    }
}

private void clearTimerTaks(TimerTask timerTask)
{
    if (timerTask != null)
    {
        timerTask.cancel();
        timerTask = null;
    }
}

2 个答案:

答案 0 :(得分:0)

向后滚动将是最简单的。

添加一个设置为1.0的实例变量(称为scrollDist}

然后改变这一行

scrollPos = (int) (horizontalScrollview.getScrollX() + 1.0);

scrollPos = (int) (horizontalScrollview.getScrollX() + scrollDist);

和这一行

scrollPos = 0;

scrollDist *= -1.0;

这样每次它都会在滚动视图结束时反转。

答案 1 :(得分:0)

对于无限滚动,请使用以下snnipet

public void getScrollMaxAmount() {
        int actualWidth = (horizontalOuterLayout.getMeasuredWidth() - getWindowManager().getDefaultDisplay().getWidth());
        scrollMax = actualWidth;
    }
public void moveScrollView() {

        // ********************* Scrollable Speed ***********************

        scrollPos = (int) (horizontalScrollview.getScrollX() + 1.0);
        if (scrollPos >= scrollMax) {
            Log.v("childCount", ""+scrollMax); 
            addImagesToView();
            getScrollMaxAmount();
        }
        horizontalScrollview.scrollTo(scrollPos, 0);
    }