ScrollView滚动到底部和顶部

ScrollView是Android提供的垂直滑动的组件,HorizontalScrollView与之对应是水平滑动的组件,内部只能包含一个根布局,否则会报错。
这里写图片描述

ScrollView中提供了fullScroll来滚动到的位置,有如下4个常用位置:

ScrollView.FOCUS_DOWN;//底部
ScrollView.FOCUS_UP;//顶部
ScrollView.FOCUS_LEFT;//左边
ScrollView.FOCUS_RIGHT;//右边

效果图如下
这里写图片描述

这里布局文件使用了4个按钮和1个HorizontalScrollView、1个ScrollView,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/down_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="滚动到底部" />

    <Button
        android:id="@+id/up_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="滚动到顶部" />

    <Button
        android:id="@+id/left_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="滚动到左边" />

    <Button
        android:id="@+id/right_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="滚动到右边" />

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView"
        android:layout_width="match_parent"
        android:layout_height="30dp">

        <LinearLayout
            android:id="@+id/horizontal_root_ll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" />
    </HorizontalScrollView>

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

        <LinearLayout
            android:id="@+id/root_ll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" />
    </ScrollView>

</LinearLayout>

MainActivity代码:

public class MainActivity extends AppCompatActivity {

    private Button down_btn;
    private Button up_btn;
    private Button left_btn;
    private Button right_btn;
    private ScrollView scrollView;
    private LinearLayout rootLl;

    private HorizontalScrollView horizontalScrollView;
    private LinearLayout horizontalRootLl;

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

    private void init() {
        down_btn = findViewById(R.id.down_btn);
        up_btn = findViewById(R.id.up_btn);
        left_btn = findViewById(R.id.left_btn);
        right_btn = findViewById(R.id.right_btn);
        scrollView = findViewById(R.id.scrollView);
        horizontalScrollView = findViewById(R.id.horizontalScrollView);
        horizontalRootLl = findViewById(R.id.horizontal_root_ll);
        rootLl = findViewById(R.id.root_ll);
        TextView tv;
        for (int i = 0; i < 100; i++) {
            tv = new TextView(this);
            tv.setText("第" + i + "个");
            rootLl.addView(tv);
        }
        for (int i = 0; i < 100; i++) {
            tv = new TextView(this);
            tv.setText("第" + i + "个");
            horizontalRootLl.addView(tv);
        }
        down_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
        up_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.fullScroll(ScrollView.FOCUS_UP);
            }
        });
        left_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                horizontalScrollView.fullScroll(ScrollView.FOCUS_LEFT);
            }
        });
        right_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                horizontalScrollView.fullScroll(ScrollView.FOCUS_RIGHT);
            }
        });
    }
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值