Android ScrollView不是从顶部开始,而是在GridView的开始处 [英] Android ScrollView doesn't start at top, but at the beginning of the GridView
问题描述
我有一个ScrollView的问题,里面有一个个性化的GridView和其他视图。第一次启动Activity时,ScrollView从顶部开始,但如果我在ScrollView启动的其他时间访问Activity在GridView的开头。我使用这个链接中为我的GridView找到的类ExpandableHeightGridView。
活动布局的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 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>
我试过使用代码 但是它可以从GridView顶部到屏幕顶部进行快速动画,不喜欢。 任何建议?? 解决方案: 好的抱歉,如果它是一个迟到的回复,但我偶然发现了同样的问题(只是我使用的是ListView)而且有一些试验和错误<强>我找到了这个解决方案: 基本上问题在于,当你在自动请求父级焦点(ScrollView) hack,然后使用 ExpandableHeightGridView 调整其内容大小,这是在渲染布局后发生的,因此当您尝试使用scrollTo()向上滚动时会看到该动画(发生在创建布局并调整了gridView大小后)所以任何泛型回调都无法处理这个编程)。 那么,我发现的最简单的解决方案就是禁用可聚焦的p roperty在ListView / GridView上: 通过这种方式,当您第一次输入活动时,焦点将默认,而不依赖Listview / GridView。 / p> 而且所有的工作都很好=) 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 top, but if I visit the Activity other times the ScrollView starts at the beginning of the GridView.I used the class ExpandableHeightGridView found in this link for my GridView.
The xml code for the Activity layout is this one: I've tried using the code but it makes a fast animation from the top of the GridView to the top of the screen and i don't like it. Any suggestion?? 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: That way when you first enter the activity, focus will default and not rely on Listview/GridView. And all working fine =) 这篇关于Android ScrollView不是从顶部开始,而是在GridView的开始处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! scrollView.fullScroll(ScrollView.FOCUS_UP);
但它没有工作,甚至与 scrollView.scrollTo(0,0); code>我没有成功。
唯一能够工作的代码是:
$ b $ pre $ scrollView.post(new Runnable()
{
public void run(){
scrollViewLuogo.fullScroll(ScrollView.FOCUS_UP);
}
});
listView.setFocusable(false);
<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);
but it didn't work.And even with scrollView.scrollTo(0, 0);
I didn't have success.
The only code that worked was: scrollView.post(new Runnable()
{
public void run() {
scrollViewLuogo.fullScroll(ScrollView.FOCUS_UP);
}
});
listView.setFocusable(false);