NestedScrollView+Recyclerview下滑卡顿解决方法

2018-02-17 23:33:20

直接在xml中定义,android:nestedScrollingEnabled="false"

官方文档:
Enable or disable nested scrolling for this view.
If this property is set to true the view will be permitted to initiate nested scrolling operations with a compatible parent view in the current hierarchy. If this view does not implement nested scrolling this will have no effect. Disabling nested scrolling while a nested scroll is in progress has the effect of stopping  the nested scroll.
这里设置为false,放弃自己的滑动,交给外部的NestedScrollView处理,就没有出现卡顿的现象了,并且有fling的效果


在6.0以上的手机并没有发现问题,不过在4.4的手机上,api为19的却依然卡顿,解决的办法。在java代码中设置。

recyclerView.setHasFixedSize(
true); recyclerView.setNestedScrollingEnabled(false);


解决了问题。


本文为大家解决安卓开发时候NestedScrollView+Recyclerview下滑卡顿的问题,希望能够帮助到你。

大家在进行安卓开发用到NestedScrollView+Recyclerview的时候,经常出现的情况就是加载下滑的时候没有任何问题,很流畅,但是在下滑以后明显出现了卡顿的情况,小编根绝这个问题,给大家再来的解决方法,一起来学习下。

我们先来看下这个BUG的表现:
1.滑动卡顿,
2.加载下滑时流畅,下滑时明显的卡顿
3.进入页面时直接加载RecyclerView部分的内容(这里我理解为控件惯性,不知道对不对-------尴尬!!!!!!)
下面我们一一来解决这些问题

在开发项目中,涉及到到商品详情页,新闻详情页等的页面时,通常情况下,商品详情页的底部会附上商品的评论或者是相关商品的的推荐,或者是相关性的文章.那么我们就会用到列表的RecyclerView,在头部可能是一些比较复杂的多种界面,可能采用比较简单的方法来处理,那就是NestedScrollView+Recyclerview,这这种方式比较直观和方便操作.比如像下面的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:id="@+id/scrollView_comment"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 >

 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
 .....此处省略  
  <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="44dp"
   android:gravity="center">

   <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
     android:layout_width="20dp"
     android:layout_height="20dp"
     android:src="@color/text_msg_33"/>

    <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="12dp"
     android:layout_marginRight="12dp"
     android:text="1"
     android:textColor="#8c8c8c"
     android:textSize="15sp"/>

    <ImageView
     android:layout_width="20dp"
     android:layout_height="20dp"
     android:src="@color/text_msg_33"/>
   </LinearLayout>

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="20dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/bg_shop_card"
    android:gravity="center"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:text="加入购物车"
    android:textColor="@color/white"
    android:textSize="14sp"/>
  </LinearLayout>

  <View
   android:layout_width="match_parent"
   android:layout_height="10dp"
   android:background="#f2f2f2"/>

  <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="35dp"
   android:gravity="center_vertical">

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="11dp"
    android:text="用户评价"
    android:textColor="#666666"
    android:textSize="13sp"/>

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/line_1px"
    android:layout_marginRight="20dp"
    android:text="(21313)"
    android:textColor="#666666"
    android:textSize="13sp"/>

  </LinearLayout>

  <View
   android:layout_width="match_parent"
   android:layout_height="0.5dp"
   android:background="#dcdcdc"/>

  <android.support.v7.widget.RecyclerView
   android:id="@+id/recycler_seller_comment"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:descendantFocusability="blocksDescendants"
   android:nestedScrollingEnabled="false"
   />

  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="查看更多"
   android:textColor="#8c8c8c"
   android:textSize="13sp"/>
 </LinearLayout>
</android.support.v4.widget.NestedScrollView>

首先.滑动动卡顿的问题.

在布局文件中添加

android:nestedScrollingEnabled="false"

这一属性

或者通过代码设置也是可以的,

mRecycler.setNestedScrollingEnabled(false);

这样滑动的时候就不会出现有卡顿的现象.

其次是加载上下滑动加载流畅时

通过代码

mRecycler.setHasFixedSize(false);

对于第三种现象,我找了很多方法,都以失败而告终,其实出现这种情况是应为Recyclerview在加载数据的时候获取到了焦点导致,所

以只需要在对RecylerView在带中设置不能获取焦点即可.

添加以下代码

mRecycler.setFocusable(false);

 

以上是小编测试过的解决方法,接下来,我们再给大家分享一篇简单的方法代码:

 最开始使用ScrollView的时候嵌套ListView会出现item显示不全等一些问题,现在google提供NestedScrollView已经可以解决该问题,但是在使用NestedScrollView嵌套RecyclerView的时候会发现我们在RecyclerView上滑动的时候没有了滚动的效果,查看文档找到的解决办法:

 LinearLayoutManager layoutManager = new LinearLayoutManager(this);
  layoutManager.setSmoothScrollbarEnabled(true);
  layoutManager.setAutoMeasureEnabled(true);

  recyclerView.setLayoutManager(layoutManager);
  recyclerView.setHasFixedSize(true);
  recyclerView.setNestedScrollingEnabled(false);

就在小编完稿的时候,又发现了两种方法,大神真的是多啊,一起整理后分享给你

当ScrollView嵌套RecyclerView时,会出现滑动卡顿,不平滑的效果。对此有两种解决方案。

方案一

设置RecyclerView属性方法

recyclerView.setHasFixedSize(
true); recyclerView.setNestedScrollingEnabled(false);

或者直接在recycleview中 添加属性

android:nestedScrollingEnabled="false"


方案二

如果方案一无效,不妨试试重写ScrollView的onInterceptTouchEvent()方法,强制让其触摸事件都交给其子控件去处理

public class RecycleScrollView extends ScrollView {
private int downX;
private int downY;
private int mTouchSlop;
public RecycleScrollView(Context context) {
super(context);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public RecycleScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public RecycleScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
int action = e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downX = (int) e.getRawX();
downY = (int) e.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int moveY = (int) e.getRawY();
if (Math.abs(moveY - downY) > mTouchSlop) {
return true;
}
}
return super.onInterceptTouchEvent(e);
}
}

通过以上操作,界面就不会再卡顿了,还原了原本的惯性。

以上就是关于NestedScrollView+Recyclerview下滑卡顿的所有方法,希望我们整理的东西能够真正帮助到你,喜欢的话就收藏一下吧。


  • 2021-02-03 16:58:39

    iOS静态库与动态库的区别与打包

    这篇主要是记录一下 iOS 下静态库与动态库的打包流程,以便以后用到时快速查阅,供自己也供大家学习记录。同时也简述了一下 动态库 与 静态库 的区别。

  • 2021-02-03 16:59:59

    iOS 静态库和动态库全分析

    库就是程序代码的集合,将 N 个文件组织起来,是共享程序代码的一种方式。从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行。

  • 2021-02-03 17:01:30

    iOS库 .a与.framework区别

    静态库:连接时完整地拷贝至可执行文件中,被屡次使用就有多份冗余拷贝。 动态库:连接时不复制,程序运行时由系统动态加载到内存,供程序调用,系统只加载一次,多个程序共用,节省内存。

  • 2021-02-03 17:13:58

    iOS - 封装静态库

    静态库:链接时完整的拷贝至可执行文件中,被多次使用就有多份冗余拷贝,.a的静态库 .framework的静态库

  • 2021-02-03 17:16:07

    iOS 中的动态库、静态库和 framework

    首先来看什么是库,库(Library)说白了就是一段编译好的二进制代码,加上头文件就可以供别人使用。 什么时候我们会用到库呢?一种情况是某些代码需要给别人使用,但是我们不希望别人看到源码,就需要以库的形式进行封装,只暴露出头文件。另外一种情况是,对于某些不会进行大的改动的代码,我们想减少编译的时间,就可以把它打包成库,因为库是已经编译好的二进制了,编译的时候只需要 Link 一下,不会浪费编译时间。

  • 2021-02-03 17:17:53

    iOS 同一个工程下打包不同的app

    应用图标,启动画面,应用启动后的首页都不一样。 有些课程(例如公务员考试和高考)是有目标考试的概念,不同的目标考试大纲是不一样的。拿高考来举例,北京的高考和上海的高考,就有着完全不一样的考试大纲。高考的文科和理科,又有着完全不同的考试科目。 有些课程会有一些自定义的界面,例如高考的应用可以设置昵称,有些课程的真题练习中是有推荐真题模块的,而有些课程又没有。 有些课程有扫描答题卡功能,有些课程有考前冲刺功能,有些课程有大题专项查看功能,而有些课程又没有上述功能。另外还有一些微小细节,但是解决方法和类似,所以就不一一展开说明。

  • 2021-02-04 14:02:30

    window软件界面找不到了跑到屏幕外面去了

    一般可以这样操作,按Alt+空格,然后按M,然后用上下左右键把窗口移动到能看到的地方,再按回车。有些第三方的软件可能不能用,大部分都可以这样做。

  • 2021-02-04 14:08:13

    基于 Electron 的爬虫框架 Nightmare

    Electron 可以让你使用纯 JavaScript 调用 Chrome 丰富的原生的接口来创造桌面应用。你可以把它看作一个专注于桌面应用的 Node.js 的变体,而不是 Web 服务器。其基于浏览器的应用方式可以极方便的做各种响应式的交互,接下来介绍下关于 Electron 上衍生出的框架 Nightmare

  • 2021-02-04 20:13:02

    iOS framework制作及使用(包含xib和图片)

    静态库与动态库简介: 静态库:链接使用时完整地拷贝至可执行文件中 动态库:链接时不复制,程序运行时由系统动态加载到内存,供程序调用,系统只加载一次 本文制作framework对应xcode版本:10.1