ScrollView 初始化的时候不在最顶部?

日常开发中经常用到ScrollView嵌套控件,比如RecyclerView、自定义View、动态创建的View等等

在初始化的时候可能会直接滑动到子View的位置,这是因为子view 主动请求并获取到了焦点

所以呢,解决办法也很简单,在ScrollView或者更外层的根布局添加两个属性即可,即把焦点抢回来的意思

android:focusable="true"
android:focusableInTouchMode="true"



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要将TabLayout固定在ScrollView顶部,可以使用以下方法: 1. 使用CoordinatorLayout和AppBarLayout 将ScrollView作为CoordinatorLayout的子视图,并将AppBarLayout作为ScrollView的直接父视图。在AppBarLayout中添加TabLayout和Toolbar,然后使用app:layout_scrollFlags属性将Toolbar和TabLayout与ScrollView关联起来。 例如: ``` <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <com.google.android.material.appbar.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways"> <!-- Toolbar content --> </com.google.android.material.appbar.Toolbar> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" app:tabGravity="fill" app:tabMode="fixed"> <!-- TabLayout content --> </com.google.android.material.tabs.TabLayout> </com.google.android.material.appbar.AppBarLayout> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-- ScrollView content --> </ScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` 2. 使用NestedScrollView和LinearLayout 将TabLayout和ScrollView放在一个LinearLayout中,然后将LinearLayout放在NestedScrollView中。在TabLayout中添加app:layout_scrollFlags属性,并将其设置为scroll|enterAlways。 例如: ``` <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" app:tabGravity="fill" app:tabMode="fixed"> <!-- TabLayout content --> </com.google.android.material.tabs.TabLayout> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <!-- ScrollView content --> </ScrollView> </LinearLayout> </androidx.core.widget.NestedScrollView> ``` 注意:在使用第二种方法时,TabLayout必须是LinearLayout的第一个子视图。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yechaoa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值