在日常开发中,NestedScrollView嵌套RecyclerView时,RecyclerView适配器中包含EditText时,用户在操作EditText时布局会自动滑动,导致用户交互问题。我在一个项目中也遇到类似的问题,网上搜索一大堆禁止这个滑动事件那个滑动事件的。试了一大堆,下面这个方法成功解决了我的问题,分享给大家。
1.、自定义NestedScrollView
代码:
public class NoNestedScrollViewextends NestedScrollView{
public NoNestedScrollView(@NonNull Context context) {
super(context);
}
public NoNestedScrollView(@NonNull Context context,@Nullable AttributeSet attrs) {
super(context, attrs);
}
public NoNestedScrollView(@NonNull Context context,@Nullable AttributeSet attrs,int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
return 0;
}
}
2、修改xml文件
<com.yym.ui.view.NoNestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rl_time"
android:overScrollMode="never"
android:fadingEdge="none"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_marginHorizontal="35dp"
android:layout_width="match_parent"
android:overScrollMode="never"
android:layout_height="wrap_content" />
<!--可以多个RecyclerView-->
</LinearLayout>
</com.yym.ui.view.NoNestedScrollView>