Search code examples
androidandroid-layoutandroid-listviewandroid-scrollview

screen freezing - list view in a scrollview android


i read several blogs\so\forum posts and see that there is clearly an issue with putting a ListView in a ScrollView. But somewhere i found that it should work if i put a linear layout(which as the list) in the scrollview and now it will work. below is my xml. but the lists do not scroll up\down, its frozen... any idea why?

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical" android:fillViewport="true">
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:orientation="vertical">
        <LinearLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:orientation="horizontal">

            <ListView android:id="@+id/listView1" android:layout_width="100dp"
                android:layout_height="wrap_content" android:background="@color/white"
                android:cacheColorHint="#00000000" />
            <HorizontalScrollView android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ListView android:id="@+id/listView2" android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:background="@color/white"
                    android:cacheColorHint="#00000000" />
            </HorizontalScrollView>
        </LinearLayout>

    </LinearLayout>
</ScrollView>

Solution

  • I modified the xml layout and also modified the onScroll listener. It works perfectly fine now.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <LinearLayout android:layout_width="fill_parent"
        android:id="@+id/mainLayout" android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <ListView android:id="@+id/listView1" android:layout_width="100dp"
            android:layout_height="wrap_content" android:background="@color/white"
            android:cacheColorHint="#00000000" android:smoothScrollbar="true"
            android:scrollbars="none" />
        <HorizontalScrollView android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <ListView android:id="@+id/listView2" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:background="@color/white"
                android:cacheColorHint="#00000000" android:smoothScrollbar="true"
                android:scrollbarStyle="outsideOverlay" />
        </HorizontalScrollView>
    </LinearLayout>
    

    lv1.setOnScrollListener(new OnScrollListener() {
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                int index = firstVisibleItem;
                View v = view.getChildAt(0);
                int top = (null == v) ? 0 : v.getTop();
                Log.i("lv1","index:"+index+" top:"+top);
                lv1.setSelection(index);
                lv2.setSelectionFromTop(index, top);
            }
    
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                String sState;
                switch (scrollState) {
                case OnScrollListener.SCROLL_STATE_FLING:
                    sState = "Fling";
                    break;
                case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                    sState = "Touch Scroll";
                    break;
                case OnScrollListener.SCROLL_STATE_IDLE:
                    sState = "Idle";
                    break;
                default:
                    sState = "Unknown";
                    break;
                }
                          }
        });