详解分别用Kotlin和java写RecyclerView的示例
本文介绍了分别用Kotlin和java写RecyclerView的示例,分享给大家,具体如下:
java:跟一般的写法一样,增加了按钮响应
MainActivity:
publicclassMainActivityextendsAppCompatActivityimplementsRecyclerAdapter.OnItemClickListener{ privateRecyclerViewmRecyclerView; privateRecyclerView.LayoutManagermLayoutManager; privateRecyclerAdaptermRecyclerAdapter; privateArrayListlist; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerItemitem; list=newArrayList<>(); item=newRecyclerItem(); item.setName("No."+0); list.add(item); mRecyclerView=findViewById(R.id.recyclerView); mRecyclerAdapter=newRecyclerAdapter(this,list); mRecyclerAdapter.setOnItemClickListener(this); //竖直方向的网格样式,每行一个Item mLayoutManager=newGridLayoutManager(this,3,OrientationHelper.VERTICAL,false); //设置布局管理器 mRecyclerView.setLayoutManager(mLayoutManager); //设置adapter mRecyclerView.setAdapter(mRecyclerAdapter); //设置Item添加和移除的动画 mRecyclerView.setItemAnimator(newDefaultItemAnimator()); } //adapter的按钮点击事件 @Override publicvoidonItemContentClick(Viewview,intposition){ //点击最后一个按钮增加一个item if(position==list.size()-1){ RecyclerItemitem=newRecyclerItem(); item.setName("No."+(position+1)); list.add(0,item); //更新RecyclerView mRecyclerAdapter.updateData(list); }else{ Toast.makeText(this,"No."+position,Toast.LENGTH_SHORT).show(); } } }
Adapter:
publicclassRecyclerAdapterextendsRecyclerView.Adapter{ privateArrayListmData; privateContextmContext; privateOnItemClickListeneronItemClickListener; publicvoidsetOnItemClickListener(OnItemClickListenerlistener){ this.onItemClickListener=listener; } publicRecyclerAdapter(ContextmContext,ArrayListmData){ this.mData=mData; this.mContext=mContext; } //刷新整个list publicvoidupdateData(ArrayListdata){ this.mData=data; notifyDataSetChanged(); } //刷新局部list publicvoidupdateDataItem(ArrayListdata,intitemnm){ this.mData=data; notifyItemChanged(itemnm); } @Override publicViewHolderonCreateViewHolder(ViewGroupparent,intviewType){ Viewv=LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false); ViewHolderviewHolder=newViewHolder(v); returnviewHolder; } @Override publicvoidonBindViewHolder(finalViewHolderholder,intposition){ holder.btn.setText(mData.get(position).getName()); holder.btn.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ if(onItemClickListener!=null){ intpos=holder.getLayoutPosition(); onItemClickListener.onItemContentClick(holder.itemView,pos); } } }); } @Override publicintgetItemCount(){ returnmData==null?0:mData.size(); } publicstaticclassViewHolderextendsRecyclerView.ViewHolder{ Buttonbtn; publicViewHolder(ViewitemView){ super(itemView); btn=itemView.findViewById(R.id.recycle_name); } } publicinterfaceOnItemClickListener{ voidonItemContentClick(Viewview,intposition); } }
RecyclerItem:
publicclassRecyclerItem{ Stringname; publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; }
activity_main:
recycler_item:
Kotlin:语法略有不同,recyclerview调用类似
MainActivity:
classMainActivity:AppCompatActivity(),MainAdapter.ItemClick{ valitems:MutableList=ArrayList() varmainAdapter:MainAdapter?=null overridefunonCreate(savedInstanceState:Bundle?){ super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //添加一个item varitem:RecyclerItem item=RecyclerItem() item.name="No."+0 items.add(item) valrecyclerView=findViewById(R.id.recyclerView)asRecyclerView recyclerView.layoutManager=GridLayoutManager(this,3,OrientationHelper.VERTICAL,false) mainAdapter=MainAdapter(items,this) mainAdapter!!.setItemClickListener(this) recyclerView.adapter=mainAdapter recyclerView.itemAnimator=DefaultItemAnimator() } //adapter的按钮点击事件 overridefunOnItemClick(v:View,position:Int){ if(position==items.size-1){ valitem=RecyclerItem() item.name="No."+(position+1) items.add(0,item) //更新数据 mainAdapter!!.updateData(items) }else{ Toast.makeText(this,items.get(position).name,Toast.LENGTH_SHORT).show() } } }
MainAdapter:
classMainAdapter:RecyclerView.Adapter{ varitems:List?=null varcontext:Context?=null varitemClick:ItemClick?=null constructor(items:List,context:Context){ this.items=items this.context=context } funsetItemClickListener(itemClick:ItemClick){ this.itemClick=itemClick } funupdateData(items:List){ this.items=items notifyDataSetChanged() } overridefunonBindViewHolder(holder:ViewHolder?,position:Int){ varname=items!!.get(position).name if(holder!=null){ holder.textName.setText(name) holder.textName.setOnClickListener(View.OnClickListener{ if(itemClick!=null){ itemClick!!.OnItemClick(holder.itemView,position) } }) } } overridefungetItemCount():Int{ returnitems!!.size } overridefunonCreateViewHolder(parent:ViewGroup?,viewType:Int):ViewHolder{ valv=LayoutInflater.from(parent!!.context).inflate(R.layout.recycler_item,parent,false)asRelativeLayout valholder=ViewHolder(v) returnholder } classViewHolder(itemView:View?):RecyclerView.ViewHolder(itemView){ vartextName:TextView=itemView!!.findViewById(R.id.recycle_name)asTextView } interfaceItemClick{ funOnItemClick(v:View,position:Int); } }
RecyclerItem:
classRecyclerItem{ varname:String="" get set }
activity_main:
recycler_item:
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" android:layout_height="100dp">
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。