AndroidUI系列--在DecorView层解决RecyclerView和ScrollView的滑动冲突

本文主要是介绍AndroidUI系列--在DecorView层解决RecyclerView和ScrollView的滑动冲突,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

滑动冲突,这个是作安卓的必经之坑。最开始的ListView和ScollView冲突,或者ListView嵌套ListView滑动冲突,再或者ListView和ViewPager的滑动冲突,再或者是GraidView等可滑动控件互相嵌套的冲突。解决方案呢,有很多。比如在onTouchEvent中拦截事件。又或者自定义ListView,修改onMesure测量,使它在测量时获得最大的宽高,这样可以让它不滑动。全部展示,当然作为在android摸爬滚打了这么久的程序猿,这些坑都应该踩过了,而且网上一大堆解决方案,不得不说,这就是开源的好处啊,想着谷歌巴巴把kotlin扶上位了,我们这些苦逼的程序猿,那就只有跟着大部队走了。没办法呀~夹缝里生存。

这里写图片描述

View的绘制流程,Activity–phonewindow–decorview–contentview,如下图

这里写图片描述

我们平时在Activity的setContentView就是在ContentViews作文章。那么我们的冲突就是在这里,在ContentView里设置了一个activity_main.xml,为什么会有滑动冲突呢,那是因为recyclerview和scollview都设置在了activity_main.xml。那么换个角度,如果把recyclerview加在contentviews和activity_main.xml布局平级。那么是不是就不存在滑动冲突了呢,想到就来试试。
首先自定义一个view,用来弹窗。

package com.example.administrator.bounceview;import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;/*** Created by ShuWen on 2017/5/23.*/public class BounceView extends View {private int mArcMaxHeight;//弹窗最高距离private int mArcHeight;//记录变换过程的距离private Paint mPaint;//画笔private Path mPath = new Path();//绘制动画弧度private BounceAnimatorListener animatorListener;//动画开始的监听回调private Status status = Status.NONE;//记录动画的状态public enum Status{//没动,上升,下降NONE,STATUS_UP,STATUS_DOWN}public BounceView(Context context) {super(context);init();}public BounceView(Context context, AttributeSet attrs) {super(context, attrs);init();}public BounceView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();}//初始化private void init() {mPaint = new Paint();mPaint.setColor(Color.WHITE);mPaint.setAntiAlias(true);mPaint.setStyle(Paint.Style.FILL);mArcMaxHeight = getResources().getDimensionPixelOffset(R.dimen.m_maxarcheight);}//上升的动画public void show(){status = Status.STATUS_UP;if (animatorListener != null){this.postDelayed(new Runnable() {@Overridepublic void run() {animatorListener.showContent();}},600);}ValueAnimator animator = ValueAnimator.ofInt(0,mArcMaxHeight);animator.setDuration(700);animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator valueAnimator) {mArcHeight = (int) valueAnimator.getAnimatedValue();if (mArcHeight == mArcMaxHeight){bounce();}invalidate();}});animator.start();}//下降的动画private void bounce() {status = Status.STATUS_DOWN;ValueAnimator valueAnimator = ValueAnimator.ofInt(mArcMaxHeight,0);valueAnimator.setDuration(600);valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator valueAnimator) {mArcHeight = (int) valueAnimator.getAnimatedValue();invalidate();}});valueAnimator.start();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);int currentY = 0;switch (status){case NONE:currentY = 0;break;case STATUS_UP:currentY = (int) (getHeight()*(1 - (float)(mArcHeight/mArcMaxHeight))+mArcMaxHeight);break;case STATUS_DOWN:currentY = mArcMaxHeight;break;}mPath.reset();mPath.moveTo(0,currentY);mPath.quadTo(getWidth()/2,currentY - mArcHeight,getWidth(),currentY);mPath.lineTo(getWidth(),getHeight());mPath.lineTo(0,getHeight());mPath.close();canvas.drawPath(mPath,mPaint);}public void setAnimatorListener(BounceAnimatorListener animatorListener){this.animatorListener = animatorListener;}public interface BounceAnimatorListener{void showContent();}
}

上升过程中,绘制动画,使用ValueAnimator在回调里进行更新界面,调用invalidate()。中间使用到了二阶贝塞尔曲线,关于贝塞尔其实很简单的,在网上一搜,当然就有了。

那么在创建一个类,用来加载BounceVeiw。

package com.example.administrator.bounceview;import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;/*** Created by ShuWen on 2017/5/23.*/public class BounceMenu {private RecyclerView recyclerView;private BounceView bounceView;private ViewGroup parentVG;private View rootView;private BounceMenu(View view, int resId, final MyAdapter myAdapter) {parentVG = findParentVG(view);rootView = LayoutInflater.from(view.getContext()).inflate(resId,null,false);recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);bounceView = (BounceView) rootView.findViewById(R.id.bounceview);recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));bounceView.setAnimatorListener(new BounceView.BounceAnimatorListener() {@Overridepublic void showContent() {recyclerView.setVisibility(View.VISIBLE);recyclerView.setAdapter(myAdapter);recyclerView.scheduleLayoutAnimation();}});}public static BounceMenu makeBounce(View view, int resId, final MyAdapter myAdapter){return new BounceMenu(view, resId, myAdapter);}public void show(){if (rootView != null){parentVG.removeView(rootView);}ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);parentVG.addView(rootView,layoutParams);bounceView.show();}private ViewGroup findParentVG(View view) {do {if (view instanceof FrameLayout){//找到decorview的根布局if (view.getId() == android.R.id.content){return (ViewGroup) view;}}if (view != null){ViewParent viewParent = view.getParent();view = viewParent instanceof View? (View) viewParent :null;}}while (view!= null);return null;}
}

在这里面,传入需要添加recylerview的跟布局,通过这个根布局获得decorview的contentviews这个布局,然后在这个布局上添加recyclerview。这样就是与activity_main同级,不会有滑动冲突。在bounceview的监听里,添加recyclerview的动画。
那么再来看看MianAcicity的代码:

package com.example.administrator.bounceview;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity {private MyAdapter myAdapter;private List<String> stringList;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);stringList = new ArrayList<>();for (int i = 0; i < 20; i++) {stringList.add("阿西吧"+i);}myAdapter = new MyAdapter(this,stringList) {@Overrideprotected int ItemLayoutId() {return R.layout.item;}@Overrideprotected void onBindHolder(MyViewHolder myViewHolder, int position) {TextView textView = myViewHolder.getTextView(R.id.text);textView.setText(stringList.get(position));}};}public void click(View view){BounceMenu bounceMenu = BounceMenu.makeBounce(findViewById(R.id.activity_main),R.layout.bounce_view_layout,myAdapter);bounceMenu.show();}
}

调用就是相当的简单了。同时还有炫酷的动画,何乐而不为呢。接下来所有代码都贴出来。
activity_main的xml布局。使用ScrollView,展示主要数据。在每一项的点击事件,触发弹窗,recyclerview。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.administrator.bounceview.MainActivity"><ScrollView
        android:layout_width="match_parent"android:layout_height="match_parent"android:background="#465"><LinearLayout
            android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextView
                android:layout_width="match_parent"android:layout_height="100dp"android:gravity="center"android:textSize="20sp"android:onClick="click"android:text="你滑动啊"/><View
                android:layout_width="match_parent"android:layout_height="1dp"android:background="#fff"></View><TextView
                android:layout_width="match_parent"android:layout_height="100dp"android:gravity="center"android:textSize="20sp"android:onClick="click"android:text="你滑动啊"/><View
                android:layout_width="match_parent"android:layout_height="1dp"android:background="#fff"></View><TextView
                android:layout_width="match_parent"android:layout_height="100dp"android:gravity="center"android:onClick="click"android:textSize="20sp"android:text="你滑动啊"/><View
                android:layout_width="match_parent"android:layout_height="1dp"android:background="#fff"></View><TextView
                android:layout_width="match_parent"android:layout_height="100dp"android:gravity="center"android:textSize="20sp"android:onClick="click"android:text="你滑动啊"/><View
                android:layout_width="match_parent"android:layout_height="1dp"android:background="#fff"></View><TextView
                android:layout_width="match_parent"android:layout_height="100dp"android:gravity="center"android:textSize="20sp"android:onClick="click"android:text="你滑动啊"/><View
                android:layout_width="match_parent"android:layout_height="1dp"android:background="#fff"></View><TextView
                android:layout_width="match_parent"android:layout_height="100dp"android:gravity="center"android:textSize="20sp"android:onClick="click"android:text="你滑动啊"/><View
                android:layout_width="match_parent"android:layout_height="1dp"android:background="#fff"></View><TextView
                android:layout_width="match_parent"android:layout_height="100dp"android:gravity="center"android:textSize="20sp"android:onClick="click"android:text="你滑动啊"/><View
                android:layout_width="match_parent"android:layout_height="1dp"android:background="#fff"></View> <TextView
            android:layout_width="match_parent"android:layout_height="100dp"android:gravity="center"android:textSize="20sp"android:onClick="click"android:text="你滑动啊"/><View
                android:layout_width="match_parent"android:layout_height="1dp"android:background="#fff"></View><TextView
                android:layout_width="match_parent"android:layout_height="100dp"android:gravity="center"android:textSize="20sp"android:onClick="click"android:text="你滑动啊"/><View
                android:layout_width="match_parent"android:layout_height="1dp"android:background="#fff"></View></LinearLayout></ScrollView></RelativeLayout>

那么再看看弹窗的xml。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><RelativeLayout
        android:layout_width="match_parent"android:layout_height="290dp"android:layout_gravity="bottom"><com.example.administrator.bounceview.BounceView
            android:id="@+id/bounceview"android:layout_width="match_parent"android:layout_height="wrap_content"/><android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"android:layout_width="match_parent"android:layout_height="match_parent"android:clipChildren="false"android:overScrollMode="never"android:layout_alignParentBottom="true"android:layoutAnimation="@anim/bounce_layout"android:layout_marginTop="70dp"></android.support.v7.widget.RecyclerView></RelativeLayout></FrameLayout>

这里就是弹窗了,recyclerview和scrollview同级,不会产生滑动冲突。
这个是解决滑动冲突的一个可行方案,相当不错。如果觉得动画不必要,直接去掉动画,只需要BounceMenu中的一些逻辑就ok了。我会把代码放在git上,有兴趣的朋友可以自己研究研究。

git地址:https://github.com/SingleShu/BounceView

效果图:

这篇关于AndroidUI系列--在DecorView层解决RecyclerView和ScrollView的滑动冲突的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/758001

相关文章

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

解决pandas无法读取csv文件数据的问题

《解决pandas无法读取csv文件数据的问题》本文讲述作者用Pandas读取CSV文件时因参数设置不当导致数据错位,通过调整delimiter和on_bad_lines参数最终解决问题,并强调正确参... 目录一、前言二、问题复现1. 问题2. 通过 on_bad_lines=‘warn’ 跳过异常数据3

解决RocketMQ的幂等性问题

《解决RocketMQ的幂等性问题》重复消费因调用链路长、消息发送超时或消费者故障导致,通过生产者消息查询、Redis缓存及消费者唯一主键可以确保幂等性,避免重复处理,本文主要介绍了解决RocketM... 目录造成重复消费的原因解决方法生产者端消费者端代码实现造成重复消费的原因当系统的调用链路比较长的时

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499

SpringBoot监控API请求耗时的6中解决解决方案

《SpringBoot监控API请求耗时的6中解决解决方案》本文介绍SpringBoot中记录API请求耗时的6种方案,包括手动埋点、AOP切面、拦截器、Filter、事件监听、Micrometer+... 目录1. 简介2.实战案例2.1 手动记录2.2 自定义AOP记录2.3 拦截器技术2.4 使用Fi

kkFileView启动报错:报错2003端口占用的问题及解决

《kkFileView启动报错:报错2003端口占用的问题及解决》kkFileView启动报错因office组件2003端口未关闭,解决:查杀占用端口的进程,终止Java进程,使用shutdown.s... 目录原因解决总结kkFileViewjavascript启动报错启动office组件失败,请检查of

SQL Server安装时候没有中文选项的解决方法

《SQLServer安装时候没有中文选项的解决方法》用户安装SQLServer时界面全英文,无中文选项,通过修改安装设置中的国家或地区为中文中国,重启安装程序后界面恢复中文,解决了问题,对SQLSe... 你是不是在安装SQL Server时候发现安装界面和别人不同,并且无论如何都没有中文选项?这个问题也

java内存泄漏排查过程及解决

《java内存泄漏排查过程及解决》公司某服务内存持续增长,疑似内存泄漏,未触发OOM,排查方法包括检查JVM配置、分析GC执行状态、导出堆内存快照并用IDEAProfiler工具定位大对象及代码... 目录内存泄漏内存问题排查1.查看JVM内存配置2.分析gc是否正常执行3.导出 dump 各种工具分析4.

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

SpringBoot整合Dubbo+ZK注册失败的坑及解决

《SpringBoot整合Dubbo+ZK注册失败的坑及解决》使用Dubbo框架时,需在公共pom添加依赖,启动类加@EnableDubbo,实现类用@DubboService替代@Service,配... 目录1.先看下公共的pom(maven创建的pom工程)2.启动类上加@EnableDubbo3.实