判断Listview滑到顶部的最精准方案,解决Listview设置EmptyView与SwipeRefreshLayout冲突

本文主要是介绍判断Listview滑到顶部的最精准方案,解决Listview设置EmptyView与SwipeRefreshLayout冲突,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

故事发生的背景

SwipeRefreshLayout是谷歌自家控件,提供下拉刷新的功能。然而这个控件简单易用的同时也有一个令人头疼的缺点,那就是它里面只能包含一个子View!有一天,需求来了,需要在为Listview添加EmptyView和下拉刷新,同时当显示EmptyView时也要求有下拉刷新。

尝试与探索

大家都知道,设置EmptyView需要把它放在一个容器内。这还不简单,SwipeRefreshLayout不就很好的容器吗?

    <android.support.v4.widget.SwipeRefreshLayoutandroid:id="@+id/refrashLayout"android:layout_width="match_parent"android:layout_height="match_parent"><ListViewandroid:id="@+id/listview"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_gravity="center_horizontal"android:clipToPadding="false"android:divider="#E1E6E9"android:dividerHeight="20dp"android:padding="20dp"android:scrollbars="none" /><ScrollViewandroid:id="@+id/emptyView"android:layout_width="match_parent"android:layout_height="match_parent"android:fillViewport="true"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="Nothing here..." /></ScrollView></android.support.v4.widget.SwipeRefreshLayout>

故事就这样结束了吗?NO,这样写了后果是,当Listview没有数据显示的时候,EmptyView没有显示出来。

后来,我Google一下,在stackoverflow上看到很多解决方案,找到几种比较靠谱的方案,有一些说需要重写自定义Listview,个人觉得不够优雅就不列举出来了。

方案一:

把EmptyView放到SwipeRefreshLayout外面,这似乎是一个很完美的方案,同时也是最简单的,然而,这样的话EmptyView没有下拉刷新了。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#E1E6E9"android:orientation="vertical"><ScrollView
        android:id="@+id/emptyView"android:layout_width="match_parent"android:layout_height="match_parent"android:visibility="gone"android:fillViewport="true"><TextView
            android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="Nothing here..." /></ScrollView><android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/refrashLayout"android:layout_width="match_parent"android:layout_height="match_parent"><ListView
            android:id="@+id/listview"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_gravity="center_horizontal"android:clipToPadding="false"android:divider="#E1E6E9"android:dividerHeight="20dp"android:padding="20dp"android:scrollbars="none" /></android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

方案二

为了给EmptyView加下拉刷新,在把方案一中的EmptyView套上另一个SwipeRefreshLayout。这样的话就需要两份处理SwipeRefreshLayout的代码了,个人觉得比较麻烦,不够优雅,但也不失为一个完整的解决方案。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#E1E6E9"android:orientation="vertical"><android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/emptyView"android:layout_width=""android:layout_height=""><TextView
            android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="Nothing here..." /></android.support.v4.widget.SwipeRefreshLayout><android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/refrashLayout"android:layout_width="match_parent"android:layout_height="match_parent"><ListView
            android:id="@+id/listview"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_gravity="center_horizontal"android:clipToPadding="false"android:divider="#E1E6E9"android:dividerHeight="20dp"android:padding="20dp"android:scrollbars="none" /></android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

方案三

我一向不喜欢简单粗暴,推崇优雅的解决方法。既然你SwipeRefreshLayout只能包含一个子View,那么我用FrameLayout wrap them all together不就行了? 为什么用FrameLayout? 因为FrameLayout相比于其他几个开销更低。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#E1E6E9"android:orientation="vertical"><android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/refrashLayout"android:layout_width="match_parent"android:layout_height="match_parent"><FrameLayout
            android:layout_width="match_parent"android:layout_height="match_parent"><ListView
                android:id="@+id/listview"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="20dp"android:clipToPadding="false"android:layout_gravity="center_horizontal"android:scrollbars="none"android:divider="#E1E6E9"android:dividerHeight="20dp" /><ScrollView
                android:id="@+id/emptyView"android:layout_width="match_parent"android:layout_height="match_parent"android:fillViewport="true"><TextView
                    android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="Nothing here..." /></ScrollView></FrameLayout></android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

很合理的方案,然而故事并没有这么轻易结束,运行效果如图。
这里写图片描述
LIstview没有到达顶部下拉就会刷新!显然这个bug是无法接受的。我们需要在listview没有到达顶部的时候禁用SwipeRefreshLayout,在listview到达顶部的时候恢复SwipeRefreshLayout,就可以解决这个问题了。显然我们需要判断listview是否到达顶部的方法。

判断Listview滑到顶部的方案

我们网上一搜索,方法都是前篇一律的方案一。

方案一:简洁而粗糙

listview.setEmptyView(emptyView);
//防止SwipeRefreshLayout过早触发
listview.setOnScrollListener(new AbsListView.OnScrollListener() {@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {if (firstVisibleItem==0)refrashLayout.setEnabled(true);else refrashLayout.setEnabled(false);}
});

我天真地以为故事真的这样结束了,然而….
这里写图片描述
显然这是最简单的方案了,但却不能精确地判断是否真的滑到顶部!对于一些item高度比较大时,用这种方法判断似乎毫无意义。

终极方案

想到这个方法,我还是从Listview源码里面找到启发(源码真是最好的老师)。当Listview捕捉到ACTION_MOVE滑动动作时会调用trackMotionScroll方法,在里面有这样一段代码,判断子view是否移出屏幕。

if(down){  //手指向下滑动final int top = listPadding.top - incrementalDeltaY;for (int i = 0; i < childCount; i++) {final View child = getChildAt(i);if (child.getBottom() >= top) {break;} else {  //子View已经移出了屏幕count++;int position = firstPosition + i;if (position >= headerViewsCount && position < footerViewsStart) {mRecycler.addScrapView(child);}}}}else{final int bottom = getHeight() - listPadding.bottom - incrementalDeltaY;for (int i = childCount - 1; i >= 0; i--) {final View child = getChildAt(i);if (child.getTop() <= bottom) {break;} else {start = i;count++;int position = firstPosition + i;if (position >= headerViewsCount && position < footerViewsStart) {mRecycler.addScrapView(child);}}}}

那么给listview设置监听器,只要listview的第一个item的顶部y坐标值等于listview的顶部y坐标值表明listview到达了顶部
下面就show you the code!

        listview.setEmptyView(emptyView);listview.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_MOVE) {Log.d("Measure","listview.getListPaddingTop():"+listview.getListPaddingTop()+" listview.getTop():"+listview.getTop()+"listview.getChildAt(0).getTop():"+listview.getChildAt(0).getTop());if (listview.getFirstVisiblePosition() == 0 &&listview.getChildAt(0).getTop() >= listview.getListPaddingTop()) {refrashLayout.setEnabled(true);Log.d("TAG", "reach top!!!");}else refrashLayout.setEnabled(false);}return false;}});

这里写图片描述
EmptyView:
这里写图片描述

在情人节写代码,写博客,其实我是拒绝的~~不说了,完整代码已上传至Github,项目当中其他类不用管,相关代码在MainActivity中。

这篇关于判断Listview滑到顶部的最精准方案,解决Listview设置EmptyView与SwipeRefreshLayout冲突的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

如何解决mmcv无法安装或安装之后报错问题

《如何解决mmcv无法安装或安装之后报错问题》:本文主要介绍如何解决mmcv无法安装或安装之后报错问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mmcv无法安装或安装之后报错问题1.当我们运行YOwww.chinasem.cnLO时遇到2.找到下图所示这里3.

浅谈配置MMCV环境,解决报错,版本不匹配问题

《浅谈配置MMCV环境,解决报错,版本不匹配问题》:本文主要介绍浅谈配置MMCV环境,解决报错,版本不匹配问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录配置MMCV环境,解决报错,版本不匹配错误示例正确示例总结配置MMCV环境,解决报错,版本不匹配在col

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

Feign Client超时时间设置不生效的解决方法

《FeignClient超时时间设置不生效的解决方法》这篇文章主要为大家详细介绍了FeignClient超时时间设置不生效的原因与解决方法,具有一定的的参考价值,希望对大家有一定的帮助... 在使用Feign Client时,可以通过两种方式来设置超时时间:1.针对整个Feign Client设置超时时间

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

Java进行文件格式校验的方案详解

《Java进行文件格式校验的方案详解》这篇文章主要为大家详细介绍了Java中进行文件格式校验的相关方案,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、背景异常现象原因排查用户的无心之过二、解决方案Magandroidic Number判断主流检测库对比Tika的使用区分zip

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)的解决方法

《mysql出现ERROR2003(HY000):Can‘tconnecttoMySQLserveron‘localhost‘(10061)的解决方法》本文主要介绍了mysql出现... 目录前言:第一步:第二步:第三步:总结:前言:当你想通过命令窗口想打开mysql时候发现提http://www.cpp

SpringBoot启动报错的11个高频问题排查与解决终极指南

《SpringBoot启动报错的11个高频问题排查与解决终极指南》这篇文章主要为大家详细介绍了SpringBoot启动报错的11个高频问题的排查与解决,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一... 目录1. 依赖冲突:NoSuchMethodError 的终极解法2. Bean注入失败:No qu

PyCharm如何设置新建文件默认为LF换行符

《PyCharm如何设置新建文件默认为LF换行符》:本文主要介绍PyCharm如何设置新建文件默认为LF换行符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录PyCharm设置新建文件默认为LF换行符设置换行符修改换行符总结PyCharm设置新建文件默认为LF