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>

Answers


First of all ListView should not be inserted into the ScrollView. Putting your ListView inside the LinearLayout allows you to display it, but ScrollView eats all vertical scrolling events, so your ListView never receives any touch events.

You may want to play with ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent).


put them inside

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">


</LinearLayout>

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;
            }
                      }
    });

Need Your Help

Display an ashx image using jQuery?

asp.net jquery colorbox

I've been trying to use the jQuery plugin Colorbox to display images I have in my DB through an ashx file. Unfortunately it just spits a bunch of gibberish at the top of the page and no image. Ca...

jquery multiple dialogs on the same page

jquery

I am wondering whether is there a way to consolidate the script in the head of the document so when you have multiple dialogs, you don't have this huge chunk of your header taken up? For example, ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.