How to implement a Non scrollable listview inside a SingleChildScrollView
class _DashboardState extends State<Dashboard> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('App Bar Here')),
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Hello World'),
Container(
child: ListView.builder(
physics: NeverScrollableScrollPhysics() //add this line,
itemBuilder: (context, index) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
Container(
color: Color(0xffaaaaaa),
height: 20,
child: Text('Jss One')),
Text(
'English',
style: TextStyle(fontSize: 20),
),
],
),
),
);
},
itemCount: 50,
),
),],),));
}}
set physics property to NeverScrollablePhysics() in order to not scroll the lisview
ListView inside ScrollView is not scrolling on Android
You shouldn't put a ListView
inside a ScrollView
because the ListView
class implements its own scrolling and it just doesn't receive gestures because they all are handled by the parent ScrollView
. I strongly recommend you to simplify your layout somehow. For example you can add views you want to be scrolled to the ListView
as headers or footers.
UPDATE:
Starting from API Level 21 (Lollipop) nested scroll containers are officially supported by Android SDK. There're a bunch of methods in View
and ViewGroup
classes which provide this functionality. To make nested scrolling work on the Lollipop you have to enable it for a child scroll view by adding android:nestedScrollingEnabled="true"
to its XML declaration or by explicitly calling setNestedScrollingEnabled(true)
.
If you want to make nested scrolling work on pre-Lollipop devices, which you probably do, you have to use corresponding utility classes from the Support library. First you have to replace you ScrollView
with NestedScrollView. The latter implements both NestedScrollingParent and NestedScrollingChild so it can be used as a parent or a child scroll container.
But ListView
doesn't support nested scrolling, therefore you need to subclass it and implement NestedScrollingChild
. Fortunately, the Support library provides NestedScrollingChildHelper class, so you just have to create an instance of this class and call its methods from the corresponding methods of your view class.
How to get a non scrollable ListView?
Don't put a listview in a scrollview, it doesn't work. If you want a list of items that doesn't scroll, it's called a linearlayout.
Android list view inside a scroll view
For any Child view to scroll inside a ScrollView. Anything like ListView, RecyclerView, etc. You just have to replace ScrollView with androidx.core.widget.NestedScrollView in your current xml and then magic happens.
Below is a sample xml code :
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
android:paddingBottom="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Recycler View inside a Scroll View"
android:textColor="@color/black"
android:textSize="@dimen/_20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Below is a Recycler View as an example."
android:textSize="16sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@id/et_damaged_qty" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="This textview automatically goes below the Recycler View."
android:textSize="16sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.core.widget.NestedScrollView>
Now you can get rid of all the ugly hacks you did to get around with nested scrolling.
ScrollView not scrollable when contains ListViews
As scrollview and listview are not supporting nested scrolling you can not go with them. Either you can have your custom scrollview for this or
You can go with nestedscrollview instead of scrollview and Recyclerview instead of listview.
how to place a listview inside a SingleChildScrollView but prevent them from scrolling separately?
As suggested by other answers, you can do that by setting shrinkWrap: true, physics: NeverScrollableScrollPhysics()
, but building a shrinkwrapped listview is more expensive than building a normal listview, I think it will be a good idea to use a map() on the list.
Column(
children: <Widget>[
...itemList.map((item) {
return Text(item);
}).toList(),
],
),
Related Topics
How to Create a Table with Borders in Android
How to Take a Screenshot of a Current Activity and Then Share It
Static Variable Null When Returning to the App
Using "Printf" for Hex Values in Android Shell
Jelly Bean Datepickerdialog --- How to Cancel
Is a Relativelayout More Expensive Than a Linearlayout
How to View Androidmanifest.Xml from APK File
How to Make a Phone Call from HTML on Android
Android Webview, How to Handle Redirects in App Instead of Opening a Browser
How to Set Textview Textstyle Such as Bold, Italic
Why I'm Getting Duplicate Class When Running My Android Project
Android Ndk: Getting the Backtrace
Design Android Edittext to Show Error Message as Described by Google
How to Create and Read Log on Android Devices
Webview's HTML Button Click Detection in Activity
Detect from Browser If a Specific Application Is Installed in Android