最近在项目遇到这样的问题:在一线性垂直布局内,有两个垂直的RecyclerView,如果直接高度直接设置wrap-content,
通常会导致滑动冲突或是内容显示不全。

首先说下解决的思路,就是在最外面嵌套一层自定义的ScrollView,重写其相关方法,判断若为垂直滑动则拦截下来,不交由RecyclerView来处理。

这样的话,滑动冲突就能解决,并且是很流畅的。

不过这样在有些设备可能还会出现个问题, 就是内容显示不全。这里可以通过在显示不全的RecyclerView外面套一层RelativeLayout,即:


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!-- 和项目为androidx无关 --> <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>

这样的话, 内容就可以显示完全了。

下面继续ScrollView的滑动事件


自定义一个view,并继承自ScrollView, 再重写onInterceptTouchEvent方法;

具体内容则是:


@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) > touchSlop) {
return true;
}
}
return super.onInterceptTouchEvent(e);
}

获取触屏开始和结束位置坐标的Y值,取相减的绝对值,再与touchSlop比较,大于这个值则判断此次为滑动事件,则拦截。

其中touchSlop 是判断是否滑动的参考值。官方给出的解释是

/**
* Distance in pixels a touch can wander before we think the user is scrolling
*/

大体过程就是这样,下面贴上自定义ScrollView的全部代码:



package com.asche.wetalk.helper;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.ScrollView; /**
* 拦截滑动事件,不由recyclerview处理
*/
public class MyScrollViewScroll extends ScrollView {
// private int downX;
private int downY;
private int touchSlop; public MyScrollViewScroll(Context context) {
super(context);
touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
} public MyScrollViewScroll(Context context, AttributeSet attrs) {
super(context, attrs);
touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
} public MyScrollViewScroll(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
touchSlop = 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) > touchSlop) {
return true;
}
}
return super.onInterceptTouchEvent(e);
} @Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
}
}

安卓多个RecyclerView滑动与显示问题的更多相关文章

  1. 安卓开发——WebView+Recyclerview文章详情页,解决高度问题

    安卓开发--WebView+Recyclerview文章详情页,解决高度问题 最近在写一个APP时,需要显示文章详情页,准备使用WebView和RecyclerView实现上面文章,下面评论.出现了W ...

  2. 解决ScrollView嵌套RecyclerView出现item显示不全的问题

      问题:ScrollView嵌套RecyclerView时,RecyclerView的item显示不全 出现问题不要慌,耐心解决才是王道,哈哈.首先说下出现这个问题的情景吧,首先声明这个问题在23版 ...

  3. js鼠标滑动图片显示隐藏效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Android上下左右滑动,显示底层布局

    转载博客地址:http://www.cnblogs.com/flyme2012/p/4106308.html 闲着没事做了一个小东西.Android的上下左右滑动,显示底层布局.可以做类似于QQ消息列 ...

  5. ios 仿新浪微博 UINavigationController 向左滑动时显示上一个控制器的View.

    仿新浪微博 UINavigationController 向左滑动时显示上一个控制器的View. 实现原理,UINavigationController 的 self.view显示时把当前显示的vie ...

  6. 监听RecyclerView滑动到末端

    监听RecyclerView滑动到末端 效果图 实现 1. 添加滑动到末端的接口 package com.kongqw.recyclerviewdemo; /** * Created by kongq ...

  7. Android ScrollView嵌套Recyclerview滑动卡顿,松手即停问题解决;

    假如你的布局类似这样的: <ScrollView android:layout_width="match_parent" android:layout_height=&quo ...

  8. 安卓,网页控件,显示网页 Android, web controls, display web pages

    安卓,网页控件,显示网页Android, web controls, display web pages 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq ...

  9. android studio中使用recyclerview制作个显示考勤打卡的日历来

    1. 用户在app端选择个日期就能查询这个月的考勤打卡信息,并以日历上标注不同的颜色来显示给用户,当然这个日历是recyclerview做出来的,只是每行显示7个,表示一周的七天. 2. 员工考勤打卡 ...

随机推荐

  1. c++语言的组合类的使用,用组合类的方法计算两点间距离。

    组合类的使用主要涉及到类的构造函数,类的复制构造函数. #include <iostream> #include<cmath> class Point{ public: Poi ...

  2. Berkeley DB 使用经验总结

    作者:陈磊 NoSQL是现在互联网Web2.0时代备受关注的技术之一,被用来存储大量的非关系型的数据.Berkeley DB作为一款优秀的Key/Value存储引擎自然也在讨论之列.最近使用BDB来发 ...

  3. Linux串口参数设置

    linux串口编程参数配置详解 1.linux串口编程需要的头文件 #include <stdio.h>         //标准输入输出定义#include <stdlib.h&g ...

  4. NOIP后省选集训前文化课划水记

    划水划了一个多月,文化课没啥长进还他妈累死了...--ghj1222 11.11(NOIP Day2) 师傅开车开得很快,晚上8:00多就到了二狱 晚上听毒瘤班主任swh讲了半节语文,我:黑人问号.j ...

  5. USACO 1.1.2 Greedy Gift Givers(gift1)

    这道题大意就是几个人互送礼物,让你求每个人的盈利. 原题给的样例数据: 5(人的个数.) =========(下面是人名,输出按照这顺序)davelauraowenvickamr ========== ...

  6. CentOS文件服务与数据管理

    CentOS文件服务与数据管理-专栏简介 本专栏内容涵盖了中高级Linux系统管理员所必须的文件服务.磁盘管理.数据管理.文件恢复等必备技能,实乃涨薪.跳槽之必备技能,且听一线运维老兵为你逐步揭开迷雾 ...

  7. (转)ios学习--你会遇到的runtime面试题(详)

    1.了解runtime吗?是什么? 2.你怎么知道的? 3.对象如何找到对应方法去调用的 于是我总结了很多网上被问到的一些关于runtime的题目,并做了详细的回答,并在后面补充了我在学习runtim ...

  8. Arcgis Server for JavaScript API之自定义InfoWindow

    各位看到这个标题不要嫌烦,因为本人最近一直在研究相关的问题,所以相关文章也只能是这些,同时希望看过我的文章的朋友,我的文章能够给你帮助. 在前面的两篇相关的文章里面,实现InfoWindow是通过di ...

  9. docker上安装eslaticsearch

    一 elasticsearch安装 1 查找镜像: docker search elasticsearch 可以看到如下截图; 2 拉取镜像 docker pull elasticsearch 我并没 ...

  10. 优先队列priority_queue的简单应用

    优先队列 引入 优先队列是一种特殊以及强大的队列. 那么优先队列是什么呢? 说白了,就是一种功能强大的队列. 它的功能强大在哪里呢? 四个字:自动排序. 优先队列的头文件&&声明 头文 ...