如题 版本:2.9.25 我使用了一个包装类来封装了一下,布局使用Framlayout包裹了RecyclerView和Swiperefreshlayout, 1.布局的代码如下 ` <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipreLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
2.包装类,对RecyclerView和SwipeRefreshLayout进行了封装
MyRecyclerView extends FrameLayout implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener `
3.包装类的初始化如下
private void initView(AttributeSet attrs, Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_my_refresh, this, true);
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipreLayout);
recycler = (RecyclerView) view.findViewById(R.id.recycler);
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
recycler.setLayoutManager(new LinearLayoutManager(context));
}
public void setAdapter(A adapter) {
mAdapter = adapter;
mAdapter.setOnLoadMoreListener(this, recycler);
recycler.setAdapter(adapter);
mAdapter.setLoadMoreView(new CustomLoadMoreView());
}
` 4.下拉刷新和上拉加载的逻辑如下
@Override
public void onRefresh() {
mOnRefreshListener.onPullDownToRefresh();
}
@Override
public void onLoadMoreRequested() {
swipeRefreshLayout.setEnabled(false);
if (mAdapter.getData().size() < PAGE_SIZE) {
mAdapter.loadMoreEnd(false);
} else {
if (mCurrentCounter >= TOTAL_COUNTER) {
mAdapter.loadMoreEnd(true);//true is gone,false is visible
} else {
mOnRefreshListener.onLoadMoreRefresh();
mCurrentCounter = mAdapter.getData().size();
mAdapter.loadMoreComplete();
}
swipeRefreshLayout.setEnabled(true);
}
}
`
5.adapter的布局如下 ` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:layout_gravity="center" android:orientation="horizontal">
<TextView
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="@+id/tv_stock_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TEST1" />
<TextView
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="@+id/tv_stsock_code"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TEST2" />
<TextView
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="@+id/tv_stock_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TEST3" />
`