Listview Scroll to the End of the List After Updating the List

Listview Scroll to the end of the list after updating the list

Supposing you know when the list data has changed, you can manually tell the list to scroll to the bottom by setting the list selection to the last row. Something like:

private void scrollMyListViewToBottom() {
myListView.post(new Runnable() {
@Override
public void run() {
// Select the last row so it will scroll into view...
myListView.setSelection(myListAdapter.getCount() - 1);
}
});
}

How to scroll ListView to the bottom?

Try using list.scrollTo(0, list.getHeight());

Scroll down refreshes my list from all positions

in your onScroll method, if firstVisibleItem == 0 set swipe enable otherwise it should remain disable as you should only swipe to refresh if you are on top of the list.

Programmatically scrolling to the end of a ListView

If you use a shrink-wrapped ListView with reverse: true, scrolling it to 0.0 will do what you want.

import 'dart:collection';

import 'package:flutter/material.dart';

void main() {
runApp(new MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Example',
home: new MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
List<Widget> _messages = <Widget>[new Text('hello'), new Text('world')];
ScrollController _scrollController = new ScrollController();

@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Container(
decoration: new BoxDecoration(backgroundColor: Colors.blueGrey.shade100),
width: 100.0,
height: 100.0,
child: new Column(
children: [
new Flexible(
child: new ListView(
controller: _scrollController,
reverse: true,
shrinkWrap: true,
children: new UnmodifiableListView(_messages),
),
),
],
),
),
),
floatingActionButton: new FloatingActionButton(
child: new Icon(Icons.add),
onPressed: () {
setState(() {
_messages.insert(0, new Text("message ${_messages.length}"));
});
_scrollController.animateTo(
0.0,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 300),
);
}
),
);
}
}

Can't scroll down to the last item in the ListView

You have 25dp margins for listView, setting 15dp to your textView android:layout_marginTop="15dp" and 10dp to your listView.

By making your listView at the bottom of your textView app:layout_constraintTop_toBottomOf="@id/pageHeading"

That's adding 15dp + 10dp.

There is many tricks to avoid that:

  • Add margin bottom to listView 25dp

  • remove unimportant margins

  • Add bottom padding to your main layout

You handle it on your own.

Your layout should be like:

   <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="25dp"
>

<TextView
android:id="@+id/pageHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Student Information"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:paddingTop="10dp"
android:gravity="center_horizontal"
android:textStyle="bold"/>

<ListView
android:id="@+id/studentInfoList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
app:layout_constraintTop_toBottomOf="@id/pageHeading"
android:paddingTop="10dp"
android:dividerHeight="7dp">
</ListView>

</android.support.constraint.ConstraintLayout>

Flutter - Update ListView and reset scroll position

here you have to provide key to the ListView to differentiate two different listview. if you do not provide key then flutter think that both are same list view and it keep tracking listview using index of it.

what could be solution in your case if we consider first element of list as a key.

....
child: ListView(
key: ObjectKey(_list[0]),
children: _list
.....


Related Topics



Leave a reply



Submit