RecyclerView使用,看AndroidL新特性

在去年Google I/0大会,Google开放了一个全新的视图类RecyclerView,它被用来代替ListView以及GridView,提供更为高效的回收复用机制,同时实现管理与视图的解耦合,今天对这个新的控件来进行一次总结。

概述

首先,让我们来看一下RecyclerView类之下都有哪些重要的类,以及他们的作用:

基本用法

首先让我来看一下RecyclerView的基本用法:

1.创建一个线性布局管理器LayoutManager

// 创建一个线性布局管理器mLayoutManager = new LinearLayoutManager(this);// 默认是Vertical,可以不写mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);mRecyclerView.setLayoutManager(mLayoutManager);

2.重新一个adapter继承RecyclerView.Adapter《VH》,VH就表示我们平时在ListView中用的ViewHolder类(该类必须继承RecyclerView.ViewHolder);

在adapter中有几个重要的方法需要我们自己补充:

RecyclerView.Adapter<MyAdapter.ViewHolder> {private List<String> dataList;public MyAdapter(List<String> list) {this.dataList = list;}@Overridepublic int getItemCount() {// TODO Auto-generated method stubreturn dataList.size();}@Overridepublic void onBindViewHolder(ViewHolder viewHolder, int position) {// TODO Auto-generated method stubviewHolder.textView.setText(dataList.get(position));}@Overridepublic ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {// TODO Auto-generated method stubView view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, null);ViewHolder holder = new ViewHolder(view);return holder;} RecyclerView.ViewHolder {public TextView textView;public ViewHolder(View view) {super(view);// TODO Auto-generated constructor stubtextView = (TextView) view.findViewById(R.id.item_text);}}}

3.设置数据集

ArrayList();for (int i ; i++) {i);}MyAdapter adapter = new MyAdapter(list);mRecyclerView.setAdapter(adapter);

改变LinearLayoutManager的方向可以设置为横向的ListView显示,这也是RecyclerView的强大之处,,可以实现回收管理和视图的解耦。

mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);如何为Item添加分割线

简单的使用之后我们看到的RecyclerView连基本的分割线都没有,接下来就来看看如何为它添加分割线吧:使用addItemDecoration方法可以为RecyclerView添加一个ItemDecoration,利用ItemDecoration为我们绘制分割线。

ItemDecoration下有三个方法,ItemDecoration并没有对其实现,需要我们自己完成:

让我们来看看代码:

{private Drawable mDrawable;public ItemDivider(Context context, int resId) {//在这里我们传入作为Divider的Drawable对象mDrawable = context.getResources().getDrawable(resId);}(Canvas c, RecyclerView parent) {final int left = parent.getPaddingLeft();final int right = parent.getWidth() – parent.getPaddingRight();final int childCount = parent.getChildCount();for (int i = 0; i < childCount; i++) {final View child = parent.getChildAt(i);final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();top = child.getBottom() + params.bottomMargin;final int bottom = top + mDrawable.getIntrinsicHeight();mDrawable.setBounds(left, top, right, bottom);mDrawable.draw(c);}}(Rect outRect, int position, RecyclerView parent) {outRect.set(0, 0, 0, mDrawable.getIntrinsicWidth());}}

在这里我写了一个shape来代替分割线:

===”2dp” /></shape>

添加分割线的效果:

如何添加点击事件 ##

在Adapter中设置自己OnItemClickListener以及OnItemLongClickListener 在ViewHolder中公开Item的根布局View,之后在onBindViewHolder方法获得根布局View并设置点击的listener,调用自己设置的listener

1.首先在MyAdapter中创建自己的接口:

public interface OnItemClickListener {(View parent, int position);}public interface OnItemLongClickListener {public boolean onLongClick(View parent, int position);}

2.接着在ViewHolder中将根布局View公开:

.ViewHolder {public TextView textView;public View itemView;public ViewHolder(View view) {super(view);// TODO Auto-generated constructor stubitemView = view;textView = (TextView) view.findViewById(R.id.item_text);}}近朱者赤,近墨者黑

RecyclerView使用,看AndroidL新特性

相关文章:

你感兴趣的文章:

标签云: