Android studio中RecycleView的运用

本文介绍了如何在Android Studio中使用RecycleView进行UI设计,包括约束布局、基本控件的使用以及RecycleView与Adapter的结合。通过创建列表项并实现点击跳转,展示了在不同tab页面中使用RecycleView,特别是聊天界面的列表模式设计。讲解了Adapter作为数据与UI桥梁的角色,以及如何通过LayoutManager控制Item的布局方式,如LinearLayoutManager实现横向或纵向滑动效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习目的:

  1. 掌握UI设计中的layout布局(约束布局)与基本控件(button、text、imageview等);
  2. 掌握复杂控件与adapter的使用(请使用RecycleView进行设计)。在主界面中的某个tab页里添加列表项。

功能说明:

在基础门户页面的某个tab页面中添加recycleview.以及在上一列表页面的基础上进行点击跳转设计。

在四个tab页面中的聊天界面使用recycleview设计列表模式。

使用RecyclerView时候,必须指定一个适配器Adapter和一个布局管理器LayoutManager。

当完制作一个界面的布局时,我们要向里面传数据,怎么去传呢?全写在xml文件里显而不合适,这就是Adapter的作用。简而言之,Adapter就是数据与ui之间的桥梁,它把数据与前端ui连接到一起,是一个展示数据的载体。而设置布局管理器RecyclerView则是来控制Item的布局方式,横向、竖向以及瀑布流方式。

例如:你想控制横向或者纵向滑动列表效果可以通过LinearLayoutManager这个类来进行控制(与GridView效果对应的是GridLayoutManager,与瀑布流对应的还StaggeredGridLayoutManager等)。也就是说RecyclerView不再拘泥于ListView的线性展示方式,它也可以实现GridView的效果等多种效果。

代码部分:

Recycleviewchat.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/activity_chat_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDFE6"
    android:orientation="vertical"
    tools:context=".ChatActivity">

    <!--聊天界面顶部-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/activity_chat_imageView_back"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:scaleType="center"
            app:srcCompat="@drawable/back" />

        <TextView
            android:id="@+id/activity_chat_textView1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="8"
            android:gravity="center"
            android:text="TextView"
            android:textSize="30sp" />

        <ImageView
            android:id="@+id/activity_chat_imageView_settings"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginRight="1dp"
            android:layout_weight="1"
            android:scaleType="center"
            app:srcCompat="@drawable/settings" />
    </LinearLayout>

    <View
        android:layout_width="matc
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值