Nativescript ListView scrollToIndex with animation on Android

I am developing a chat application when a list is loading and when a new item is added to the list, I need to scroll to the end. I can do it with this.

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

But it shows the bottom of the instant list

There is a guide to do this 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);
     }
}

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

Is there any way to do this on Android?

+4
source share
1 answer

smoothScrollToPosition android, ListView, . .

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>

-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)
}
+3

Source: https://habr.com/ru/post/1666131/


All Articles