Search code examples
androidlistviewhorizontalscrollview

ListView in a HorizontalScrollView in Android


I have an ListView inside a HorizontalScrollView and it works perfectly fine, except for the initial load. I'm loading the data for the list view from a web service so I created a thread to do that. Once I have the data, I simply call _myListAdapter.notifyDataSetChanged(); and the data becomes visible in the ListView. But if the ListView is far off the screen, the containing HorizontalScrollView will automatically scroll to make this ListView visible. How can I call notifyDataSetChanged without making the ListView scroll into the current view?

Here's an idea of how I have my layout XML file:

<HorizontalScrollView 
    android:id="@+id/my_scrollview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:scrollbars="none">

        <LinearLayout android:id="@+id/my_layout"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal">

                <ListView android:id="@+id/my_list"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1" />

        </LinearLayout>

</HorizontalScrollView>

Solution

  • I would try making your ListView invisible until after you've called notifyDataSetChanged().

    <ListView android:id="@+id/my_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:visibility="gone"/>
    

    Then in the code:

    _myListAdapter.notifyDataSetChanged();
    ListView listView = (ListView) findViewById(R.id.my_list);
    listView.setVisibility(View.VISIBLE);
    

    Not sure if this will work... worth a shot, though.