Swiperefreshlayout下拉刷新时禁止滑动,以及禁止内嵌的listview滑动。亲测可用!

本文介绍了一种解决ListView在使用SwipeRefreshLayout时出现的下拉刷新与列表滑动冲突的方法。通过在FrameLayout中嵌套SwipeRefreshLayout和ListView,并利用LinearLayout抢夺焦点的方式阻止滑动,有效解决了这一难题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      从事安卓开发也有三年了,一直在借鉴别人的东西,这还是第一次写博客。写的不好敬请见谅!

     最近遇到一个需求,listview下拉刷新,以及上滑加载等。 网上找了许多参考资料,最终采用的是Swiperefreshlayout中嵌套listview。分页加载数据、上滑加载都搞定了,卡在下拉刷新时view跟着下移,以及下拉刷新时listview还可滑动导致的问题。

    下拉刷新view跟着下移通过自定义Swiperefreshlayout最终解决了,但是下拉刷新时禁止listview滑动遇到了关卡。网上搜索了许多资料,有的说setEnabled(false),也有的说自定义listview拦截事件,都试过了,均无效。(也许是我个人没写正确)

    今天突然想到,让listview失去焦点是不是可以呢?找了一下,单纯的属性好像是没有的,于是在外面套一层FrameLayout,里面嵌套了一个Swiperefreshlayout,与一个linearlayout(linearlayout中有一个edittext)然后Swiperefreshlayout中嵌套listview。初始的时候默认linearlayout不可见。当你需要禁止listview滑动或者Swiperefreshlayout滑动时设置linearlayout可见。这时候LinearLayout布局就将焦点拿走了。Swiperefreshlayout与listview均不可滑动!就解决了!一直借鉴别人的东西,脑子都生锈了,以后要多多写博客。希望对大家有用,下面贴上布局代码:

<?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">

    <com.nankang.gis.landpatrol.view.CustomSwiperefreshlayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/messageList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            android:cacheColorHint="@color/color_transparent"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:fadingEdge="none"
            android:isScrollContainer="false"
            android:listSelector="@color/color_transparent"
            android:overScrollMode="never"
            android:scrollbars="none" />
    </com.nankang.gis.landpatrol.view.CustomSwiperefreshlayout>

    <LinearLayout
        android:id="@+id/layout_listview_not_scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="gone">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</FrameLayout>
希望对大家有所帮助,转载请注明:https://blog.youkuaiyun.com/qq_28189795/article/details/80235277

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值