Recyclerview加载更多

布局文件

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.xml

1
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.xml

1
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
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
75
76
77
78
79
80
public class TestAdapter extends RecyclerView.Adapter {
private Context mContext;
private List<DynamicModel> mVideoList;
private int TYPE_IMAGE = 0;
private int TYPE_FOOTER = 2;

public TestAdapter(Context context, List<DynamicModel> videoList) {
mContext = context;
mVideoList = videoList;
}

@Override
public int getItemViewType(int position) {
if (position + 1 == getItemCount()) {
return TYPE_FOOTER;
}
return TYPE_IMAGE;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

RecyclerView.ViewHolder holder = null;

if (viewType == TYPE_IMAGE) {
View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_with_image, parent, false);
holder = new ImageHolder(itemView);
} else if (viewType == TYPE_FOOTER) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_foot, parent,
false);
return new FootViewHolder(view);
}

return holder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ImageHolder) {
((ImageHolder) holder).bindView(mVideoList.get(position));
}
}


@Override
public int getItemCount() {
return mVideoList.size() == 0 ? 0 : mVideoList.size() + 1;
}

private class ImageHolder extends RecyclerView.ViewHolder {

private final ImageView imgAvatar;
private final TextView tvName;

public ImageHolder(View itemView) {
super(itemView);
imgAvatar = ((ImageView) itemView.findViewById(R.id.img_avatar));
tvName = ((TextView) itemView.findViewById(R.id.tv_name));
}

private void bindView(DynamicModel item) {

if (item.getUsers().size() > 0) {
Glide.with(mContext).load(AppConfig.GetImageUrl(item.getUsers().get(0).getAvatarImage())).transform(new GlideCircleTransform(mContext)).into(imgAvatar);
tvName.setText(item.getUsers().get(0).getUserName());
} else {
Glide.with(mContext).load(R.drawable.default_seyo).transform(new GlideCircleTransform(mContext)).into(imgAvatar);
tvName.setText("seyo+");
}
}
}


private class FootViewHolder extends RecyclerView.ViewHolder {

public FootViewHolder(View itemView) {
super(itemView);
}
}
}

Fragment

1
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
75
public 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。

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×

keyboard_arrow_up 回到顶端