RecyclerView 无限循环

RecyclerView 横向左右无限滑动

实现相对而言无限个Item(无限个数据)

只需要在RecyclerView.Adapter的方法中

1
2
3
4
@Override
public int getItemCount() {
return Integer.MAX_VALUE;
}

实现第一次显示的时候, 就可以左滑

在添加完数据后调用RecyclerView.scrollToPosition(),将RecyclerView当前显示的首个Item的position设置为比较大的值。

1
mRecyclerView.scrollToPosition(mPersonAdapter.getItemRawCount() * 500);

滑动的时候, 一个一个的滑动(类似ViewPager的效果)

1
new PagerSnapHelper().attachToRecyclerView(mRecyclerView);

全篇代码

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.zy.policyplatform.R;
import com.zy.policyplatform.bean.Policy;

import java.util.ArrayList;
import java.util.List;

/**
* <pre>
* author : Haitao
* e-mail : http://blog.nhtzj.com
* time : 2018/2/25
* desc : 牛人榜单 横向无限滑动
* version: 2.0
* </pre>
*/
public class ProfitPersonView extends LinearLayout {

private RecyclerView mRecyclerView;
private PersonAdapter mPersonAdapter;

private OnClickListener mItemClickListener;
private OnItemClickListener onItemClickListener;

public ProfitPersonView(Context context) {
super(context);
init(context);
}

public ProfitPersonView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}

public ProfitPersonView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public ProfitPersonView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}

private void init(Context context) {
setOrientation(LinearLayout.VERTICAL);
LayoutInflater.from(context).inflate(R.layout.view_home_person, this, true);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
DividerItemDecoration itemDecoration = new DividerItemDecoration(context, DividerItemDecoration.HORIZONTAL);
itemDecoration.setDrawable(ContextCompat.getDrawable(context, R.drawable.shape_recycleview_divider_vertical));
mRecyclerView.addItemDecoration(itemDecoration);
mPersonAdapter = new PersonAdapter(context);
mRecyclerView.setAdapter(mPersonAdapter);

initListener();
}

private void initListener() {
mItemClickListener = new OnClickListener() {
@Override
public void onClick(int position, long itemId) {
if (onItemClickListener != null) {
onItemClickListener.onItemClick(position % mPersonAdapter.getItemRawCount(), itemId);
}
}

};

}

public void setDatas(List<Policy> datas) {
if (mPersonAdapter == null) {
return;
}
mPersonAdapter.clear();
if (datas != null && datas.size() > 0) {
mPersonAdapter.addAll(datas);
}
mRecyclerView.scrollToPosition(mPersonAdapter.getItemRawCount() * 500);

}

private class PersonAdapter extends RecyclerView.Adapter {

private List<Policy> mItems;
private Context mContext;
private LayoutInflater mInflater;

public PersonAdapter(Context context) {
mContext = context;
mInflater = LayoutInflater.from(context);
mItems = new ArrayList<>();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ItemViewHolder viewHolder = new ItemViewHolder(mInflater.inflate(R.layout.item_home_person, parent, false));
viewHolder.itemView.setTag(viewHolder);
viewHolder.itemView.setOnClickListener(mItemClickListener);
return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// onBindLoopViewHolder(holder, position % getItemRawCount());
}

@Override
public int getItemCount() {
int rawCount = getItemRawCount();
if (rawCount > 0) {
return Integer.MAX_VALUE;
}
return 0;
}

/**
* 真实数据的大小
*/
public int getItemRawCount() {
return mItems == null ? 0 : mItems.size();
}



public void addAll(List<Policy> items) {
if (items != null) {
this.mItems.addAll(items);
notifyItemRangeInserted(this.mItems.size(), items.size());
}
}

public final void clear() {
this.mItems.clear();
notifyDataSetChanged();
}



private class ItemViewHolder extends RecyclerView.ViewHolder {
public ItemViewHolder(View itemView) {
super(itemView);
}
}
}

/**
* 可以共用同一个listener,相对高效
*/
public abstract class OnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
RecyclerView.ViewHolder holder = (RecyclerView.ViewHolder) v.getTag();
onClick(holder.getAdapterPosition(), holder.getItemId());
}

public abstract void onClick(int position, long itemId);
}

/**
*外部调用,添加点击回调
*/
public interface OnItemClickListener {
void onItemClick(int position, long itemId);
}

public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
}

RecyclerView分割线请查看RecyclerView-简单设置分割线

坚持原创技术分享,您的支持是对我最大的鼓励!