RecyclerView是ListView的衍生而来的,它比ListView效率更高,使用起来也更加方便。

本文将会讲解用RecyclerView如何实现:垂直列表效果、横向列表效果、网格列表效果、瀑布流(横向与纵向),以及Item的点击监听(因为RecyclerView没有item的点击监听功能,这里是我自创的,感觉还算好用)。效果还算强大吧?哈哈,下面看源码

(1)主布局

<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><android.support.v7.widget.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="match_parent"/></RelativeLayout>

(2)Item

<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/tv"android:textSize="60dp"android:padding="20dp"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>

(3)MainActivity代码

publicclassMainActivityextendsAppCompatActivity{privateRecyclerViewrecyclerView;privateString[]ibsValue={"直播","番剧","动画","音乐","舞蹈","游戏","科技","娱乐","鬼畜","电影","电视剧","游戏中心",};@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);recyclerView=(RecyclerView)findViewById(R.id.recyclerView);/***设置固定尺寸*/recyclerView.setHasFixedSize(false);/***想用线性布局就用这个*///LinearLayoutManagerlinearLayoutManager=newLinearLayoutManager(this);/***如果用线行布局的话,可以设置垂直/横向*///linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);/***想用网格布局就用这个*///GridLayoutManagergridLayoutManager=newGridLayoutManager(this,2);/***想用瀑布流布局就用这个*/StaggeredGridLayoutManagerstaggeredGridLayoutManager=newStaggeredGridLayoutManager(3,StaggeredGridLayoutManager.HORIZONTAL);/***在这里选择排列布局(用哪个就放进来就行了)*/recyclerView.setLayoutManager(staggeredGridLayoutManager);/***设置适配器*/recyclerView.setAdapter(newMyAdapter(ibsValue));}}

(4)适配器Adapter

/***CreatedbyAdministratoron2015/11/15.*/publicclassMyAdapterextendsRecyclerView.Adapter<MyAdapter.ViewHolder>{/***这里创建一个数组,准备接收传过来的数据*/publicString[]datas;/***这里调用在创建MyAdapter实例的时候,可以将数据传过来*@parammdatas*/publicMyAdapter(String[]mdatas){datas=mdatas;}/***这里加载加载Item,并且创建ViewHolder对象,把加载的Item(View)传给viewholder*@paramparent*@paramviewType*@return*/@OverridepublicViewHolderonCreateViewHolder(ViewGroupparent,intviewType){Viewview=LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);ViewHolderviewHolder=newViewHolder(view,parent.getContext());returnviewHolder;}/***这里给item中的子View绑定数据*@paramholder*@paramposition*/@OverridepublicvoidonBindViewHolder(ViewHolderholder,intposition){holder.mTextView.setText(datas[position]);}/***这里返回item数量*@return*/@OverridepublicintgetItemCount(){returndatas.length;}/***ViewHolder类,注意要继承RecyclerView.ViewHolder*/publicstaticclassViewHolderextendsRecyclerView.ViewHolderimplementsView.OnClickListener{publicTextViewmTextView;publicContextcontext;publicViewHolder(ViewitemView,Contextcontext){super(itemView);mTextView=(TextView)itemView.findViewById(R.id.tv);this.context=context;itemView.setOnClickListener(this);}/***这里可以设置点击监听*@paramv*/@OverridepublicvoidonClick(Viewv){Toast.makeText(context,mTextView.getText().toString(),Toast.LENGTH_SHORT).show();}}}

运行效果: