首页 > 代码库 > 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);//必须指定adaoterrecyclerView.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的点击事件
@Overridepublic 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。