Search code examples
androidlistviewandroid-listviewcursor

A scrollable view with two ListViews inside


I have this view which contains a header (not a ListView header) with information in general, and below that should be two ListViews, populated by custom-made CursorAdapters. Now I can't put everything inside a ScrollView since you can't put a ListView inside that (it will break things and won't scroll).

Any idea? The layout is something like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    tools:context="...">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        android:paddingTop="8dp">
    <!-- A lot of things here (the header I was talking about) -->
    </RelativeLayout>

    <!-- Just a horizontal line (separator) -->
    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/gray_dark" />

    <!-- Two ListViews here -->
    <ListView
        android:id="@+id/first_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/second_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Solution

  • Use Custom class for ScrollView

    import android.content.Context;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.widget.ScrollView;
    
    public class VerticalScrollview extends ScrollView{
    
        public VerticalScrollview(Context context) {
            super(context);
        }
    
         public VerticalScrollview(Context context, AttributeSet attrs) {
                super(context, attrs);
            }
    
            public VerticalScrollview(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
            }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            final int action = ev.getAction();
            switch (action)
            {
                case MotionEvent.ACTION_DOWN:
                        Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false" );
                        super.onTouchEvent(ev);
                        break;
    
                case MotionEvent.ACTION_MOVE:
                        return false; // redirect MotionEvents to ourself
    
                case MotionEvent.ACTION_CANCEL:
                        Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false" );
                        super.onTouchEvent(ev);
                        break;
    
                case MotionEvent.ACTION_UP:
                        Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false" );
                        return false;
    
                default: Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action ); break;
            }
    
            return false;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            super.onTouchEvent(ev);
            Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction() );
             return true;
        }
    }
    

    and use it in your xml like:-

    <com.yourPackage.VerticalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        tools:context="...">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
      >
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="8dp"
            android:paddingTop="8dp">
        <!-- A lot of things here (the header I was talking about) -->
        </RelativeLayout>
    
        <!-- Just a horizontal line (separator) -->
        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:background="@color/gray_dark" />
    
        <!-- Two ListViews here -->
        <ListView
            android:id="@+id/first_listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <ListView
            android:id="@+id/second_listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    </com.yourPackage.VerticalScrollView>
    

    now your both list views should be scroll able.