gpt4 book ai didi

java - Android ScrollView 不是从顶部开始,而是在 GridView 的开头

转载 作者:IT老高 更新时间:2023-10-28 20:28:29 26 4
gpt4 key购买 nike

我有一个 ScrollView 的问题,它里面有一个个性化的 GridView 和其他 View 提示。我第一次启动 Activity 时,ScrollView 从顶部开始,但如果我在其他时间访问 Activity,则 ScrollView 启动在 GridView 的开头。我使用了 this link 中的类 ExpandableHeightGridView对于我的 GridView。Activity布局的xml代码是这样的:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollViewLuogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_marginLeft="14dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:adjustViewBounds="true"
android:maxHeight="200dp"
android:maxWidth="200dp"
android:scaleType="fitXY"
android:src="@android:drawable/ic_menu_gallery" />

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical" >

<TextView
android:id="@+id/nomeTVActivityLuogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="17sp"
android:textColor="#005788" />

<TextView
android:id="@+id/indirizzoTVActivityLuogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout2"
android:layout_marginTop="10dp"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageViewMappaLuogo"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:layout_marginTop="5dp"
android:scaleType="fitXY"
android:src="@drawable/sfondo_mappa" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:text="Immagini"
android:textColor="#97d6f9" />

<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:layout_marginTop="5dp"
android:background="#97d6f9" />

<com.example.mappine.ExpandableHeightGridView
android:id="@+id/gridViewLuogo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:numColumns="3" >
</com.example.mappine.ExpandableHeightGridView>

</LinearLayout>

</RelativeLayout>

我已尝试使用代码 scrollView.fullScroll(ScrollView.FOCUS_UP);但它没有用。即使使用 scrollView.scrollTo(0, 0);我没有成功。唯一有效的代码是:

    scrollView.post(new Runnable() 
{
public void run() {
scrollViewLuogo.fullScroll(ScrollView.FOCUS_UP);
}
});

但它从 GridView 顶部到屏幕顶部制作了一个快速动画,我不喜欢它。

有什么建议吗??

最佳答案

解决方案:

好吧,抱歉,如果回复晚了,但我偶然发现了同样的问题(只是我使用的是 ListView)并且经过了一些试验和错误我找到了解决方案:

基本上问题在于,当您“破解”并使用 ExpandableHeightGridView 调整其内容的大小时,GridView/ListView 子级会自动请求父级焦点 (ScrollView),这发生在呈现布局之后,并且因此,您在尝试使用 scrollTo() 向上滚动时会看到该动画(在创建布局和调整 gridView 大小之后发生,因此任何通用回调都无法以编程方式处理此问题)。

那么,我发现的最简单的解决方案是简单地禁用 ListView/GridView 上的可聚焦属性:

listView.setFocusable(false);

这样当你第一次进入activity时,焦点会默认,不依赖于Listview/GridView。

一切正常 =)

关于java - Android ScrollView 不是从顶部开始,而是在 GridView 的开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16886077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com