布局文件1
2
3
4
5
6
7
8
9
10
11
12<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_refresh"
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.support.v4.widget.SwipeRefreshLayout>
普通 item_with_image.xml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<ImageView
android:id="@+id/img_avatar"
android:layout_width="40dp"
android:layout_height="40dp"
tools:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:textColor="@color/text_black"
android:textSize="15sp"
tools:text="seyo+"/>
</LinearLayout>
底部 item_foot.xml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:orientation="horizontal">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="6dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/loading"/>
</LinearLayout>
1 | public class TestAdapter extends RecyclerView.Adapter { |
Fragment1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75public void initView(
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(layoutManager);
adapter = new VideoAdapter(getActivity(), mList);
mRecyclerView.setAdapter(adapter);
mRefreshLayout = ((SwipeRefreshLayout) view.findViewById(R.id.view_refresh));
mRefreshLayout.setOnRefreshListener(this);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState){
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == adapter.getItemCount()) {
boolean isRefreshing = mRefreshLayout.isRefreshing();
if (isRefreshing) {
adapter.notifyItemRemoved(adapter.getItemCount());
return;
}
if (!isLoading) {
isLoading = true;
loadData();
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
lastVisibleItemPosition = layoutManager.findLastCompletelyVisibleItemPosition();
}
});
)
@Override
public void onRefresh() {
mList.clear();
loadData();
}
private void loadData() {
NetService service = new NetService();
service.getDynamicList(0, AppConfig.TYPE_RECOMMEND, AppConfig.PAGESIZE,curIndex, new Observer<DetailList<DynamicModel>>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
MethodUtils.LoadingDialog(getContext(), "正在加载");
}
@Override
public void onNext(@NonNull DetailList<DynamicModel> list) {
mList.addAll(list.getData());
adapter.notifyDataSetChanged();
}
@Override
public void onError(@NonNull Throwable e) {
MethodUtils.loadingDialogDismiss();
ToastUtils.showMessage(getContext(), "网络异常,请求失败");
mRefreshLayout.setRefreshing(false);
isLoading = false;
if (adapter.getItemCount() > 0) {
adapter.notifyItemRemoved(adapter.getItemCount());
}
}
@Override
public void onComplete() {
mRefreshLayout.setRefreshing(false);
isLoading = false;
MethodUtils.loadingDialogDismiss();
if (adapter.getItemCount() > 0) {
adapter.notifyItemRemoved(adapter.getItemCount());
}
}
});
}
挺简单的,主要就是多布局,当滑动到最后一个 item 时,让他绑定 item_foot.xml 布局。然后在加载导数据之后 adapter.notifyItemRemoved(adapter.getItemCount()); 去掉最后 foot。