I have this layout xml file for an activity which uses a listview. It works fine except it always starts from the top of the listview. Not from the top of the layout. I tested
listView.setSelection(0);
It didn't work for me. How can I display the screen from the top?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="开发者_如何学Chttp://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="@+id/topLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/darkbackground"
>
<include layout="@layout/subtitle_bar" />
<TextView
android:id="@+id/rankHistory"
android:text="@string/rankHistory"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20px"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#ffffffff"
android:gravity="center"
android:layout_gravity="center_horizontal"
></TextView>
<ListView android:id="@+id/list"
android:layout_marginTop="15px"
android:layout_width="fill_parent"
android:layout_height="500px"
android:dividerHeight="15px"
android:divider="@color/darkbackground"
>
</ListView>
</LinearLayout>
</ScrollView>
try this:
listView.smoothScrollToPosition(0);
Here you can download sample project http://www.hrupin.com/2011/10/14/how-to-scroll-listview-to-top-or-other-position
Hope, it help you!
You shouldn't put a ListView in a ScrollView. Put the items above the ListView in a separate xml file and then add them as a header to the ListView by using listView.addHeaderView(view);
精彩评论