Android 通用首页代码示例

在 Android 中,使用 NestedScrollView 来包含多个部分,如横向 Banner、GridView 和 RecyclerView,可以通过嵌套不同的布局组件来实现。以下是一个示例布局的 XML 文件,展示如何将这些部分嵌套在 NestedScrollView 内:

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
    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="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity">

    <!-- 使用垂直线性布局嵌套多个部分 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!-- 横向 Banner(使用水平 RecyclerView) -->
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/bannerRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" />

        <!-- GridView -->
        <GridView
            android:id="@+id/gridView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:numColumns="2"
            android:columnWidth="160dp"
            android:stretchMode="columnWidth"
            android:gravity="center"
            android:verticalSpacing="8dp"
            android:horizontalSpacing="8dp" />

        <!-- RecyclerView -->
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        
        <!-- 其他部分... -->

    </LinearLayout>
</androidx.core.widget.NestedScrollView>

在这个示例布局中:

  1. 使用 NestedScrollView 来创建可滚动的容器。
  2. 使用垂直线性布局 (LinearLayout) 来嵌套多个部分。你可以在线性布局中添加更多的子视图来包含其他部分。
  3. 使用水平的 RecyclerView 来显示横向的 Banner。你需要创建相应的适配器和数据来填充 Banner。
  4. 使用 GridView 来显示网格视图。你可以在代码中设置适配器并提供数据。
  5. 使用 RecyclerView 来显示列表视图。同样,你需要创建适配器和提供数据。

请根据你的实际需求创建适当的适配器和填充数据,以便每个部分显示正确的内容。这个布局示例提供了一种将不同类型的内容嵌套在 NestedScrollView 中的方法,以实现多个部分的滚动。

相关推荐
执明wa1 小时前
LayoutInflater详解: XML是如何变成View的?
android·xml·开发语言·android studio
404_coder2 小时前
源码视角下的 Android 开机流程:从 Zygote、SystemServer 到 Launcher
android
二流小码农2 小时前
鸿蒙开发:以登录案例了解代码架构MVVM
android·ios·harmonyos
用户69371750013843 小时前
从代码生产者到 AI 协作者:软件工程师的角色重构
android·前端·后端
GitLqr3 小时前
别在 Flutter 的 main() 里乱锁屏幕方向,小心 iPad 分屏功能被你搞没了
android·flutter·ios
sg_knight3 小时前
MySQL 存储过程详解:从入门到实战
android·数据库·mysql·database·dba·关系型数据库·db
爱笑鱼3 小时前
Binder(四):ioctl(BINDER_WRITE_READ) 之后,事务怎样到达目标进程?
android
AFinalStone3 小时前
Android 7系统休眠唤醒(二)开机全链路—BootROM到Launcher
android·电源管理·休眠唤醒
Mr YiRan4 小时前
Android NDK开发之统计到未被回收的图片
android
浮江雾5 小时前
Flutter第十七节-----路由管理(3)
android·开发语言·前端·javascript·flutter·入门