Android RecyclerView详解及简单实例
Android RecyclerView
小白今天第一次接触RecyclerView,前辈大神告诉我这是一个很神奇的控件,一看就是一整天。
RecyclerView中有规定好的方法去显示列表,图片甚至视频。还带有删除新建某一列表的方法。相对于ListView这个
RecyclerView控件就更加牛了。
明白的大神看一眼就懂,小白可以自己照源码敲一遍看看效果。这段程序是把A-Z按列排列下来:
packagecom.example.osserver.recycler; importandroid.app.Activity; importandroid.os.Bundle; importandroid.support.v7.app.ActionBarActivity; importandroid.support.v7.widget.*; importandroid.support.v7.widget.LinearLayoutManager; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.TextView; importjava.util.ArrayList; importjava.util.List; publicclassMainActivityextendsActionBarActivity{ privateRecyclerViewmRecyclerView; privateList<String>mDatas; privateHomeAdaptermAdapter; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); mRecyclerView=(RecyclerView)findViewById(R.id.id_recyclerview); mRecyclerView.setLayoutManager(newLinearLayoutManager(this)); mRecyclerView.setAdapter(mAdapter=newHomeAdapter()); mRecyclerView.addItemDecoration(newDividerItemDecoration(this,DividerItemDecoration.HORIZONTAL_LIST)); } privatevoidinitData(){ mDatas=newArrayList<String>(); for(inti='A';i<='Z';i++){ mDatas.add(""+(char)i); } } classHomeAdapterextendsRecyclerView.Adapter<MyViewHolder> { @Override publicMyViewHolderonCreateViewHolder(ViewGroupparent,intviewType){ MyViewHolderholder=newMyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.item,parent,false)); returnholder; } @Override publicvoidonBindViewHolder(MyViewHolderholder,intposition){ holder.tv.setText(mDatas.get(position)); } @Override publicintgetItemCount(){ returnmDatas.size(); } } classMyViewHolderextendsRecyclerView.ViewHolder { TextViewtv; publicMyViewHolder(Viewview){ super(view); tv=(TextView)view.findViewById(R.id.id_num); } } }
接下来就是列表的润色部分:
今天我也只是进行到了这一步,踩了好多坑。
packagecom.zhy.sample.demo_recyclerview; /* *Copyright(C)2014TheAndroidOpenSourceProject * *LicensedundertheApacheLicense,Version2.0(the"License"); *limitationsundertheLicense. */ importandroid.content.Context; importandroid.content.res.TypedArray; importandroid.graphics.Canvas; importandroid.graphics.Rect; importandroid.graphics.drawable.Drawable; importandroid.support.v7.widget.LinearLayoutManager; importandroid.support.v7.widget.RecyclerView; importandroid.support.v7.widget.RecyclerView.State; importandroid.util.Log; importandroid.view.View; /** *Thisclassisfromthev7samplesoftheAndroidSDK.It'snotbyme! *<p/> *Seethelicenseabovefordetails. */ publicclassDividerItemDecorationextendsRecyclerView.ItemDecoration{ privatestaticfinalint[]ATTRS=newint[]{ android.R.attr.listDivider }; publicstaticfinalintHORIZONTAL_LIST=LinearLayoutManager.HORIZONTAL; publicstaticfinalintVERTICAL_LIST=LinearLayoutManager.VERTICAL; privateDrawablemDivider; privateintmOrientation; publicDividerItemDecoration(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.v("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); } } }
不能添加图片,那就写这些好了。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!