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

标签 java android gridview scrollview

我有一个 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/

相关文章:

java - 词法分析器

java - 如何传递一个类型的值并将其转换为另一种类型?

java - ListView.CHOICE_MODE_SINGLE Unresolved 引用

java - Java 三角函数

asp.net - 在 TextBox ASP.NET VB 中显示 GridView 中的选定数据

html - 将背景图像应用于引导列网格布局

java - Tesseract 适用于其他语言,但不适用于 Tess4J

对于库模块中定义的 POJO 的 @NonNull 注释构造函数参数,Android Room @Embedded 注释编译失败

android - 如何在后台捕获任何布局的图像

android - 将图片从一项 Activity 传递到另一项 Activity