You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SlidingPaneLayout中为RecyclerView实现下拉刷新

在保留SlidingPaneLayout的前提下为RecyclerView实现下拉刷新

不用替换SlidingPaneLayout,直接把RecyclerView嵌套进SwipeRefreshLayout就能实现下拉刷新,完全不用调整整体布局结构,具体改法如下:

修改布局文件

把原有的RecyclerView用SwipeRefreshLayout包裹,作为SlidingPaneLayout的第一个子View即可:

<?xml version="1.0" encoding="utf-8"?>
<androidx.slidingpanelayout.widget.SlidingPaneLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/sliding_pane_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FeedBaseFragment">

    <!-- 用SwipeRefreshLayout包裹RecyclerView -->
    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="550dp"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:padding="8dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/detail_container"
        android:layout_width="300dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:name="com.example.test.FeedDetailsFragment" />
</androidx.slidingpanelayout.widget.SlidingPaneLayout>

修改Fragment代码

在onViewCreated中初始化SwipeRefreshLayout并设置刷新逻辑:

class FeedBaseFragment : Fragment() {
    private lateinit var currentView: View
    private val feedItemsViewModel: FeedItemsViewModel by activityViewModels()
    // 新增SwipeRefreshLayout实例
    private lateinit var swipeRefreshLayout: SwipeRefreshLayout

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_feed_base, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        currentView = view
        super.onViewCreated(currentView, savedInstanceState)
        
        val slidingPaneLayout = currentView.findViewById<SlidingPaneLayout>(R.id.sliding_pane_layout)
        slidingPaneLayout.lockMode = SlidingPaneLayout.LOCK_MODE_LOCKED

        // 初始化SwipeRefreshLayout并设置监听
        swipeRefreshLayout = currentView.findViewById(R.id.swipe_refresh_layout)
        swipeRefreshLayout.setOnRefreshListener {
            // 执行刷新逻辑
            refreshFeedItems()
        }

        // 初始化RecyclerView适配器
        val adapter = FeedItemsAdapter {
            feedItemsViewModel.updateCurrentFeedItem(it)
            slidingPaneLayout.openPane()
        }
        currentView.findViewById<RecyclerView>(R.id.recycler_view).adapter = adapter
        adapter.submitList(feedItemsViewModel.feedItemsData)
    }

    // 刷新逻辑示例,替换为你的实际数据更新逻辑
    private fun refreshFeedItems() {
        feedItemsViewModel.refreshFeedData().observe(viewLifecycleOwner) { newList ->
            val adapter = currentView.findViewById<RecyclerView>(R.id.recycler_view).adapter as FeedItemsAdapter
            adapter.submitList(newList)
            // 刷新完成后停止动画
            swipeRefreshLayout.isRefreshing = false
        }
    }
}

原理说明

SwipeRefreshLayout和SlidingPaneLayout可以兼容嵌套,只要将需要下拉刷新的RecyclerView单独放在SwipeRefreshLayout内部,就不会干扰SlidingPaneLayout的滑动、锁定等原有功能,完全不需要替换掉SlidingPaneLayout。

内容的提问来源于stack exchange,提问作者Mathlight

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 10:54:56