我是靠谱客的博主 糟糕夏天,这篇文章主要介绍Android万能下拉刷新,上拉加载。支持自定义样式!!!,现在分享给大家,希望可以做个参考。

打造Android万能的下拉刷新,上拉加载,支持所有控件,一键集成,简单明了,支持自定义,丰富接口,摆脱臃肿依赖。一次就爱上它了


资源文件下载地址: 下载地址


详细介绍:

最近在看自己的项目时发现,这个通用刷新控件有些生疏了,它好用是真的没话说,可以用在任意控件上,比如listview、textview....而且是解耦合的,非常简单进行

自定义自己的刷新加载样式。

这是博主的一些介绍:   https://github.com/anzewei/NestRefreshLayout

然而,他介绍的并不详细,如何使用自定义的刷新加载样式呢,

我这里再细细的讲一下

首先看我的布局文件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.nestrefreshlayouts.ListView_Activity"> <!-- 刷新加载控件直接包裹再listview的外部,这就是为什么能运用所有的view上的原因 --> <!-- 我使用的是自定义的头部和尾部 --> <cn.appsdream.nestrefresh.normalstyle.NestRefreshLayout android:id="@+id/freshLayout" android:layout_width="match_parent" android:layout_height="match_parent" app:footerNestLayout="@layout/layout_footer" app:headerNestLayout="@layout/layout_header"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </cn.appsdream.nestrefresh.normalstyle.NestRefreshLayout> </LinearLayout>

那么问题来了,自定义文件要怎么写呢,

首先你要自定义一个布局,这里布局随便,可以是LinearLayout  、 RelativeLayout 等等

但是你必须重写这个布局,并且实现implements NestRefreshLayout.LoaderDecor这个接口,并实现它的方法

这个自定义布局其他地方不用动

这里与LinearLayout为例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class MyFooterLinearLayout extends LinearLayout implements NestRefreshLayout.LoaderDecor{ public MyFooterLinearLayout(Context context) { super(context); } public MyFooterLinearLayout(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public MyFooterLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public MyFooterLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public void scrollRate(int y) { } @Override public void setState(int state) { } }
看了他的源码知道,这个用于刷新的setState()方法是有很多种状态的,你可以根据不同的状态做不同的操作

具体状态如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override public void setState(int state) { if (state == STATE_READY) { setText("自定义:松开加载更多"); } else if (state == STATE_REFRESHING) { setText("自定义:加载中"); } else if (state == STATE_NORMAL) { setText("自定义:加载更多"); } else if (state == STATE_ALL) { setText("自定义:没有更多了"); } else { setText(""); } }

好了,如何引用我们的布局呢

你新建一个布局文件layout_header即可

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?> <com.example.nestrefreshlayouts.MyFooterLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--头部--> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:background="#00ffff" android:text="我是头部" /> </com.example.nestrefreshlayouts.MyFooterLinearLayout>

最后就是这个控件的引用了

复制代码
1
2
app:footerNestLayout="@layout/layout_footer" app:headerNestLayout="@layout/layout_header"

OK,这样就大工告成了

复制代码
1
2
3
4
freshLayout = (AbsRefreshLayout) findViewById(R.id.freshLayout); freshLayout.setOnLoadingListener(this); freshLayout.setPullLoadEnable(true); freshLayout.setPullRefreshEnable(true);
这里要注意下,强转类型是
复制代码
1
AbsRefreshLayout

实现了下拉刷新和上啦加载后

通过

复制代码
1
freshLayout.onLoadFinished();

来关闭头部和尾部的显示。

以后就用这个刷新的框架了。

万能刷新的这篇博客也要好好看看:https://github.com/lcodecorex/TwinklingRefreshLayout



//-------end---------

最后

以上就是糟糕夏天最近收集整理的关于Android万能下拉刷新,上拉加载。支持自定义样式!!!的全部内容,更多相关Android万能下拉刷新内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(96)

评论列表共有 0 条评论

立即
投稿
返回
顶部