引入:
implementation 'com.lcodecorex:tkrefreshlayout:1.0.7'
xml文件:
<?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">
<com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
android:id="@+id/trl_main_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.v4.widget.NestedScrollView
android:id="@+id/mNestView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/colorAccent"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_main_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout>
</LinearLayout>
java文件:
private void initRecyclerView(List<MainDataBean> dataBeans) {
if (mMainAdapter == null) {
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
// 默认为true
manager.setSmoothScrollbarEnabled(true);
// 允许自动测量
manager.setAutoMeasureEnabled(true);
mRecyclerView.setLayoutManager(manager);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setNestedScrollingEnabled(false);
mMainAdapter = new MainAdapter(mData);
mRecyclerView.setAdapter(mMainAdapter);
} else {
mMainAdapter.setData(mData);
}
}
public void initRefreshLayout() {
mTwinklingRefreshLayout.setEnableOverScroll(false);
mTwinklingRefreshLayout.setEnableRefresh(false);
mTwinklingRefreshLayout.setEnableLoadmore(true);
mTwinklingRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
@Override
public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
super.onLoadMore(refreshLayout);
if (mRecyclerView == null) { return; }
refreshData();
refreshLayout.finishLoadmore();
}
});
}