View中的scrollby记录

本文记录了在Android开发中遇到的关于View的scrollBy方法实际运行效果与预期不同的情况。在TestScroller活动中,当点击scroller1按钮时,Button内的文字移动但Button本身未移动;而点击LinearLayout ll_scroller时,整个布局没有响应滚动。问题可能涉及到事件处理和View的滚动机制。
摘要由CSDN通过智能技术生成

View中的scrollby记录

想象中的scrollBy和我运行的不同,做个记录:

activity:


class TestScroller : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test_scroller)
        scroller1.setOnClickListener {
            scroller1.scrollBy(-100,0)
        }

        ll_scroller.setOnClickListener {
            ll_scroller.scrollBy(-100,0)
//            ll_scroller.invalidate()
        }
    }
}

布局:

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

    <Button
        android:id="@+id/scroller1"
        android:text="测试Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/ll_scroller"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/scroller2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="测试LinearLayout" />
    </LinearLayout>

</LinearLayout>

结果:
在这里插入图片描述

1 : 里面的字会动,但是button不会动
2 :不会动 没有监听都
3 :不会动,因为外面的onclick被里面的button处理了也不会动
4 :会像右移动

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值