Android RecyclerView使用方法解析
1.简介
RecyclerView是一种新的视图组,目标是为任何基于适配器的视图提供相似的渲染方式。它被作为ListView和GridView控件的继承者,在最新的support-V7版本中提供支持。RecyclerView架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration,ItemAnimator实现令人瞠目的效果。
总之ListView和GridView能做的RecyclerView都能做,并且能实现瀑布流效果
2.使用
在build.gradle配置RecyclerView的库
compile'com.android.support:recyclerview-v7:23.3.0'
//设置RecyclerView的适配器 adapter=newMyRecyclerViewAdapter(RecyclerViewActivity.this,datas); recyclerview.setAdapter(adapter); //LayoutManager recyclerview.setLayoutManager(newLinearLayoutManager(RecyclerViewActivity.this,LinearLayoutManager.VERTICAL,false)); //recyclerview.scrollToPosition(datas.size()-1); //添加RecyclerView的分割线 recyclerview.addItemDecoration(newDividerListItemDecoration(RecyclerViewActivity.this,DividerListItemDecoration.VERTICAL_LIST)); //设置动画 recyclerview.setItemAnimator(newDefaultItemAnimator());
publicclassMyAdapterextendsRecyclerView.Adapter<MyAdapter.MyViewHolder>{ privatefinalContextcontext; privatefinalArrayList<String>datas; publicMyAdapter(Contextcontext,ArrayList<String>datas){ this.context=context; this.datas=datas; } /** *相当于ListView适配器中的getView的创建holder布局 * *@paramparent *@paramviewType *@return */ @Override publicMyViewHolderonCreateViewHolder(ViewGroupparent,intviewType){ Viewview=View.inflate(context,R.layout.item_hello,null); returnnewMyViewHolder(view); } @Override publicvoidonBindViewHolder(MyViewHolderholder,intposition){ holder.tv_text.setText(datas.get(position)); holder.iv_icon.setBackgroundResource(R.mipmap.ic_launcher); } @Override publicintgetItemCount(){ returndatas.size(); } classMyViewHolderextendsRecyclerView.ViewHolder{ privateTextViewtv_text; privateImageViewiv_icon; publicMyViewHolder(ViewitemView){ super(itemView); tv_text=(TextView)itemView.findViewById(R.id.tv_text); iv_icon=(ImageView)itemView.findViewById(R.id.iv_icon); } } }
3.设置ListView&GridView&瀑布流类型效果
//设置List类型效果 recyclerview.setLayoutManager(newLinearLayoutManager(RecyclerViewActivity.this,LinearLayoutManager.VERTICAL,false)); //设置Grid类型效果 recyclerview.setLayoutManager(newGridLayoutManager(RecyclerViewActivity.this,2,GridLayoutManager.VERTICAL,false)); //recyclerview.scrollToPosition(99); //设置瀑布流类型效果 recyclerview.setLayoutManager(newStaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
4.分割线
参照网址:http://blog.csdn.net/lmj623565791/article/details/45059587
//设置分割线-分割线需要自定义&还可以自定义分割线的样式 //没有提供默认的分割线 recyclerview.addItemDecoration(newDividerListItemDecoration(this,DividerListItemDecoration.VERTICAL_LIST));
publicclassDividerListItemDecorationextendsRecyclerView.ItemDecoration{ privatestaticfinalint[]ATTRS=newint[]{ android.R.attr.listDivider }; publicstaticfinalintHORIZONTAL_LIST=LinearLayoutManager.HORIZONTAL; publicstaticfinalintVERTICAL_LIST=LinearLayoutManager.VERTICAL; privateDrawablemDivider; privateintmOrientation; publicDividerListItemDecoration(Contextcontext,intorientation){ finalTypedArraya=context.obtainStyledAttributes(ATTRS); mDivider=a.getDrawable(0); a.recycle(); setOrientation(orientation); } publicvoidsetOrientation(intorientation){ if(orientation!=HORIZONTAL_LIST&&orientation!=VERTICAL_LIST){ thrownewIllegalArgumentException("invalidorientation"); } mOrientation=orientation; } @Override publicvoidonDraw(Canvasc,RecyclerViewparent){ //Log.e("recyclerview-itemdecoration","onDraw()"); if(mOrientation==VERTICAL_LIST){ drawVertical(c,parent); }else{ drawHorizontal(c,parent); } } publicvoiddrawVertical(Canvasc,RecyclerViewparent){ finalintleft=parent.getPaddingLeft(); finalintright=parent.getWidth()-parent.getPaddingRight(); finalintchildCount=parent.getChildCount(); for(inti=0;i<childCount;i++){ finalViewchild=parent.getChildAt(i); android.support.v7.widget.RecyclerViewv=newandroid.support.v7.widget.RecyclerView(parent.getContext()); finalRecyclerView.LayoutParamsparams=(RecyclerView.LayoutParams)child .getLayoutParams(); finalinttop=child.getBottom()+params.bottomMargin; finalintbottom=top+mDivider.getIntrinsicHeight(); mDivider.setBounds(left,top,right,bottom); mDivider.draw(c); } } publicvoiddrawHorizontal(Canvasc,RecyclerViewparent){ finalinttop=parent.getPaddingTop(); finalintbottom=parent.getHeight()-parent.getPaddingBottom(); finalintchildCount=parent.getChildCount(); for(inti=0;i<childCount;i++){ finalViewchild=parent.getChildAt(i); finalRecyclerView.LayoutParamsparams=(RecyclerView.LayoutParams)child .getLayoutParams(); finalintleft=child.getRight()+params.rightMargin; finalintright=left+mDivider.getIntrinsicHeight(); mDivider.setBounds(left,top,right,bottom); mDivider.draw(c); } } @Override publicvoidgetItemOffsets(RectoutRect,intitemPosition,RecyclerViewparent){ if(mOrientation==VERTICAL_LIST){ outRect.set(0,0,0,mDivider.getIntrinsicHeight()); }else{ outRect.set(0,0,mDivider.getIntrinsicWidth(),0); } } } DividerListItemDecoration
设置分割线样式:
①application设置
<!--Baseapplicationtheme.--> <stylename="AppTheme"parent="Theme.AppCompat.Light.DarkActionBar"> <!--Customizeyourthemehere.--> <itemname="colorPrimary">@color/colorPrimary</item> <itemname="colorPrimaryDark">@color/colorPrimaryDark</item> <itemname="colorAccent">@color/colorAccent</item> <itemname="android:listDivider">@drawable/divider_bg</item> </style>
②在activity中
<activityandroid:name=".recyclerview.RecyclerViewActivity" android:theme="@style/listDividerTheme"/> <stylename="listDividerTheme"parent="Theme.AppCompat.Light.DarkActionBar"> <!--Customizeyourthemehere.--> <itemname="colorPrimary">@color/colorPrimary</item> <itemname="colorPrimaryDark">@color/colorPrimaryDark</item> <itemname="colorAccent">@color/colorAccent</item> <itemname="android:listDivider">@drawable/divider_bg</item> </style>
5.自定义设置item的点击事件
RecyclerView默认是没有点击事件的,需要自定义点击事件
用到知识点:接口,getLayoutPosition()
publicclassMyAdapterextendsRecyclerView.Adapter<MyAdapter.MyViewHolder>{ privatefinalContextcontext; privatefinalArrayList<String>datas; //设置点击某个item的监听 publicinterfaceOnItemClickListener{ voidonItemClick(Viewview,intposition,Stringcontent); } privateOnItemClickListeneronItemClickListener; publicvoidsetOnItemClickListener(OnItemClickListeneronItemClickListener){ this.onItemClickListener=onItemClickListener; } //设置点击图片 publicinterfaceOnImageViewClickListener{ voidonImageViewClick(Viewview,intposition); } privateOnImageViewClickListeneronImageViewClickListener; publicvoidsetOnImageViewClickListener(OnImageViewClickListeneronImageViewClickListener){ this.onImageViewClickListener=onImageViewClickListener; } ................................... classMyViewHolderextendsRecyclerView.ViewHolder{ privateTextViewtv_text; privateImageViewiv_icon; publicMyViewHolder(ViewitemView){ super(itemView); tv_text=(TextView)itemView.findViewById(R.id.tv_text); iv_icon=(ImageView)itemView.findViewById(R.id.iv_icon); //设置点击事件 itemView.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ if(onItemClickListener!=null){ onItemClickListener.onItemClick(v,getLayoutPosition(),datas.get(getLayoutPosition())); } } }); //设置监听 iv_icon.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ if(onImageViewClickListener!=null){ onImageViewClickListener.onImageViewClick(v,getLayoutPosition()); } } }); } } }
在Activity中使用自定义的点击事件
//设置点击item的点击事件 myAdapter.setOnItemClickListener(newMyAdapter.OnItemClickListener(){ @Override publicvoidonItemClick(Viewview,intposition,Stringcontent){ Toast.makeText(RecyclerViewActivity.this,"content=="+content+",--position=="+position,Toast.LENGTH_SHORT).show(); } }); //设置点击某张图片的点击事件 myAdapter.setOnImageViewClickListener(newMyAdapter.OnImageViewClickListener(){ @Override publicvoidonImageViewClick(Viewview,intposition){ Toast.makeText(RecyclerViewActivity.this,"position=="+position+",view=="+view.toString(),Toast.LENGTH_SHORT).show(); } });
6.删除和增加数据
1_在适配器中新增加添加和删除两个方法
publicclassMyAdapterextendsRecyclerView.Adapter<MyAdapter.MyViewHolder>{ ........................ publicvoidaddData(intposition,Stringcontent){ datas.add(position,content); notifyItemInserted(position); } publicvoidremoveData(intposition){ datas.remove(position); notifyItemRemoved(position); } }
2_Activity中
btn_add.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ myAdapter.addData(0,"ContentNetData"); //定位到第0个位置 recyclerview.scrollToPosition(0); } }); btn_remove.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ myAdapter.removeData(0); } });
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。