Material Design 拾荒记(二) RecyclerView Simple

来源:转载

RecyclerView 是Google在Android 5.0之后推出的代替ListView,GridView等等的控件。

它让原先的操作变得简单。

RecyclerView可以认为是AbsListview的升级版,但自带了对viewholder的使用,

与listview缓存convertview不同的是,Recyclerview缓存的是viewholder.

对每个item的动画也有较好的支持。

RecyclerView提供了以下5种角色

RecyclerView.Adapter

RecyclerView.ViewHolder

RecyclerView.LayoutManager 布局器,负责Item视图的布局

RecyclerView.ItemDecoration 每个item附加的子视图,可用来绘制Divider,设置padding等

RecyclerView.ItemAnimator 负责添加、删除数据时的动画效果

这篇文章简单的介绍RecyclerView 的使用,至于复杂的使用在后续的文章中会展开。

初始化RecyclerView 布局

[code] <android.support.v7.widget.RecyclerViewandroid:id="@+id/recycleview"android:layout_width="match_parent"android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView>


[code]initData();mRecyclerView = (RecyclerView) findViewById(R.id.recycleview);//初始化适配器mAdapter = new RecycleAdapter(mData,getApplicationContext());//初始化比较管理器mLayoutManager = new LinearLayoutManager(getApplicationContext());mRecyclerView.setLayoutManager(mLayoutManager);//设置单击监听mAdapter.setOnCLickListener(new RecycleAdapter.ItemClickListener() { @Override public void OnClick(View v) {Snackbar.make(view, v.getTag().toString(), Snackbar.LENGTH_SHORT).show(); }});//设置长按监听mAdapter.setOnLongClickListener(new RecycleAdapter.ItemLongClickListener() { @Override public void OnLongClick(View v) {Snackbar.make(view, v.getTag().toString(), Snackbar.LENGTH_SHORT).show(); }});//添加的滚动监听mRecyclerView.addOnScrollListener(new MyOnScrollListener());mRecyclerView.setAdapter(mAdapter);


设置适配器

[code]public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder> implements View.OnClickListener,View.OnLongClickListener{ //数据 List<RecycleItemBean> mData; Context mContext; LayoutInflater mLayoutInflater; ItemClickListener mItemClickListener; ItemLongClickListener mItemLongClickListener; public RecycleAdapter(List<RecycleItemBean> mData, Context mContext) {this.mData = mData;this.mContext = mContext;mLayoutInflater = LayoutInflater.from(mContext); } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {//初始化ItemView view = mLayoutInflater.inflate(R.layout.recycleitem, parent, false);//ViewHolder初始化MyViewHolder holder = new MyViewHolder(view);//设置View监听holder.itemView.setOnLongClickListener(this);//设置ViewItem上面的图片监听holder.mImageView.setOnClickListener(this);//设置ViewItem上文字监听holder.mText.setOnClickListener(this);return holder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) {//绑定数据holder.mImageView.setImageResource(mData.get(position).getImageviewdrawable());holder.mText.setText(mData.get(position).getTitle());//设置View的Tag的数据,用于监听事件获得数据holder.itemView.setTag(mData.get(position).toString());holder.mImageView.setTag("Image" + position);holder.mText.setTag(mData.get(position).getTitle()); } @Override public int getItemCount() {return mData.size(); } // 删除指定的Item public void removeData(int position) {mData.remove(position);// 通知RecyclerView控件某个Item已经被删除notifyItemRemoved(position); } // 在指定位置添加一个新的Item public void addItem(RecycleItemBean model,int positionToAdd) {mData.add(model);// 通知RecyclerView控件插入了某个ItemnotifyItemInserted(positionToAdd); } @Override public void onClick(View v) {if (mItemClickListener != null) { mItemClickListener.OnClick(v);} } @Override public boolean onLongClick(View v) {if (mItemLongClickListener != null) { mItemLongClickListener.OnLongClick(v);}return true; } //ItemView上面控件的初始化 class MyViewHolder extends RecyclerView.ViewHolder{ImageView mImageView;TextView mText;public MyViewHolder(View itemView) { super(itemView); mImageView = (ImageView) itemView.findViewById(R.id.image); mText = (TextView) itemView.findViewById(R.id.title);} } //单击的回调接口 public interface ItemClickListener {void OnClick(View v); } //长按的回调接口 public interface ItemLongClickListener{void OnLongClick(View v); } //设置单击的事件监听 public void setOnCLickListener(ItemClickListener listener) {if (listener != null) { this.mItemClickListener = listener;} } //设置长按的事件监听 public void setOnLongClickListener(ItemLongClickListener listener){if (listener != null) { this.mItemLongClickListener = listener;} }}


添加事件监听

[code] //单击的回调接口 public interface ItemClickListener {void OnClick(View v); } //长按的回调接口 public interface ItemLongClickListener{void OnLongClick(View v); } //设置单击的事件监听 public void setOnCLickListener(ItemClickListener listener) {if (listener != null) { this.mItemClickListener = listener;} } //设置长按的事件监听 public void setOnLongClickListener(ItemLongClickListener listener){if (listener != null) { this.mItemLongClickListener = listener;} }


项目地址:https://github.com/locojyw/RecyclerView



分享给朋友:
您可能感兴趣的文章:
随机阅读: