RecyclerView

RecyclerView是一种新的视图组,目标是为任何基于适配器的视图提供相似的渲染方式。它被作为ListView和GridView控件的继承者,在最新的support-v7版本中提供使用。
RecyclerView架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager、ItemDecoration、ItemAnimator实现令人瞠目的效果
(总之ListView和GridView能做的RecyclerView都能做,并且能实现瀑布流效果)
ListView效果 使用方法:

  1. 在build.gradle中关联库:
  • compile 'com.android.support:recyclerview-v7:23.3.0'
    tip:此处的23.3.0不固定,于appcompat版本一致皆可
    (也可以在目录结构中找到app右键-open Module Settings-Dependencies-+-Library dependency-找到recyclerview的v7包,也要完成上面的tip)
  1. 准备Item项的显示方式 item_recyclerview.xml
    如:此例子中只放了ImageView和TextView
  2. **准备适配器 **
public class MyRecyclerViewAdapter extends RecyclerView.Adapter{ private final Context context; private ArrayList datas; /**构造方法**/ public MyRecyclerViewAdapter(Context context,ArrayList datas){ this.context=context; this.datas=datas; } class MyViewHolder extends RecyclerView.ViewHolder{ private ImageView iv_icon; private TextView tv_title; public MyViewHolder (View itemView){ super(itemView); iv_icon=(ImageView).findViewById(R.id.iv_icon); tv_title=(TextView).findViewById(R.id.tv_title); } } //相当于getView方法中创建View和ViewHolder public MyViewHolder onCreateViewHolder(ViewGroup parent,int viewType){ View itemView=View.inflate(context,R.layout.item_recyclerview,null); return new MyViewHolder(itemView); } //相当于getView方法中绑定数据 public MyViewHolder onCreateViewHolder(ViewGroup parent,int position){ String data=https://www.it610.com/article/datas.get(position); //根据位置在datas中获取值 holder.tv_title.setText(data); } //得到item的条数 public int getItemCount(){ return datas.size(); } }```4.RecyclerViewActivity.java

private RecyclerView recyclerview;
private ArrayList datas;
private MyRecyclerViewAdapter adapter;
onCreate()方法中:
{ initView();
initData(); //准备数据源
initAdapter(); //为RecyclerView适配Adapter;
setLayoutManager();
}
private void initView(){
recyclerview=(RecyclerView)findViewById(R.id.recyclerview);
//此处注意 第一次使用时在xml中定义控件要用全包名即()
}
//准备数据源
private void initData(){
datas=new ArrayList ();
for(int i=0; i<100; i++){ data.add("content"+i); }
}
//适配器
private void initAdapter(){
adapter=new MyRecyclerViewAdapter(RecyclerViewActivity.this,datas);
recyclerview.setAdapter(adapter);
}
//为recyclerview设置layoutManager
private void initAdapter(){
recyclerview.setLayoutManager(new LinearLayoutManager(MyRecyclerViewActivity.this,LinearLayout.VERTICAL,false));
}
* LinearLayoutManager(Context context,int orientation,boolean reverseLayout) 参数:context:上下文 orientation:方向 reverseLayout:是否倒转######RecyclerView的OnItemSelect RecyclerView本身不支持点击事件,要在Adapter中MyViewHolder类中实现 为了能在MyRecyclerViewAdapter中可以用点击事件,可以定义接口来实现 在MyRecyclerViewAdapter中定义接口

【RecyclerView】public interface OnItemClickListener{
/**
*当RecyclerView某个项被点击时候回调
*@param view 点击item的视图
*@param data 点击得到的数据
*/
public void onItemClick(View view,String data);
}
//实例化
private OnItemClickListener onItemClickListener;
//设置setter方法来设置RecyclerView某条的监听
public void setOnItemClickListener (OnItemClickListener onItemClickListener){
this.onItemClickListener=onItemClickListener;
}

    推荐阅读