布局排版很简单,是ScrollView里嵌套了一个LinearLayout,在LinearLayout有个RecyclerView,RecyclerView每次刷新数据后会出现自动向上滚动的现象。解决办法如下:
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--父布局加了 android:descendantFocusability="blocksDescendants"-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<View.../>
<View .../>
<!--子布局加了android:overScrollMode="never"-->
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:overScrollMode="never"
app:layoutManager="LinearLayoutManager" />
<Button ... />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
android:descendantFocusability具体含义
<!-- Defines the relationship between the ViewGroup and its descendants
when looking for a View to take focus. -->
<attr name="descendantFocusability">
<!-- The ViewGroup will get focus before any of its descendants. -->
<enum name="beforeDescendants" value="0" />
<!-- The ViewGroup will get focus only if none of its descendants want it. -->
<enum name="afterDescendants" value="1" />
<!-- The ViewGroup will block its descendants from receiving focus. -->
<enum name="blocksDescendants" value="2" />
</attr>
android:overScrollMode具体含义
<!-- Defines over-scrolling behavior. This property is used only if the
View is scrollable. Over-scrolling is the ability for the user to
receive feedback when attempting to scroll beyond meaningful content. -->
<attr name="overScrollMode">
<!-- Always show over-scroll effects, even if the content fits entirely
within the available space. -->
<enum name="always" value="0" />
<!-- Only show over-scroll effects if the content is large
enough to meaningfully scroll. -->
<enum name="ifContentScrolls" value="1" />
<!-- Never show over-scroll effects. -->
<enum name="never" value="2" />
</attr>