You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现根据星期几自动滚动HorizontalScrollView至对应日期按钮

实现HorizontalScrollView自动滚动到当天对应星期按钮

步骤1:编写布局文件

先在布局里定义HorizontalScrollView,内部嵌套LinearLayout承载周一到周五的按钮,给每个按钮设置唯一ID:

<HorizontalScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_monday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MONDAY"/>

        <Button
            android:id="@+id/btn_tuesday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TUESDAY"/>

        <Button
            android:id="@+id/btn_wednesday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="WEDNESDAY"/>

        <Button
            android:id="@+id/btn_thursday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="THURSDAY"/>

        <Button
            android:id="@+id/btn_friday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="FRIDAY"/>

    </LinearLayout>
</HorizontalScrollView>

步骤2:在代码中实现自动滚动逻辑

方式1:Java代码(API 26+)

import android.os.Bundle;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import androidx.appcompat.app.AppCompatActivity;
import java.time.DayOfWeek;
import java.time.LocalDate;

public class MainActivity extends AppCompatActivity {
    private HorizontalScrollView scrollView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scrollView = findViewById(R.id.scrollView);

        // 等待视图绘制完成再执行滚动,避免获取不到按钮真实位置
        scrollView.post(() -> {
            DayOfWeek today = LocalDate.now().getDayOfWeek();
            Button targetBtn = null;

            switch (today) {
                case MONDAY:
                    targetBtn = findViewById(R.id.btn_monday);
                    break;
                case TUESDAY:
                    targetBtn = findViewById(R.id.btn_tuesday);
                    break;
                case WEDNESDAY:
                    targetBtn = findViewById(R.id.btn_wednesday);
                    break;
                case THURSDAY:
                    targetBtn = findViewById(R.id.btn_thursday);
                    break;
                case FRIDAY:
                    targetBtn = findViewById(R.id.btn_friday);
                    break;
            }

            // 滚动到目标按钮左侧,让按钮显示在可视区域首位
            if (targetBtn != null) {
                scrollView.scrollTo(targetBtn.getLeft(), 0);
                // 如需平滑滚动,替换为:scrollView.smoothScrollTo(targetBtn.getLeft(), 0);
            }
        });
    }
}

方式2:Kotlin代码(API 26+)

import android.os.Bundle
import android.widget.Button
import android.widget.HorizontalScrollView
import androidx.appcompat.app.AppCompatActivity
import java.time.DayOfWeek
import java.time.LocalDate

class MainActivity : AppCompatActivity() {
    private lateinit var scrollView: HorizontalScrollView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        scrollView = findViewById(R.id.scrollView)

        scrollView.post {
            val today = LocalDate.now().dayOfWeek
            val targetBtn = when(today) {
                DayOfWeek.MONDAY -> findViewById<Button>(R.id.btn_monday)
                DayOfWeek.TUESDAY -> findViewById<Button>(R.id.btn_tuesday)
                DayOfWeek.WEDNESDAY -> findViewById<Button>(R.id.btn_wednesday)
                DayOfWeek.THURSDAY -> findViewById<Button>(R.id.btn_thursday)
                DayOfWeek.FRIDAY -> findViewById<Button>(R.id.btn_friday)
                else -> null
            }

            targetBtn?.let {
                scrollView.scrollTo(it.left, 0)
                // 平滑滚动替换为:scrollView.smoothScrollTo(it.left, 0)
            }
        }
    }
}

低版本兼容(API <26)

如果要兼容Android 7.0以下版本,把LocalDate替换为Calendar:

// Java代码替换星期获取部分
Calendar calendar = Calendar.getInstance();
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
Button targetBtn = null;

// Calendar规则:周日=1,周一=2,周二=3,周三=4,周四=5,周五=6
switch (dayOfWeek) {
    case Calendar.MONDAY:
        targetBtn = findViewById(R.id.btn_monday);
        break;
    case Calendar.TUESDAY:
        targetBtn = findViewById(R.id.btn_tuesday);
        break;
    case Calendar.WEDNESDAY:
        targetBtn = findViewById(R.id.btn_wednesday);
        break;
    case Calendar.THURSDAY:
        targetBtn = findViewById(R.id.btn_thursday);
        break;
    case Calendar.FRIDAY:
        targetBtn = findViewById(R.id.btn_friday);
        break;
}

关键注意点

  • 必须在view.post()中执行滚动操作:视图刚创建时未完成绘制,直接调用getLeft()会得到0,导致滚动无效。
  • 平滑滚动需求:将scrollTo()替换为smoothScrollTo()即可实现缓动滚动效果。

内容的提问来源于stack exchange,提问作者Isay SR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 21:36:29