【问题标题】:Horizontal ScrollView Gallery. Scroll both directions or from begining水平滚动视图库。滚动两个方向或从头开始
【发布时间】:2019-03-03 16:59:23
【问题描述】:

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

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;
    }
}

【问题讨论】:

    标签: android scrollview


    【解决方案1】:

    以另一种方式向后滚动是最简单的。

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

    然后改变这一行

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

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

    还有这一行

    scrollPos = 0;

    scrollDist *= -1.0;

    这样每次到达滚动视图的末尾时它都会反转。

    【讨论】:

      【解决方案2】:

      对于无限滚动,请使用以下 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);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多