Android中下拉刷新PullRefreshLayout

有时我们页面中没有用到ListView、RecycleView,但是页面中有数据是要及时刷新的,这个时候我觉得用PullRefreshLayout正合适。网上博客有很多,我在这里总结一下,方便日后使用,也希望能帮到他人。

GitHub:https://github.com/baoyongzhang/android-PullRefreshLayout

 一、首先添加依赖

dependencies {
    implementation 'com.baoyz.pullrefreshlayout:library:1.2.0'
}

二、布局

<com.baoyz.widget.PullRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

	<!-- ListView、ScrollView、RecyclerView、Other -->
	
</com.baoyz.widget.PullRefreshLayout>

三、使用方式

        PullRefreshLayout layout = (PullRefreshLayout) findViewById(R.id.prl_view);
        layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //修改数据的代码,最后记得填上此行代码
                layout.setRefreshing(false);
            }
        });       

四、改变样式

       1.更改刷新风格,里面有五种风格的使用MATERIALCIRCLESWATER_DROPRINGSMARTISAN

       1.1 代码中使用方式:

layout.setRefreshStyle(PullRefreshLayout.STYLE_CIRCLES);

        1.2布局中使用方式:

<com.baoyz.widget.PullRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:refreshType="water_drop">
	
</com.baoyz.widget.PullRefreshLayout>

         2.改变配色方案。

         2.1 在代码中,调用setColorSchemeColors方法。int数组长度必须为4。

layout.setColorSchemeColors(new int[]{R.color.colorAccent,R.color.colorPrimary,R.color.colorPrimaryDark,R.color.FD7538});
        

               对于Smartisan样式,它只有一种颜色,可以调用'setColor'方法,设置一种颜色。

layout.setColor(R.color.colorAccent);

         2.2 在布局中添加属性

<com.baoyz.widget.PullRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:refreshColors="@array/scheme_colors"
    app:refreshColor="@color/one_color">
	
</com.baoyz.widget.PullRefreshLayout>

        3. 可以自定义刷新样式,调用setRefreshDrawable()方法以使用自定义刷新drawable,方式可以看GitHub。

猜你喜欢

转载自blog.csdn.net/qq_40441190/article/details/84951642