代码拉取完成,页面将自动刷新
继承自ViewGroup的自定义布局,主要特点是可以在任意方向添加滑动响应视图,用来做下拉刷新和追加数据等功能非常方便,提供SlidingView接口方便扩展自己喜欢的响应视图,欢迎提供更多自定义响应视图。
在根模块的build.gradle
的适当位置添加以下代码:
allprojects {
repositories {
...
mavel { url 'https://jitpack.io/' }
}
}
在模块的build.gradle
文件中添加以下代码:
implementation("cn.numeron:slide-refresh-layout:latest_version")
layout_direction
和layout_slide_type
属性必不可少<?xml version="1.0" encoding="utf-8"?>
<cn.numeron.sliderefreshlayout.SlideRefreshLayout 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/slide_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<cn.numeron.sliderefreshlayout.SlidingRefreshView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_direction="top"
app:layout_slide_type="refresh" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<cn.numeron.sliderefreshlayout.SlidingAppendView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_direction="bottom"
app:layout_slide_type="append" />
</cn.numeron.sliderefreshlayout.SlideRefreshLayout>
2.在java/kotlin
代码中添加监听事件:
```kotlin
slideRefreshLayout.onRefreshListener = SlideRefreshLayout.OnRefreshListener {
//do your refresh operation
}
slideRefreshLayout.onAppendListener = SlideRefreshLayout.OnAppendListener {
//do your append operation
}
```
实现你自己的SlidingView
class SlidingTextView @JvmOverloads constructor(
context: Context,
attr: AttributeSet? = null,
defStyleAttr: Int = android.R.attr.textViewStyle
) : AppCompatTextView(context, attr, defStyleAttr), SlidingView {
/**
* 当被主动滑动时调用,会调用多次
* @param type [Type] 因滑动操作需要响应的类型,[Type.Refresh]或者[Type.Append]
* @param threshold [Int] 响应距离的阈值
* @param distance [Int] 总共滑动了多少距离
*/
override fun onSliding(type: Type, threshold: Int, distance: Int) {
text = if (distance < threshold) "下拉刷新" else "松开刷新"
}
/**
* 在复位过程中调用,会调用多次
* @param type [Type] 因滑动操作需要响应的类型,[Type.Refresh]或者[Type.Append]
* @param distance [Int] 移动了多少距离
*/
override fun onRestoring(type: Type, distance: Int) = Unit
/**
* 在刷新或追加的操作开始时调用
* @param type [Type] 因滑动操作需要响应的类型,[Type.Refresh]或者[Type.Append]
*/
override fun onStartSliding(type: Type) {
text = "正在刷新"
}
/**
* 在刷新或追加的操作停止时调用
* @param type [Type] 因滑动操作需要响应的类型,[Type.Refresh]或者[Type.Append]
*/
override fun onFinishSliding(type: Type) = Unit
}
在xml
中声明布局:
<com.numeron.sliderefreshlayout.SlideRefreshLayout ...
<Your view implements SlidingView
android:layout_width="match_parent"
android:layout_height="64dp"
app:layout_direction="top"
app:layout_slide_type="refresh"
... />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
... />
<Your view implements SlidingView
android:layout_width="match_parent"
android:layout_height="64dp"
app:layout_direction="top"
app:layout_slide_type="append"
... />
</com.numeron.sliderefreshlayout.SlideRefreshLayout>
竖向列表下滑刷新和上滑加载:
横向列表右滑刷新和左滑加载:
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。