Android ScrollView doesn't start at top, but at the beginning of the GridView

后端 未结 12 2435
天涯浪人
天涯浪人 2020-12-23 11:19

I have a problem with a ScrollView that has inside of it a personalized GridView and other tipe of views.The first time I start the Activity, the ScrollView starts at its to

相关标签:
12条回答
  • 2020-12-23 12:01

    The easiest way is to add on the parent ScrollView the following xml attributes:

    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    

    That means that the ScrollView as a whole will be the one getting focus instead of any inner container when you launch the activity. This is also useful, for example, when you have an edit text inside your layout and you don't want it to get focus immediately and popup the keyboard when entering a screen (It's the same principle).

    So, your ScrollView on top of your layout would look like this:

    <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" >
    
    0 讨论(0)
  • 2020-12-23 12:03

    ScrollView

    android:fitsSystemWindows="false"
    
    0 讨论(0)
  • 2020-12-23 12:05

    Solution:

    Ok sorry if it is a late reply but I stumbled upon the same issue (only that I was using ListView instead) and with a bit of trial and error I found the solution to this:

    Basically the problem lies in the fact that the GridView/ListView child automatically requests parent focus (ScrollView) when you "hack" and resize its content with ExpandableHeightGridView, which happens after layout is rendered, and hence you see that animation when trying to scroll it up with scrollTo() (happens AFTER layout is created and AFTER gridView is resized so any generic callback is useless to handle this programatically).

    So then, the simplest solution I found was to simply disable focusable property on the ListView/GridView with:

    listView.setFocusable(false);
    

    That way when you first enter the activity, focus will default and not rely on Listview/GridView.

    And all working fine =)

    0 讨论(0)
  • 2020-12-23 12:07

    If nothing works, just put this as the first element of your scrollView's single child:

    <EditText
       android:layout_width="0dp"
       android:layout_height="0dp"/>
    

    example:

      <ScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
            <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                <EditText
                        android:layout_width="0dp"
                        android:layout_height="0dp"/>
                <!-- your other views  -->
            </LinearLayout>
    </ScrollView>
    
    0 讨论(0)
  • 2020-12-23 12:09

    Simply add this two line in your parent layout

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

    Try this, Hope it will work for you

    0 讨论(0)
  • 2020-12-23 12:09

    Sadly the example you followed is extremely poor. ListViews and GridViews should never be placed into ScrollViews.

    ScrollView's purpose is to give infinite height to its child view. List/GridView's purpose is to take a potentially very large data set and only generate enough item views to fill the space made available to it at a time for efficiency. Both are capable of scrolling their content without the other.

    Putting a List/GridView in a ScrollView is saying, "unstoppable force, meet immovable object." Either you've defeated the point of List/GridView or you've defeated the point of ScrollView by combining them.

    Better ways to include other content within the same scrolling region include using header/footer views with a ListView, or otherwise concatenating the contents of list adapters together into a single adapter. Creating ListView items that contain several items per row to form a grid for part of an adapter's content is straightforward.

    0 讨论(0)
提交回复
热议问题