Disable Scrolling in Listview

Flutter: ListView disable scrolling with touchscreen

As mentioned in the comments, the NeverScrollableScrollPhysics class will do this:

NeverScrollableScrollPhysics class

Scroll physics that does not allow the user to scroll.

Disable ListView Scroll

I think the best way to do this is to use a SingleChildScrollView:

This widget is useful when you have a single box that will normally be
entirely visible, for example a clock face in a time picker, but you
need to make sure it can be scrolled if the container gets too small
in one axis (the scroll direction).

And instead of using a ListView just use a Column and place it inside of the SingleChildScrollView:

    SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[/**/],
),
)

Or if you need to use ListView for some reason, you can use shrinkWrap with NeverScrollableScrollPhysics:

    SingleChildScrollView(
child: ListView(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
children: <Widget>[/**/],
),
)

programmatically prevent scroll listview flutter

If you want to set it programatically, use a setState() to update the physics type like this:

ScrollPhysics physics = AlwaysScrollableScrollPhysics();

return Stack(
children: [
ListView(
physics: physics,
),
RaisedButton(
child: Text("Disable scrolling"),
onPressed: () {
setState((){
if(physics is AlwaysScrollableScrollPhysics){
physics = NeverScrollableScrollPhysics();
} else {
physics = AlwaysScrollableScrollPhysics();
}
});
},
),
]
);

Disable scrolling in listview

In your CustomListView:

@Override
public boolean dispatchTouchEvent(MotionEvent ev){
if(ev.getAction()==MotionEvent.ACTION_MOVE)
return true;
return super.dispatchTouchEvent(ev);
}

Then ListView will react to clicks, but will not change scroll position.

DIsable scrolling for listview and enable for whole layout

You can try this.

FOR xml PART DO THIS:

Put your entire layout data under one Scroll View, for example:

    <ScrollView
android:id="@+id/scrollViewId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> // SAY YOUR FIRST LIST VIEW:

<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> // SAY YOUR SECONDLIST VIEW:

// Add your other views as per requirement....

</LinearLayout>

</ScrollView>

NOW IN JAVA CLASS DO THE FOLLOWING THING...

Just add this custom method to your code after setting adapter to list view:

setListViewHeightBasedOnChildren(listview)

For Example:

      list = (ListView) findViewById(R.id.listview);
list.setAdapter(new ArrayAdapter<String>
(MainActivity.this,android.R.layout.simple_list_item_1,name));
setListViewHeightBasedOnChildren(list);

Do it same for second list view too.

Here is body of setListViewHeightBasedOnChildren METHOD

   public static void setListViewHeightBasedOnChildren(ListView listView) 
{
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;

int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight=0;
View view = null;

for (int i = 0; i < listAdapter.getCount(); i++)
{
view = listAdapter.getView(i, view, listView);

if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
LayoutParams.MATCH_PARENT));

view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();

}

ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount()));

listView.setLayoutParams(params);
listView.requestLayout();

}

Hope it works for you.

ListView reaches end then stop scrolling and start scrolling the rest of the page Flutter

just set physics: ClampingScrollPhysics() to the child ListView. It creates scroll physics that prevent the scroll offset from exceeding the bounds of the content. More about ListView and its physics refer to : https://medium.com/flutter-community/flutter-listview-and-scrollphysics-a-detailed-look-7f0912df2754



Related Topics



Leave a reply



Submit