Android - 原生下拉刷新功能(SwipeRefreshLayout)

  • 使用场景

鉴于公司内部要求,禁止使用第三方控件,所以使用android原生刷新控件

  • 遇到的问题

1.刷新控件SwipeRefreshLayout与首页控件CoordinatorLayout产生冲突

具体实现

一、xml代码(未解决冲突之前的代码)

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".refresh.RefreshActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/refresh_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="测试下拉加载" />
    </LinearLayout>


</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

二、java代码

public class RefreshActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.main_refresh)
    SwipeRefreshLayout mMainRefresh;
    @BindView(R.id.refresh_btn)
    Button mRefreshBtn;
    private boolean isRefresh = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refresh);
        ButterKnife.bind(this);

        mMainRefresh.setOnRefreshListener(this);
    }

    @Override
    public void onRefresh() {
        if (!isRefresh) {
            isRefresh = true;
            mRefreshBtn.setText("刷新成功");
            mMainRefresh.setRefreshing(false);
            isRefresh = false;
        }
    }
}

三、解决滑动冲突xml代码修改

  • 修改方式

将布局内的LinearLayout替换成CoordinatorLayout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".refresh.RefreshActivity">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/main_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
                
        <!--这个地方的布局文件换成对应的CoordinatorLayout即可--> 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <Button
                android:id="@+id/refresh_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="测试下拉加载" />
        </LinearLayout>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</FrameLayout>
// 判断AppBarLayout是否为展开状态,如果状态为展开,则进行下拉刷新动作
// 添加APPBarLayout状态监听
mAppBar.addOnOffsetChangedListener(mOnOffsetChangedListener);

private AppBarLayout.OnOffsetChangedListener mOnOffsetChangedListener = new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
            if (i == 0) {
                if (mState != ToolbarLayoutState.EXPANDED) {
                    // 展开
                    mState = ToolbarLayoutState.EXPANDED;
                    mRefreshLayout.setEnabled(true);
                }
            } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
                if (mState != ToolbarLayoutState.COLLAPSED) {
                    //  关闭
                    mState = ToolbarLayoutState.COLLAPSED;
                    mRefreshLayout.setEnabled(false);
                }
            } else {
                if (mState != ToolbarLayoutState.INTERNEDIATE) {
                    mState = ToolbarLayoutState.INTERNEDIATE;
                }
            }
        }
    };
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。