Android中使用kotlin开发RecyclerView:
提示:这里简述项目相关背景:
RecyclerView是Android中经常使用的控件,接下来就是对RecyclerView使用方法的一些讲解
提示:我在Androidstudio中使用了 ViewBinding
在build.gradle(app)的android中加入以下代码
buildFeatures
{
viewBinding = true
}
在MyApplication中使用:
@Override
//绑定对象实例,可以访问activity——main.xml布局中的视图
private lateinit var binding : ActivityMain2Binding
//适配器数据
private val newsList = ArrayList<NewsData>()
override fun onCreate(savedInstanceState: Bundle?)
{ //初始化binding,用于访问activity_main。xml布局中的UI控件(view对象)
binding = ActivityMain2Binding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
//设置root(根/所有)将内容视图设置为activity活动的布局的根视图
setContentView(binding.root)
//添加适配器
fragment.rlvNews.adapter = MyAdapter(newsList)
//获取并赋值布局管理器;
fragment.rlvNews.layoutManager = LinearLayoutManager(context)
}
适配器使用:
class MyAdapter (private val newsList:List<NewsData>): RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
// 内部类ViewHolder
class MyViewHolder (itemView:View)
: RecyclerView.ViewHolder(itemView) {
//绑定适配器ID获取适配器视图
val newsTitle: TextView = itemView.findViewById(R.id.tv_news_title)
val newsContent: TextView = itemView.findViewById(R.id.tv_news_content)
val newsImage : RoundedImageView = itemView.findViewById(R.id.iv_news_image)
}
//绑定视图
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val newsData = newsList[position]
holder.newsTitle.text = newsData.title
holder.newsContent.text = newsData.author_name
//使用Glide框架加载图片
Glide.with(holder.itemView.context).load(newsData.thumbnail_pic_s).into(holder.newsImage)
//点击事件
holder.itemView.setOnClickListener{
//跳转页面activity
val intent= Intent(holder.itemView.context,DetailActivity::class.java)
//把数据用activity带到下一级页面
intent.putExtra("url=",newsList[holder.adapterPosition].url)
//启动活动 跳转
holder.itemView.context.startActivity(intent)
}
}
//创建视图
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.news_item_one_iamge,parent,false)
return MyViewHolder(itemView)
}
//获取项目个数
override fun getItemCount(): Int {
return newsList.size
}
}
数据类:
data class NewsData(val title:String,val date:String,val author_name:String,val thumbnail_pic_s:String,val url:String)
xml中写:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="5dp"
android:background="#F8F8F8" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginRight="8dp"
android:orientation="vertical">
<TextView
android:layout_weight="1"
android:id="@+id/tv_news_title"
android:layout_marginTop="-3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_news_title"
android:textColor="#222222"
android:textSize="18sp"
android:maxLines="2"
android:ellipsize="end"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/tv_news_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_news_description"
android:textColor="#8f8f8f"
android:textSize="10sp"
tools:ignore="SmallSp" />
</LinearLayout>
<com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/iv_news_image"
android:layout_weight="0.5"
android:layout_width="0dp"
android:scaleType="fitXY"
app:riv_corner_radius="2dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="@mipmap/image_discuss" />
</LinearLayout>
</LinearLayout>
在后面我会尽量完善和写出更好的文章
如有异议或者疑问可以私聊博主