SwipeToLoadLayout是一个非常好用的开源控件,可以支持本身能够滑动的控件,比如ListView、ScrollView、RecyclerView、GridView,还可以支持其他WebView,Linearlayout,RelativeLayout,FrameLayout,ImageView,TextView等不能滑动的控件的刷新和加载。
网上关于SwipeToLoadLayout的使用资料有很多,自己也去研究了下源码,简单的总结下使用方法和可能遇到的坑。
源码地址https://github.com/Aspsine/SwipeToLoadLayout
核心代码是项目中的一个library
其中这里有一个坑,就是相关的控件id的名字不能随意修改,第一次使用SwipeToLoadLayout控件的小伙伴可能会比较懵逼
因为在源代码SwipeToLoadLayout中,原作者把上拉头和下拉头以及目标控件的id都写死了,所以如果随意更改id,会导致控件找不到
为了方便使用,我将这段代码注释了,觉得不能改id有点怪怪的,并增加set方法
public void setHeaderView(View headerView) {
mHeaderView = headerView;
}
public void setTargetView(View targetView) {
mTargetView = targetView;
}
public void setFooterView(View footerView) {
mFooterView = footerView;
}
这样,就可以自己设置目标和上拉下拉头,id的名字就可以随意改动了
下面贴上我在源代码基础上改动后的代码
<?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"
tools:context="demo.recyclerview.swipetoloadlayout.com.swipetoloadlayoutrecylerviewdemo.MainActivity">
<com.aspsine.swipetoloadlayout.SwipeToLoadLayout
android:id="@+id/swipeToLoadLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false" />
</com.aspsine.swipetoloadlayout.SwipeToLoadLayout>
</LinearLayout>
public class MainActivity extends AppCompatActivity implements OnRefreshListener, OnLoadMoreListener {
@BindView(R.id.swipeToLoadLayout)
SwipeToLoadLayout mSwipeToLoadLayout;
@BindView(R.id.recycler_view)
RecyclerView mRecyclerView;
private List<String> mList = new ArrayList<>();
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*不会用ButterKnife的小伙伴可以直接用findViewById()来找控件*/
ButterKnife.bind(this);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
MyAdapter mAdapter = new MyAdapter(this, mList);
mRecyclerView.setAdapter(mAdapter);
mSwipeToLoadLayout.setTargetView(mRecyclerView);
/*如果使用setRefreshHeaderView和setLoadMoreFooterView方法,则不需要在xml中写入R.layout.layout_twitter_header,R.layout.layout_classic_footer*/
mSwipeToLoadLayout.setRefreshHeaderView(LayoutInflater.from(this).inflate(R.layout.layout_twitter_header, mSwipeToLoadLayout, false));
mSwipeToLoadLayout.setLoadMoreFooterView(LayoutInflater.from(this).inflate(R.layout.layout_classic_footer, mSwipeToLoadLayout, false));
mSwipeToLoadLayout.setOnRefreshListener(this);
mSwipeToLoadLayout.setOnLoadMoreListener(this);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
if (!ViewCompat.canScrollVertically(recyclerView, 1)) {
mSwipeToLoadLayout.setLoadingMore(true);
}
}
}
});
}
@Override
public void onRefresh() {
LogT.i("正在刷新:");
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mSwipeToLoadLayout.setRefreshing(false);
}
}, 2000);
}
@Override
public void onLoadMore() {
LogT.i("加载更多数据:");
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mSwipeToLoadLayout.setLoadingMore(false);
}
}, 2000);
}
}
自己做的小demo的地址https://github.com/linqinen708/RecyclerViewSwipeToLoadLayout