android-基础编程-RecyclerView

别裁伪体亲风雅,转益多师是汝师。这篇文章主要讲述android-基础编程-RecyclerView相关的知识,希望能为你提供帮助。
以后android-基础编程*都是控件demo里面的,不再累赘重写。直接介绍控件使用。
RecyclerView is a more advanced and flexible version of ListView. This widget is a  Container  for large sets of views that can be recycled and scrolled very efficiently.RecyclerView从2014年发布.位于android.support.v7.widget.RecyclerView中。
1.android studio配置RecyclerView库代码
在gradle中加入

compile \'com.android.support:recyclerview-v7:23.+\'

2.定义布局中使用
< android.support.v7.widget.RecyclerView android:id="@+id/recycle_view" android:layout_width="match_parent" android:layout_height="match_parent"> < /android.support.v7.widget.RecyclerView>

还要recycleview的item的布局:
< FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="#44ff0000" android:layout_height="wrap_content" > < TextView android:id="@+id/id_num" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:text="1" /> < /FrameLayout>

3.在RcyclerViewActi中使用
a.加载view布局:
recyclerView = (RecyclerView) findViewById(R.id.recycle_view);

 
b.设置布局管理LayoutManager
recyclerView.setLayoutManager(new LinearLayoutManager(this));

目前SDK中提供了三种自带的LayoutManager,分别是线程、网格、瀑布式布局管理器:
  • LinearLayoutManager
  • GridLayoutManager
  • StaggeredGridLayoutManager
c.配置数据源
adapter = new RecyclerAdapter(this); adapter.setData(mDatas); //必须指定adaoter recyclerView.setAdapter(adapter);

  4.点击事件,recycler不支持点击事件,需要自己实现
  a.在adapter定义接口实现如下:
public static interface OnRecyclerViewItemClickListener { void onItemClick(View view , int pos); }// 2 添加接口和设置接口的方法 private OnRecyclerViewItemClickListener mOnItemClickListener = null; public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) { this.mOnItemClickListener = listener; }

b.adater implement view的点击事件
@Override public void onClick(View v) { //Toast.makeText(get, "默认Toast样式").show(); Log.d("zcx","test"); if (mOnItemClickListener != null) { //调用onitemclick mOnItemClickListener.onItemClick(v,1); }

c.调用onitemclick
adapter.setOnItemClickListener(new RecyclerAdapter.OnRecyclerViewItemClickListener() { @Override public void onItemClick(View view, int pos) { //DO your fucking bussiness here! adapter.addData(1); Toast.makeText(getApplicationContext(),"zcx"+pos,Toast.LENGTH_LONG).show(); } });

5.item的动画效果:
recyclerView.setItemAnimator(new DefaultItemAnimator());

最后的实验结果如下:
android-基础编程-RecyclerView

文章图片

android-基础编程-RecyclerView

文章图片

【android-基础编程-RecyclerView】 

    推荐阅读