最近在用recyclerview 来显示数据,但问题是在布局中,如果有多个控件在一起的话,recyclerview子布局数量一多就会自己滑动,让人的体验不是很好,所以我解决了这个问题,让recyclerview有多少item的时候显示多少,随着屏幕滑动,把自己的滑动给禁用了。
下面是布局文件:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:overScrollMode="never"
android:visibility="invisible"
android:id="@+id/recyclerContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
</ScrollView>
这里的recyclerview 用relativelayout 套着,作用是先recyclerview的数据全部显示出来,如果不用的话,rc只能显示一部分的item,然后再外层是scrollview ,这里的作用是让滑动更加流畅,如果不加srollview的话,去滑动rc 就只能滑动一下,滚一下的效果。
下面还要把 rc 的水平或者垂直方向的滑动禁用了,重写布局管理器,这里我用的是LinearLayoutManager,下面贴代码
import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
class MyLayoutManager(val context: Context):LinearLayoutManager(context) {
override fun canScrollVertically(): Boolean {
return false
}
}
canScrollVertically 方法是垂直滑动,返回false就行了。
最后,设置完效果也就出来!!!
又添了一个坑,我想应该有更好的解决方法吧。