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

use*_*149 54 java android gridview scrollview

我有一个ScrollView的问题,里面有个性化的GridView和其他视图.第一次启动Activity时,ScrollView从顶部开始,但是如果我访问Activity其他时候ScrollView从头开始GridView.I使用了此链接中为我的GridView 找到的类ExpandableHeightGridView .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>
Run Code Online (Sandbox Code Playgroud)

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

    scrollView.post(new Runnable() 
      { 
         public void run() { 
             scrollViewLuogo.fullScroll(ScrollView.FOCUS_UP); 
         } 
      });
Run Code Online (Sandbox Code Playgroud)

但它从GridView的顶部到屏幕的顶部进行快速动画,我不喜欢它.

有什么建议??

Sup*_*ngs 173

解:

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

基本上问题在于GridView/ListView子项在您"hack"并使用ExpandableHeightGridView调整其内容时自动请求父焦点(ScrollView),这在渲染布局后发生,因此您在尝试滚动它时会看到该动画使用scrollTo()(在创建布局之后发生,并且在调整gridView后调整大小,因此任何通用回调都无法以编程方式处理此问题).

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

listView.setFocusable(false);
Run Code Online (Sandbox Code Playgroud)

这样,当您第一次进入活动时,焦点将默认,而不依赖于Listview/GridView.

一切正常=)

  • 这也适用于RecyclerView (4认同)
  • 太棒了,谢谢.我想知道为什么如果我在xml中设置它不起作用. (3认同)
  • 这应该被认为是正确的答案 (2认同)
  • 也适用于 webview (2认同)

Dis*_*ion 21

最简单的方法是在父ScrollView上添加以下xml属性:

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
Run Code Online (Sandbox Code Playgroud)

这意味着当你启动活动时,ScrollView作为一个整体将成为焦点,而不是任何内部容器.这也很有用,例如,当您在布局中有编辑文本并且您不希望它立即获得焦点并在进入屏幕时弹出键盘(这是相同的原则).

因此,布局顶部的ScrollView将如下所示:

<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:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:background="#fff" >
Run Code Online (Sandbox Code Playgroud)

  • 不适合我,我在scrollview内有一个recyclerview (3认同)
  • RecyclerView 是一个特例,和 ListView 一样。通常,将 2 个可滚动容器放在一个内部(滚动视图中的 ListView 或 RecyclerView)是一系列问题和问题,导致没有简单的解决方案。如果在 RecyclerView 和 ScrollView 中有 EditText,事情会变得非常糟糕,并且简单的解决方案(后代可聚焦性)并不容易应用,部分原因是滚动容器本身是如何实现的。 (3认同)

Sau*_*war 6

只需在父级布局中添加这两行

android:focusable =“ true” android:focusableInTouchMode =“ true”

试试这个,希望它对你有用


Pia*_*asy 5

如果你想以编程方式将它滚动到顶部/底部,你可以这样做(它来自这里):

对于焦点向下:

((ScrollView) findViewById(R.id.scrollView)).post(new Runnable() { 
    public void run() { 
        ((ScrollView) findViewById(R.id.scrollView)).fullScroll(View.FOCUS_DOWN); 
    } 
});
Run Code Online (Sandbox Code Playgroud)

专注于顶部

((ScrollView) findViewById(R.id.scrollView)).post(new Runnable() { 
    public void run() { 
        ((ScrollView) findViewById(R.id.scrollView)).fullScroll(View.FOCUS_UP); 
    } 
});
Run Code Online (Sandbox Code Playgroud)


小智 5

只需在ScrollView的子级中添加此行代码

android:focusableInTouchMode="true"
Run Code Online (Sandbox Code Playgroud)