当前位置:网站首页>Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
2022-07-03 12:39:00 【liuhaha1015】
SwipeRefreshLayout nesting RecyclerView When sliding down and up , Sometimes they conflict with each other .
First of all, the online solution is :
to RecyclerView Add slide monitor (addOnScrollListener), stay onScrolled Get the first one in item Of top, When sliding up and down top Only negative , When the first one item When fully displayed top by 0.
When top by 0 when . It means that it slides to the end , And then SwipeRefreshLayout Of Enabled Set to true......
RecyclerView Of id yes home_recyclerView,SwipeRefreshLayout Of id yes home_refresh
home_recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
LogUtils.e("onScrollStateChanged = ")
}
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
LogUtils.e("onScrolled = ")
if (recyclerView != null && recyclerView.getChildCount() > 0) {
val firstChild = recyclerView.getChildAt(0)
val firstChildPosition = if(firstChild == null) 0 else firstChild.top
LogUtils.e("onScrolled = "+ firstChild.top)
// If firstChild At the first position in the list , And top>=0, Then the drop-down refresh control is available
home_refresh.setEnabled(firstChildPosition >= 0)
}
}
})
The disadvantage is :
But this is the case RecyclerView It works only when the height of is relatively fixed , If RecyclerView Height is wrap_content The above method does not work .....recyclerView.getChildAt(0).top by 0 Whether it is the first item, Will trigger the pull-down refresh .....
My solution is :
Let's look at the layout first :
In the project SwipeRefreshLayout nesting ScrollView,ScrollView Inside RecyclerView,RecyclerView Height is wrap_content.
Above is the head of the layout ( green ), Next is the scrolling message view( Light green ),banner, And blank padding, At the bottom is RecyclerView.
The effect to be achieved is , Except for the head of the layout , The rest should be able to slide , When the news view Show them all and then make SwipeRefreshLayout start-up .

The solution is :
When RecyclerView After drawing, calculate the first item To the head of the layout ( Dark green ) Distance of y, When RecyclerView so One of the first item by 0 when , And visible first item The distance from the head is greater than or equal to the previous y when , Set again if yes SwipeRefreshLayout You can use ~
( Coordinates are used to calculate the distance )
Specific code :
var rvFirstItemDistanceHomeHead: Int = 0 // Record view When finished, the first item distance home_head Distance from the bottom
home_recyclerView.viewTreeObserver.addOnGlobalLayoutListener {
LogUtils.e("addOnGlobalLayoutListener = ")
//RecyclerView Finish drawing . Measure the first visible item distance home_head Distance from the bottom
if(rvFirstItemDistanceHomeHead == 0){
val distanceFirstItem = getDistanceFirstItem(refuseMarket)
val firstChildY = distanceFirstItem.get(0)
val headLocationY = distanceFirstItem.get(1)
if ((firstChildY - headLocationY) > 0) rvFirstItemDistanceHomeHead =
(firstChildY - headLocationY)
LogUtils.e("rvFirstItemDistanceHomeHead = " + rvFirstItemDistanceHomeHead)
}
}
home_recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
//LogUtils.e("onScrollStateChanged = " + if (newState == 0) " No scrolling " else if (newState == 1) " Dragged by the outside " else " Auto scroll ")
LogUtils.e("onScrollStateChanged = ")
// Judge the first visible item Is it the first item
val layoutManager = recyclerView.layoutManager as LinearLayoutManager
val firstCompletelyVisibleItemPosition =
layoutManager!!.findFirstCompletelyVisibleItemPosition()
LogUtils.e("onScrollStateChanged = " + firstCompletelyVisibleItemPosition)
// Judge the first visible in real time item distance home_head Distance from the bottom
val distanceFirstItem = getDistanceFirstItem(recyclerView)
val firstChildY = distanceFirstItem.get(0)
val headLocationY = distanceFirstItem.get(1)
LogUtils.e("onScrolled = " + (firstChildY - headLocationY))
// When RecyclerView so One of the first item by 0( by RecyclerView The first data of ) when , And visible first item The distance from the head is greater than or equal to the previous y when , Set to available
home_refresh.setEnabled((firstCompletelyVisibleItemPosition == 0) && (firstChildY - headLocationY >= rvFirstItemDistanceHomeHead))
}
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
LogUtils.e("onScrolled = ")
}
}) Among them getDistanceFirstItem Method is :
private fun getDistanceFirstItem(recyclerView: RecyclerView): ArrayList {
val distanceList = ArrayList()// Used to store recycleView The first is visible item At the top of the Y coordinate 、 Head view Under Of Y coordinate
if (recyclerView != null && recyclerView.getChildCount() > 0) {
val layoutManager = recyclerView.layoutManager as LinearLayoutManager
val firstCompletelyVisibleItemPosition =
layoutManager!!.findFirstCompletelyVisibleItemPosition()
val firstChild =
layoutManager.findViewByPosition(firstCompletelyVisibleItemPosition)
var firstChildY = 0//recycleView The first is visible item At the top of the Y coordinate
var headLocationY = 0// Head view Under Of Y coordinate
if (firstChild != null) {
// Get coordinates
val firstChildLocation = IntArray(2)
firstChild.getLocationInWindow(firstChildLocation)
// firstChildLocation[0]//x coordinate
firstChildY = firstChildLocation[1] // y coordinate
}
// Get coordinates
val headLocation = IntArray(2)
home_head.getLocationInWindow(headLocation)
// firstChildLocation[0]//x coordinate
headLocationY = headLocation[1] // y coordinate
distanceList.add(firstChildY)
distanceList.add(headLocationY)
}
return distanceList
}The main code is done . This is in recycleView It is also effective when the height is fixed .
in addition :
control SwipeRefreshLayout Whether it is available is in onScrollStateChanged() Method , because recycleView Height is wrap_content Of , And nested ScrollView, It won't walk when sliding onScrolled() Of .
Even if recycleView Fixed height , Only when the data display slides more than one screen onScrolled(), It can be displayed on one screen, and the sliding will not be adjusted onScrolled(), It should be like this ....
If there is something wrong, please give me some advice ~
边栏推荐
- elastic_ L02_ install
- node的ORM使用-Sequelize
- 【附下载】密码获取工具LaZagne安装及使用
- 01_ Using the concurrent tool class library, is thread safety safe
- Sword finger offer03 Repeated numbers in the array [simple]
- Openstack node address change
- Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.
- Slf4j log facade
- How to convert a decimal number to binary in swift
- 使用BLoC 构建 Flutter的页面实例
猜你喜欢

社交社区论坛APP超高颜值UI界面

Implement verification code verification

【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
![[ArcGIS user defined script tool] vector file generates expanded rectangular face elements](/img/39/0b31290798077cb8c355fbd058e4d3.png)
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements

Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?

Sword finger offer06 Print linked list from end to end

剑指Offer04. 二维数组中的查找【中等】

Xctf mobile--rememberother problem solving

低代码平台国际化多语言(i18n)技术方案

Sword finger offer07 Rebuild binary tree
随机推荐
Swift Error Handling
雲計算未來 — 雲原生
Keep learning swift
145. Post order traversal of binary tree
wpa_ cli
If you can't learn, you have to learn. Jetpack compose writes an im app (I)
elastic_ L02_ install
Dix règles de travail
云计算未来 — 云原生
111. Minimum depth of binary tree
剑指Offer07. 重建二叉树
2.7 overview of livedata knowledge points
2020-10_ Development experience set
20. Valid brackets
社交社区论坛APP超高颜值UI界面
node的ORM使用-Sequelize
Day 1 of kotlin learning: simple built-in types of kotlin
(最新版) Wifi分销多开版+安装框架
低代码平台国际化多语言(i18n)技术方案
剑指Offer04. 二维数组中的查找【中等】