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

相关推荐
非情剑10 小时前
Java-Executor线程池配置-案例2
android·java·开发语言
JienDa10 小时前
JienDa聊PHP:PHP 8革命性特性深度实战报告:枚举、联合类型与Attributes的工程化实践
android·开发语言·php
向葭奔赴♡11 小时前
Android AlertDialog实战:5种常用对话框实现
android·java·开发语言·贪心算法·gitee
峥嵘life11 小时前
Android EDLA 搭建Linux测试环境简介
android·linux·运维
李坤林11 小时前
Anroid Perfetto
android
2501_9159090611 小时前
深度解析 iOS 内存占用,构建多工具协同的内存诊断、监控与优化体系
android·ios·小程序·https·uni-app·iphone·webview
sang_xb12 小时前
深入解析 HashMap:从存储架构到性能优化
android·java·性能优化·架构
啃火龙果的兔子12 小时前
android studio运行下载gradle特别慢怎么处理
android·ide·android studio
qq_2965446512 小时前
安卓手机投屏电脑端教程,手机投屏教程,可以手机和电脑互传文件。电脑管理手机文件和APP等操作
android·智能手机·电脑