数据过多 如何实现页面滚动

(1)如果是TextView直接使用即可,如果不是则使用方法2
在这里插入图片描述

(2)在大布局的下面嵌套一个ScrollView,并在ScrollView下嵌套一个LinearLayout包含所有布局
具体代码如下

重要的部分只有这个,代码部分是示例
在这里插入图片描述
示例部分

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#f5f2f2"
    tools:context="client.android.myapp.TypeView">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="牲畜类"
        android:textSize="22dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingLeft="5dp"

        />
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="105dp"
        android:orientation="vertical"
        >
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="50dp"
            >
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text=""
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
            />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text=""
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text=""
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text=""
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="5dp"
            >
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text=""
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text=""
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="2"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#f5f2f2"
                />
        </TableRow>
    </TableLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="果蔬类"
        android:textSize="22dp"
        android:paddingTop="15dp"
        android:paddingBottom="10dp"
        android:paddingLeft="5dp"


        />
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="215dp"
        android:orientation="vertical"
        >
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="50dp"
        >
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="苹果"
            android:layout_weight="1"
            android:textSize="25dp"
            android:gravity="center"
            android:layout_marginLeft="4dp"
            android:background="#ffffff"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:text="香蕉"
            android:layout_weight="1"
            android:textSize="25dp"
            android:gravity="center"
            android:layout_marginLeft="4dp"
            android:background="#ffffff"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:text="葡萄"
            android:layout_weight="1"
            android:textSize="25dp"
            android:gravity="center"
            android:layout_marginLeft="4dp"
            android:background="#ffffff"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:text=""
            android:layout_weight="1"
            android:textSize="25dp"
            android:gravity="center"
            android:layout_marginLeft="4dp"
            android:background="#ffffff"
            />
    </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="5dp"
            >
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text=""
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text="橘子"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text="樱桃"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text="橙子"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="5dp"
            >
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="白菜"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text="菠菜"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text="韭菜"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text="花生"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="5dp"
            >
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="豆芽"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text="香菜"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text="土豆"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text="萝卜"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
        </TableRow>
    </TableLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="水产品"
        android:textSize="22dp"
        android:paddingTop="15dp"
        android:paddingBottom="10dp"
        android:paddingLeft="5dp"

        />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="105dp"
        android:orientation="vertical"
        >
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="50dp"
            >
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text=""
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text=""
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text=""
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text="贝类"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="5dp"
            >
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="植物类"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="3"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#f5f2f2"
                />
        </TableRow>
    </TableLayout>
    </ScrollView>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="干果类"
        android:textSize="22dp"
        android:paddingTop="15dp"
        android:paddingBottom="10dp"
        android:paddingLeft="5dp"

        />
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="105dp"
        android:orientation="vertical"
        >
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="50dp"
            >
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text=""
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text="开心果"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text="地瓜干"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text="山楂干"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="5dp"
            >
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="葡萄干"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="夏威夷果"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="巴旦木"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#ffffff"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:textSize="25dp"
                android:gravity="center"
                android:layout_marginLeft="4dp"
                android:background="#f5f2f2"
                />
        </TableRow>
    </TableLayout>
    </LinearLayout>
   </ScrollView>
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据懒加载(Lazy Load)是指在页面滚动到可视范围内时,再去加载该区域的数据。这样可以优化页面加载速度和用户体验。 下面是一种实现数据懒加载的方式: 1. 给需要懒加载的 DOM 元素添加一个占位符,比如一个空白的 div,占据该元素的位置和尺寸。 2. 监听页面滚动事件,判断需要懒加载的元素是否进入可视范围内。 3. 如果元素进入可视范围内,则动态加载数据,并将数据渲染到占位符元素中。 4. 加载完成后,将占位符元素替换为真实的数据元素。 以下是示例代码: HTML 结构: ```html <div class="lazy-load"> <div class="placeholder"></div> </div> ``` JavaScript 代码: ```javascript // 获取需要懒加载的元素 const lazyLoadEl = document.querySelector('.lazy-load'); // 获取占位符元素和真实数据元素 const placeholderEl = lazyLoadEl.querySelector('.placeholder'); const realDataEl = document.createElement('div'); // 监听页面滚动事件 window.addEventListener('scroll', () => { // 获取元素的位置和尺寸信息 const lazyLoadRect = lazyLoadEl.getBoundingClientRect(); const viewportHeight = window.innerHeight; // 判断元素是否进入可视范围内 if (lazyLoadRect.top < viewportHeight) { // 动态加载数据 const data = loadLazyData(); // 渲染数据到占位符元素中 realDataEl.innerHTML = data; lazyLoadEl.replaceChild(realDataEl, placeholderEl); } }); function loadLazyData() { // 加载数据的逻辑 } ``` 需要注意的是,在实际开发中,还需要对滚动事件进行节流或防抖处理,以避免频繁触发滚动事件导致性能问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值