Tuesday, 18 April 2023

RecyclerView within SlidingPaneLayout, how to implement pull down to update

I have a SlidingPaneLayout with a RecyclerView and a FragmentContainerView inside of it, as shown below.

What I'm trying to implement, is the "pull down to refresh" functionality on the recyclerview, but I'm a bit stuck. The only examples that I find, are with SwipeRefreshLayout, but I'd really like to keep the SlidingPaneLayout.

Is there any way to implement this "pull down to refresh" functionality? Or should I accept that I have to redo my layout?

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

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

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

class FeedBaseFragment : Fragment() {
    private lateinit var currentView: View

    private val feedItemsViewModel: FeedItemsViewModel by activityViewModels()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        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

        // Initialize the adapter and set it to the RecyclerView.
        val adapter = FeedItemsAdapter {
            feedItemsViewModel.updateCurrentFeedItem(it)
            slidingPaneLayout.openPane()
        }
        currentView.findViewById<RecyclerView>(R.id.recycler_view).adapter = adapter
        adapter.submitList(feedItemsViewModel.feedItemsData)
    }
}


from RecyclerView within SlidingPaneLayout, how to implement pull down to update

No comments:

Post a Comment