android Scrollview里的内容不停留在顶部的问题

本文讨论了在Android中使用ScrollView时遇到的问题,即内部的ListView导致头部布局无法在滚动后停留在顶部。通过分析,发现Listview自动获取了焦点,遮挡了ScrollView的头部。为了解决这个问题,提出了在父布局中设置特定属性的方法。

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


先说明一下:任何一个页面都应该按照布局的先后顺序来显示,我这出现的问题就是,Scrollview里面嵌套Listview的头布局一进来看不见了,直接被Listview把焦点给抢了。最先显示的是Listview的item部分。直接上图

解决代码在父布局里设置2个属性

android:focusable="true"
android:focusableInTouchMode="true"



<?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"
    tools:context="com.kyhh.youngworld.main.classification.activity.BrandListingActivity">

    <include layout="@layout/system_titlebar_layouot" />

    <com.kyhh.youngworld.widget.StickScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

	<!--这是包裹Listview头布局的父布局,在这里添加属性-->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="true"
                android:focusableInTouchMode="true">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/length_200"
                    android:src="@color/textgray2" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/brandlist_tv_attention"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/btn_pressed"
                        android:clickable="true"
                        android:padding="5dp"
                        android:text="关注"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/brandlist_tv_attentioncount"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:text="关注人数" />
                </LinearLayout>

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/btn_pressed"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:padding="10dp"
                android:tag="sticky">

                <TextView
                    android:id="@+id/brandlist_tv_integration"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="12dp"
                    android:background="@drawable/btn_pressed"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="综合排序" />

                <TextView
                    android:id="@+id/brandlist_tv_sales"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_centerInParent="true"
                    android:background="@drawable/btn_pressed"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="销量" />

                <TextView
                    android:id="@+id/brandlist_tv_price"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="12dp"
                    android:background="@drawable/btn_pressed"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="价格" />

            </RelativeLayout>

            <com.kyhh.youngworld.widget.NoScrollListview
                android:id="@+id/brandlist_xrc_goodslist"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </com.kyhh.youngworld.widget.NoScrollListview>

        </LinearLayout>

    </com.kyhh.youngworld.widget.StickScrollView>
</LinearLayout>


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值