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 中的方法,以实现多个部分的滚动。

相关推荐
xiaoshiquan12066 小时前
Gradle 复合构建:宿主项目直接依赖 SDK 源码模块的配置实践
android
小师兄吃牛肉8 小时前
Android Studio 2025 专业介绍:新特性、性能与开发体验全面解析
android·ide·android studio
美摄科技8 小时前
安卓离线类美颜特效SDK
android
fengci.9 小时前
BUUCTF(困难部分)
android·开发语言·前端·学习·php
传奇开心果11 小时前
【Compose Multiplatform跨端声明式UI实用技术】第9课:跨平台 ViewModel 与状态架构
android·kotlin
用户693717500138412 小时前
当你向领导申请 AI 编程工具预算时,如果领导追问“能提效多少,有量化数据吗”,你该怎么回答?
android·后端·ios
hunterandroid13 小时前
WorkManager 后台任务调度实战:从一次性任务到周期执行
android
事圆则缓13 小时前
Android 屏幕适配的前世今生
android
andyweike13 小时前
kotlin dsl library工程怎么配置
android·开发语言·kotlin
啵啵啵鱼13 小时前
九、JDBC 操作 MySQL 数据库
android·数据库·mysql