本文介绍了分别用芬兰湾的科特林和java写RecyclerView的示例,分享给大家,具体如下:
java:跟一般的写法一样,增加了按钮响应
MainActivity:
公共类MainActivity AppCompatActivity延伸实现RecyclerAdapter.OnItemClickListener { 私人RecyclerView mRecyclerView; 私人RecyclerView。LayoutManager mLayoutManager; 私人RecyclerAdapter mRecyclerAdapter; 私人ArrayListlist; @Override 保护空白> 公开课RecyclerAdapter延伸RecyclerView.Adapter { 私人ArrayList mData; 私人上下文mContext; 私人> 公开课RecyclerItem { 字符串名称; 公共字符串getName () { 返回名称; } 公共空间setName(字符串名称){ this.name=名称; }
activity_main:
& lt; android.support.v7.widget.RecyclerView xmlns:应用=" http://schemas.android.com/apk/res-auto " xmlns:工具=" http://schemas.android.com/tools " android: layout_width=" match_parent " android: layout_height=" match_parent " 工具:上下文=癱om.tudou.recycleviewdemo.MainActivity”比; & lt; android.support.v7.widget.RecyclerView android: id=癅 + id/recyclerView” android: layout_width=" match_parent " android: layout_height=" match_parent " android:背景=" # 00000000 " 在android: paddingBottom=" 5倾斜”; & lt;/android.support.v7.widget.RecyclerView> & lt;/android.support.constraint.ConstraintLayout> >之前recycler_item:
& lt;使用 xmlns: android=" http://schemas.android.com/apk/res/android " android: layout_width=" 100 dp” android: layout_height=?00 dp”比; & lt;按钮 android: id=癅 + id/recycle_name” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android: layout_centerInParent=" true " android:文本=" 1 "/比; & lt;/RelativeLayout> >之前芬兰湾的科特林:语法略有不同,recyclerview调用类似
MainActivity:
类MainActivity: AppCompatActivity (), MainAdapter.ItemClick { val项目:MutableList=ArrayList () var mainAdapter: mainAdapter & # 63;=null 覆盖乐趣> 类MainAdapter: RecyclerView.Adapter { var项目:单# 63;=零 var背景:背景& # 63;=零 var itemClick: itemClick & # 63;=null 构造函数(项目:列表、上下文:上下文){ 这一点。项目=项目 这一点。上下文=上下文 } 乐趣setItemClickListener (itemClick: itemClick) { 这一点。itemClick=itemClick } 乐趣updateData(项目:列表){ 这一点。项目=项目 notifyDataSetChanged () } 覆盖乐趣> 类RecyclerItem { var名称:字符串=" " 得到 集 } >之前activity_main:
& lt; android.support.constraint.ConstraintLayout xmlns:应用=" http://schemas.android.com/apk/res-auto " xmlns:工具=" http://schemas.android.com/tools " android: layout_width=" match_parent " android: layout_height=" match_parent " 工具:上下文=癱om.tudo.kotlintdemo.MainActivity”比; & lt; android.support.v7.widget.RecyclerView android: id=癅 + id/recyclerView” android: layout_width=" match_parent " android: layout_height=" match_parent " android:背景=" # 00000000 " 在android: paddingBottom=" 5倾斜”; & lt;/android.support.v7.widget.RecyclerView> & lt;/android.support.constraint.ConstraintLayout>recycler_item:
& lt; RelativeLayout> xmlns: android=" http://schemas.android.com/apk/res/android " android: layout_width=" 100 dp” android: layout_height=?00 dp”比; & lt;按钮 android: id=癅 + id/recycle_name” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android: layout_centerInParent=" true " android:文本=" 1 "/比; & lt;/RelativeLayout>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
详解分别用芬兰湾的科特林和java写RecyclerView的示例