RecyclerView的使用技巧

RecyclerView(ListView)在一个App中出现的频率还是非常高的,它是官方出的一个用来替代ListView、GridView的控件,下面简单的介绍一下RecyclerView中一些简单的使用技巧。

官方

官方网址(需要翻墙)

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

官方给出的一句话解释是:

A flexible view for providing a limited window into a large data set.

Gradle

compile ‘com.android.support:recyclerview-v7:+’

建议搭配cardView使用

compile ‘com.android.support:cardview-v7:+’

使用

RecyclerView使用起来很简单,但相比于ListView来说,使用的方法还是有一点改变的。

1
2
3
4
<android.support.v7.widget.RecyclerView 注意V7兼容包名 否则只能api 21才能用
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

1
2
3
4
5
6
7
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));//线性显示(默认纵向显示) 类似于ListView
// recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));//线性显示(横向显示)
// recyclerView.setLayoutManager(new GridLayoutManager(this, 2));//线性宫格显示 类似于GridView
// recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));//瀑布流显示

recyclerView.setAdapter(new RecyclerViewAdapter(this,data));
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
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private Context context;
private List<Post> posts;

public RecyclerViewAdapter(Context context, List<Post> posts) {
this.context = context;
this.posts = posts;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_post, parent,false);
ViewHolder vh = new ViewHolder(v);
return vh;
}

@Override
public void onBindViewHolder(ViewHolder vh, int position) {
vh.contentTv.setText(posts.get(position).getContent());
}

@Override
public int getItemCount() {
return posts.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
private TextView contentTv;
public ViewHolder(View itemView) {
super(itemView);
contentTv = (TextView) itemView.findViewById(R.id.content_post_tv);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//你的点击事件
}
});
}
}
}

做到这里就算完成了。

—————————–分割线—————————-

这些是简单的使用,不过我们可以写一个万能的Adapter来适配每一个RecyclerView,请往下看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class RVHolder  extends RecyclerView.ViewHolder {


private ViewHolder viewHolder;

public RVHolder(View itemView) {
super(itemView);
viewHolder=ViewHolder.getViewHolder(itemView);

}


public ViewHolder getViewHolder() {
return viewHolder;
}
}

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
public abstract class AutoRVAdapter extends RecyclerView.Adapter<RVHolder> {
public final static Integer VIEW_ITEM = 101;
public final static Integer VIEW_PROG = 100;
public List<?> list;

public Context context;

public AutoRVAdapter(Context context, List<?> list) {
this.list = list;
this.context = context;
}

@Override
public RVHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(onCreateViewLayoutID(viewType),parent,false);

return new RVHolder(view);
}

public abstract int onCreateViewLayoutID(int viewType);


@Override
public void onViewRecycled(final RVHolder holder) {
super.onViewRecycled(holder);
}

@Override
public void onBindViewHolder(final RVHolder holder, final int position) {

onBindViewHolder(holder.getViewHolder(), position);
if (onItemClickListener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onItemClick(null, v, holder.getPosition(), holder.getItemId());
}
});
}

}

public abstract void onBindViewHolder(ViewHolder holder, int position);

@Override
public int getItemCount() {
return list.size();
}

private AdapterView.OnItemClickListener onItemClickListener;

public AdapterView.OnItemClickListener getOnItemClickListener() {
return onItemClickListener;
}

public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
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
public class ViewHolder {
private SparseArray<View> viewHolder;
private View view;

public static ViewHolder getViewHolder(View view){
ViewHolder viewHolder = (ViewHolder) view.getTag();
if (viewHolder == null) {
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
}
return viewHolder;
}
private ViewHolder(View view) {
this.view = view;
viewHolder = new SparseArray<View>();
view.setTag(viewHolder);
}
public <T extends View> T get(int id) {
View childView = viewHolder.get(id);
if (childView == null) {
childView = view.findViewById(id);
viewHolder.put(id, childView);
}
return (T) childView;
}

public View getConvertView() {
return view;
}

public TextView getTextView(int id) {

return get(id);
}
public Button getButton(int id) {

return get(id);
}

public ImageView getImageView(int id) {
return get(id);
}

public void setTextView(int id,CharSequence charSequence){
getTextView(id).setText(charSequence);
}
}

以后我们只要集成AutoRVAdapter就好了,简单方便。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class RecyclerViewAdapter extends AutoRVAdapter{
public RecyclerViewAdapter(Context context, List<?> list) {
super(context, list);
}
@Override
public int onCreateViewLayoutID(int viewType) {
return R.layout.item_appointment;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Post post = (Post) list.get(position);
holder.setTextView(R.id.text,post.getName());
}
}

推荐

https://github.com/cymcsg/UltimateRecyclerView

集成了很多RecyclerView的功能,包括上拉刷新,下拉加载,加载动画,视差动画等

注意的问题

  • RecyclerView嵌套在ScrollView里面是一行都不显示的(ListView显示一行)这里需要重写LayoutManager,具体可google
  • 在5.0以上的手机 RecyclerView嵌套在ScrollView 重写LayoutManager后 RecyclerView的滑动惯性会消失,此时可重写ScrollView即可
    -

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2017 HACKGROUND All Rights Reserved.

UV : | PV :