4

I am developing a chat app, when list loaded and when a new item added to list I need to scroll to bottom of list. I can do that with this.

scrollToBottom() {
  let lv =  <ListView>frame.topmost().getViewById('messageList');
  lv.scrollToIndex(this.store.items.getValue().length - 1)
}

But it showing bottom of list instant

There is a guide to do that on IOS but not on Android

private srollListView(position: number) {
     if (this._listView.ios) {
        this._listView.ios.scrollToRowAtIndexPathAtScrollPositionAnimated(
            NSIndexPath.indexPathForItemInSection(position, 0),
            UITableViewScrollPosition.UITableViewScrollPositionTop,
            true
        );
     }
     else {
         this._listView.scrollToIndex(position);
     }
}

link to guide: http://nuvious.com/Blog/2016/4/4/how-to-make-the-nativescript-listview-scrolltoindex-animated-on-ios

Is there any way to do that on Android?

Samed
  • 136
  • 1
  • 14

2 Answers2

3

You could use smoothScrollToPosition android method, which provides smooth scroll for the ListView, which you need. I am providing sample code.

main-page.xml

<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo">
    <GridLayout>
        <ListView items="{{ source }}"  id="lvid" loaded="onLoaded" itemLoading="onItemLoading" itemTap="onItemTap">
            <ListView.itemTemplate>
                <StackLayout>
                    <Label text="{{title}}" textWrap="true" />
                </StackLayout>
            </ListView.itemTemplate>
        </ListView>
    </GridLayout>
</Page>

main-page.ts

import { EventData } from 'data/observable';
import { Page } from 'ui/page';
import { HelloWorldModel } from './main-view-model';
import {ListView} from "ui/list-view"

// Event handler for Page "navigatingTo" event attached in main-page.xml
export function navigatingTo(args: EventData) {

    let page = <Page>args.object;
    var array=[];
    for(var i=0;i<100;i++){
        array.push({title:"title"+i});
    }
    page.bindingContext = {source:array};

    setTimeout(function(){
        var listview:ListView =<ListView> page.getViewById("lvid");
        listview.android.smoothScrollToPosition(60);
    }, 4000)
}
Nikolay Tsonev
  • 1,919
  • 3
  • 16
  • 29
  • It works. Thank you. Is there any doc for this and this kind of methods. it feels like it is a secret method? I mean i dig around the doc for it a lot but was not able to find it. Where did you find this? https://docs.nativescript.org/api-reference/classes/_ui_list_view_.listview.html – Samed Jan 09 '17 at 13:31
  • 3
    This method (smoothScrollToPosition) is not nativescript's method but android one, then you'll have all the needed informations https://docs.nativescript.org/api-reference/classes/_ui_list_view_.listview.html#android about the attribute 'android' on listviews, then you'll have access to those methods https://developer.android.com/reference/android/widget/ListView.html which contains smoothScrollToPosition – Kansen Jan 09 '17 at 14:38
1

Just to let everyone who are still looking for this know, they have added the scrollToIndexAnimated method to ListView since v4.2.0

https://github.com/NativeScript/NativeScript/blob/master/CHANGELOG.md#420-2018-08-08

Community
  • 1
  • 1
codey
  • 11
  • 2