RecyclerView经常被用来和listview做对比来使用,今天来分享一个项目中的例子,来给大家展示一下RecyclerView比listview的优势。
项目要求:
1.下边列表项数可以根据需求动态配置,当数量小于4项的时候,我们把listitem的宽度设为“屏幕的宽度/x项数“,当项数多于4项的时候,每一项的宽度固定为160dp。
2. 当项数少于等于3项时,显示分隔符,多于3项时去掉分隔符
在实际使用过程中,发现在listview的getView方法设置layoutparams总是不能动态变化listview item的宽度。而且分隔符的需求也不好实现。于是考虑用RecyclerView.
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
|
public
class
IconsListViewAdapter
extends
RecyclerView.Adapter<IconsListViewAdapter.ViewHolder> {
private
List<IconsAreaBean.MenusBean> mMenus;
private
Context mContext;
private
LayoutInflater mInflater;
private
int
selectIndex = -
1
;
private
int
defaultItemCount =
4
;
private
MyItemClickListener mListener;
private
int
mDefaultImgs[] = {R.drawable.moneybox, R.drawable.security, R.drawable.help};
public
IconsListViewAdapter(Context context, List<IconsAreaBean.MenusBean> iconsAreaBean) {
this
.mContext = context;
this
.mMenus = iconsAreaBean;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//LayoutInflater.from(mContext);
this
.setOnItemClickListener(
new
MyItemClickListener() {
@Override
public
void
onItemClick(View view,
int
position) {
IconsAreaBean.MenusBean iconBean = mMenus.get(position);
String accUrl = IntentWebViewUtils.getWebUrl(iconBean.getAccUrl());
if
(iconBean !=
null
&& ValityUtil.isUrl(accUrl)){
IntentUtil.openWebView((Activity) mContext, accUrl);
MobclickAgent.onEvent(mContext, ConstantConfig.HOME_PAGE_MENU_CLICK + position);
}
}
});
}
public
void
refreshData(List<IconsAreaBean.MenusBean> menusBeen){
this
.mMenus = menusBeen;
notifyDataSetChanged();
}
@Override
public
ViewHolder onCreateViewHolder(ViewGroup parent,
int
i) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.icon_list_item, parent,
false
);
return
new
ViewHolder(v);
}
@Override
public
void
onBindViewHolder(ViewHolder holder,
int
position) {
if
(mMenus ==
null
|| mMenus.size() ==
0
)
return
;
final
IconsAreaBean.MenusBean iconBean = mMenus.get(position);
if
(iconBean !=
null
) {
holder.getTextView().setText(iconBean.getName());
final
String url = iconBean.getAccUrl();
String imageUrl =
null
;
if
(iconBean.getImgUrl().startsWith(
"http"
) || iconBean.getImgUrl().startsWith(
"https"
)) {
imageUrl = iconBean.getImgUrl();
}
else
{
imageUrl = ConstantConfig.FE_HOST_URL + iconBean.getImgUrl();
}
if
(ValityUtil.isUrl(imageUrl)) {
int
defaultImg;
if
(position < mDefaultImgs.length) {
defaultImg = mDefaultImgs[position];
}
else
{
defaultImg = R.drawable.qiangui_banner_def;
}
ImageLoader imageLoader = HttpManager.getHttpManagerBase(mContext).httpImage();
ImageLoader.ImageListener listener = ImageLoader.getImageListener(holder.getImageView(),
defaultImg, defaultImg);
imageLoader.get(imageUrl, listener);
}
}
if
(mMenus.size() <= defaultItemCount) {
int menuSize = DeviceInfoUtils.getScreenWidth((Activity) mContext) / mMenus.size();
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
menuSize, LinearLayout.LayoutParams.MATCH_PARENT);
holder.getItemView().setLayoutParams(lp);
}
else
{
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
( int ) mContext.getResources().getDimension(R.dimen.qiangui_size_160px), LinearLayout.LayoutParams.MATCH_PARENT);
holder.getItemView().setLayoutParams(lp);
}
}
@Override
public
long
getItemId(
int
position) {
return
position;
}
@Override
public
int
getItemCount() {
return
mMenus !=
null
? mMenus.size() :
0
;
}
public
void
setOnItemClickListener(MyItemClickListener listener) {
this
.mListener = listener;
}
public
class
ViewHolder
extends
RecyclerView.ViewHolder
implements
View.OnClickListener {
private
View rootView;
private
ImageView imageView;
private
TextView textView;
ViewHolder(View view) {
super
(view);
rootView = view;
imageView = (ImageView) view.findViewById(R.id.img_list_item);
textView = (TextView) view.findViewById(R.id.text_list_item);
rootView.setOnClickListener(
this
);
}
/**
* 点击监听
*/
@Override
public
void
onClick(View v) {
if
(mListener !=
null
) {
mListener.onItemClick(v, getPosition());
}
}
ImageView getImageView() {
return
imageView;
}
TextView getTextView() {
return
textView;
}
View getItemView() {
return
rootView;
}
}
}
|
注意在addItemDecoration前边先remove掉,否则会造成分割线添加重复,而使得间距越来越大的情况。