我有一个SlidingPaneLayout
,里面有一个RecyclerView
和一个FragmentContainerView
,如下所示。
我想实现的是recyclerview上的“下拉刷新”功能,但我有点卡住了。我找到的唯一examples是SwipeRefreshLayout
,但我真的很想保留SlidingPaneLayout。
有什么方法可以实现这种“下拉刷新”功能吗?或者我应该接受我必须重做我的布局?
<?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)
}
}
3条答案
按热度按时间ycl3bljg1#
你绝对可以使用RecyclerView在SlidingPaneLayout中实现“下拉刷新”功能。你只需要将RecyclerView Package 在SwipeRefreshLayout中。这不会影响你的SlidingPaneLayout,你仍然可以按预期使用它。下面是你的操作方法:
1.在布局XML文件中,使用SwipeRefreshLayout Package RecyclerView:
1.在Java或Kotlin代码中,设置SwipeRefreshLayout并实现刷新侦听器:
Kotlin:
这将允许您在RecyclerView中实现“下拉刷新”功能,而无需更改SlidingPaneLayout。
laawzig22#
要在
SlidingPaneLayout
内的RecyclerView
中实现下拉刷新功能,请执行以下操作在你的活动中
向RecyclerView添加OnScrollListener,以便在RecyclerView在顶部滚动时禁用SlidingPaneLayout中的滑动手势
2guxujil3#
我给你的建议是用这个方法,我希望它对你也有效