第一行代码 第三章 RecyclerView

RecyclerView作为ListView的增强版,具备更好的性能和扩展性。本文介绍了如何实现RecyclerView的基本用法,包括垂直布局、横向滚动、网格布局以及交错网格布局(瀑布流)。同时,讨论了RecyclerView的点击事件处理,强调了自定义点击事件的重要性。
摘要由CSDN通过智能技术生成

由于ListView只能实现数据纵向(垂直)方向的滚动效果,不能实现横向(水平)方向的滚动效果。因此,android官方更加推荐使用RecyclerView。

RecyclerView可以说是ListView的加强版,不仅可以实现和ListView同样的效果,还优化了ListView的不足之处,同时具有很好的扩展性(实现横向布局,网格布局,瀑布流布局)

RecyclerView是android新增的控件,为了能在所有android版本上使用,需要将它定义在support库中,即在项目的build.gradle中添加相应的依赖库。

实例:
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.sky.recyclerviewdemo.MainActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btnVertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Vertical Layout"
        android:textAllCaps="false"/>
    <Button
        android:id="@+id/btnHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Horizontal Layout"
        android:textAllCaps="false"/>
    <Button
        android:id="@+id/btnGrid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Grid Layout"
        android:textAllCaps="false"/>
    <Button
        android:id="@+id/btnStaggered"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Staggered Grid Layout"
        android:textAllCaps="false"/>
</LinearLayout>

MainActivity.java

public 
抱歉,作为AI语言模型,我无法提供完整的安卓应用程序。下面是一个简单的示例,可用于开始编写新闻应用程序: 1. 在Android Studio中创建一个新项目,并选择Kotlin作为主要语言。 2. 在项目中创建一个RecyclerView,用于显示新闻列表。 3. 创建一个新闻数据模型类,包括标题、描述、图片等信息。例如: ``` data class News(val title: String, val description: String, val image: Int) ``` 4. 创建一个新闻适配器类,用于将新闻数据绑定到RecyclerView中的项。例如: ``` class NewsAdapter(private val newsList: List<News>) : RecyclerView.Adapter<NewsAdapter.ViewHolder>() { class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { val titleTextView: TextView = view.findViewById(R.id.title_text_view) val descriptionTextView: TextView = view.findViewById(R.id.description_text_view) val imageView: ImageView = view.findViewById(R.id.image_view) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.news_item, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { val news = newsList[position] holder.titleTextView.text = news.title holder.descriptionTextView.text = news.description holder.imageView.setImageResource(news.image) } override fun getItemCount() = newsList.size } ``` 5. 在MainActivity中初始化新闻数据,并将其传递给适配器。例如: ``` class MainActivity : AppCompatActivity() { private lateinit var newsRecyclerView: RecyclerView private lateinit var newsAdapter: NewsAdapter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) newsRecyclerView = findViewById(R.id.news_recycler_view) newsAdapter = NewsAdapter(getNewsList()) newsRecyclerView.adapter = newsAdapter newsRecyclerView.layoutManager = LinearLayoutManager(this) } private fun getNewsList(): List<News> { return listOf( News("新闻标题1", "新闻描述1", R.drawable.news_image1), News("新闻标题2", "新闻描述2", R.drawable.news_image2), News("新闻标题3", "新闻描述3", R.drawable.news_image3), // 添加更多新闻... ) } } ``` 6. 在布局文件中添加RecyclerView和新闻项布局。例如: ``` <androidx.recyclerview.widget.RecyclerView android:id="@+id/news_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> <LinearLayout android:id="@+id/news_item" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/image_view" android:layout_width="match_parent" android:layout_height="200dp" android:scaleType="centerCrop"/> <TextView android:id="@+id/title_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold" android:padding="8dp"/> <TextView android:id="@+id/description_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" android:padding="8dp"/> </LinearLayout> ``` 这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值