//首先你得明白Recylerview的好处
①RecylerView封装了viewholder的回收复用,也就是说RecylerView标准化了ViewHolder,编写Adapter面向的是ViewHolder而不再是View了,复用逻辑被封装了,写起来更加简单。
②提供了一种插拔式的体验,高度的解耦,异常的灵活,针对一个Item的显示RecylerView专门抽取出了相应的类,来控制Item的显示,使其的扩展性非常强。例如:你想控制横向或者纵向滑动列表效果可以通过LinearLayoutManager这个类来进行控制(与GridView效果对应的是GridLayoutManager,与瀑布流对应的还有StaggeredGridLayoutManager等),也就是说RecylerView不再拘泥于ListView的线性展示方式,它也可以实现GridView的效果等多种效果。你想控制Item的分隔线,可以通过继承RecylerView的ItemDecoration这个类,然后针对自己的业务需求去抒写代码。
③可以控制Item增删的动画,可以通过ItemAnimator这个类进行控制,当然针对增删的动画,RecylerView有其自己默认的实现。
添加依赖
implementation 'com.android.support:recyclerview-v7:27.0.2'
布局文件
<android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recv" ></android.support.v7.widget.RecyclerView>要写两个类MyListHolder,MyListAdapter
在MyListHolder中:
public class MyListHolder extends RecyclerView.ViewHolder { public TextView tx; public MyListHolder(View view) { super(view); //holder里面是需要拿到的视图控件 tx = (TextView) view.findViewById(R.id.item_text); } }
在MyListAdapter:
public class MyListAdpter extends RecyclerView.Adapter<MyListHolder>{ private ArrayList<Integer> heightList = new ArrayList<>(); private Context context; private ArrayList<String> list; private OnItemListner onItemListner; public MyListAdpter(Context context, ArrayList<String> list) { this.context=context; this.list=list; //创建随机数 for (int i = 0;i<list.size();i++){ heightList.add((int) (100+Math.random()*200)); } } @Override public MyListHolder onCreateViewHolder(ViewGroup parent, int viewType) { //条目的视图 View view = View.inflate(context, R.layout.list_view_layout, null); //根据条目的视图创建viewholder MyListHolder myListHolder=new MyListHolder(view); return myListHolder; } @Override public void onBindViewHolder(MyListHolder holder, final int position) { ViewGroup.LayoutParams layoutParams = holder.tx.getLayoutParams(); layoutParams.height = heightList.get(position); layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; //设置随机的背景颜色 holder.tx.setBackgroundColor(Color.rgb((int) (100+ Math.random()*155),(int) (100+ Math.random()*155),(int) (100+ Math.random()*155))); holder.tx.setText(list.get(position)); //设置监听事件 holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onItemListner.onItemClick(position); } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { onItemListner.onItemLongClick(position); //解决长按和点击的冲突 return true; } }); } @Override public int getItemCount() { return list.size(); } /** * 创建setOnitemListener方法 * @param onItemListner */ public void setOnItemListner(OnItemListner onItemListner) { this.onItemListner = onItemListner; }在MainActivity:
public class Main2Activity extends AppCompatActivity { private RecyclerView rv; private ArrayList<String> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); //获取控件 rv = (RecyclerView) findViewById(R.id.recv); //获取数据 list = new ArrayList<>(); for (int i=0;i<1000;i++){ list.add("这是标题"+i); } //制定布局管理器 rv.setLayoutManager(new LinearLayoutManager(Main2Activity.this));
rv.setLayoutManager(new GridLayoutManager(this,2, OrientationHelper.VERTICAL,false));//设置适配器 MyListAdpter myListAdpter=new MyListAdpter(this,list); rv.setAdapter(myListAdpter);
自定义接口:OnItemListenerrv.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL)); //条目的点击事件和长按事件 myListAdpter.setOnItemListner(new OnItemListner() { @Override public void onItemClick(int position) { Toast.makeText(Main4Activity.this,"点击了:"+list.get(position),Toast.LENGTH_SHORT).show(); } @Override public void onItemLongClick(int position) { Toast.makeText(Main4Activity.this,"点击了:"+list.get(position),Toast.LENGTH_SHORT).show(); } }); }}}
public interface OnItemListner { void onItemClick(int position); void onItemLongClick(int position); }