1

I'm trying to figure out why this LinearLayout is not scrolling:

<ScrollView
    android:layout_width="match_parent"
    android:fillViewport="true"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent">

   <LinearLayout
    android:id="@+id/linearLayout"
    android:orientation="vertical"
    android:weightSum="2"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="35dp"
        android:text="TITLE 1"
        android:textSize="30sp" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20sp" />

</LinearLayout>
</ScrollView>

I'm adding programmatically a textView and a listView to the linearLayout, and the page is cut in half. The only things scrolling are the listViews.

I don't need the listViews to be scrollable, I only want the entire linearLayout scrollable.

l000000l
  • 330
  • 1
  • 3
  • 14

2 Answers2

0

Try this code

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:fillViewport="true"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent">

   <LinearLayout
    android:id="@+id/linearLayout"
    android:orientation="vertical"
    android:weightSum="2"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="35dp"
        android:text="TITLE 1"
        android:textSize="30sp" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20sp" />

</LinearLayout>
</androidx.core.widget.NestedScrollView>
Công Hải
  • 4,961
  • 3
  • 14
  • 20
0

The listview doesn't scroll because it is nested. If you use listview inside Scrollview, they might interrupt each other. (both scrolls) You should customize your Scrollview, so the scrollview will be stop scrolling when you touch listview area.

1) Create new class: CustomScrollView and extend it from ScrollView

2) Change your xml <ScrollView> to your custom scrollview: <com.your_package_name.CustomScrollView>

CustomScrollView.java:


import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class CustomScrollView extends ScrollView {
    public CustomScrollView(Context context) {
        super(context);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomScrollView(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("CustomScrollView", "onInterceptTouchEvent: DOWN super false" );
                super.onTouchEvent(ev);
                break;

            case MotionEvent.ACTION_MOVE:
                return false; // redirect MotionEvents to ourself

            case MotionEvent.ACTION_CANCEL:
                // Log.i("CustomScrollView", "onInterceptTouchEvent: CANCEL super false" );
                super.onTouchEvent(ev);
                break;

            case MotionEvent.ACTION_UP:
                //Log.i("CustomScrollView", "onInterceptTouchEvent: UP super false" );
                return false;

            default:
                //Log.i("CustomScrollView", "onInterceptTouchEvent: " + action );
                break;
        }

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        //Log.i("CustomScrollView", "onTouchEvent. action: " + ev.getAction() );
        return true;
    }
}

Dilshod
  • 147
  • 4
  • 12
  • I tried, but it's not scrolling... I copied your class and changed xml – l000000l May 19 '20 at 09:38
  • I updated my question with one more detail – l000000l May 19 '20 at 09:49
  • I understood you. But you have only 2 view, how can you scroll it if you have nothing after listview? You can one way: Disable the listView -> https://stackoverflow.com/questions/7611085/disable-scrolling-in-listview – Dilshod May 20 '20 at 00:29
  • As I wrote in the post: "I'm adding programmatically a textView and a listView to the linearLayout" ... I finally find a solution, I posted it as an answer. – l000000l May 20 '20 at 01:07