【问题标题】:Listview for Nativescript 7Nativescript 7 的列表视图
【发布时间】:2021-01-03 07:17:21
【问题描述】:

Listview 是否可以与带有 Angular 的 Nativescript 7 一起使用我仍然可以看到它需要 tns-core 模块?

自从升级以来,我在nativescript-ui-listview 上收到了很多errors

【问题讨论】:

    标签: nativescript nativescript-angular nativescript-ui-listview


    【解决方案1】:

    在 NS7 中,您必须使用 @nativescript/core 而不是 tns-core 保留的复古兼容性。

    这意味着您必须将所有出现的 tns-core 替换为 @nativescript/core。

    ListView 有效。

    【讨论】:

    • 我也要编辑 node_modules,因为有些喜欢 listview,当您安装它时,它已经附带 tns-core-modules 作为依赖项
    • 我注意到我必须修改 listview reference.d.ts 文件引用以使其工作,但是如果我清理项目,我必须继续编辑该文件
    【解决方案2】:

    我让 RadListView 进行渲染,但滑动没有停靠,并且按钮在滑动后没有消失。这是我正在使用的版本,同时遵循一些教程:

    "dependencies": {
        "@angular/animations": "~11.0.0",
        "@angular/common": "~11.0.0",
        "@angular/compiler": "~11.0.0",
        "@angular/core": "~11.0.0",
        "@angular/forms": "~11.0.0",
        "@angular/platform-browser": "~11.0.0",
        "@angular/platform-browser-dynamic": "~11.0.0",
        "@angular/router": "~11.0.0",
        "@fortawesome/fontawesome-free": "^5.15.1",
        "@nativescript/angular": "~11.0.0",
        "@nativescript/core": "~7.1.0",
        "@nativescript/theme": "~3.0.0",
        "nativescript-toasty": "^3.0.0-alpha.2",
        "nativescript-ui-listview": "^9.0.4",
        "nativescript-ui-sidedrawer": "^9.0.3",
        "reflect-metadata": "~0.1.12",
        "rxjs": "^6.6.0",
        "zone.js": "~0.11.1"
      }

    就像 Robertino 提到的,您必须从 @nativescript/core 而不是 tns-core-modules 导入。查看组件文件:

    import { Component, OnInit, Inject, ViewChild } from '@angular/core';
    import { FavoriteService } from '../services/favorite.service';
    import { Dish } from '../shared/dish';
    import { ListViewEventData, RadListView, SwipeActionsEventData } from 'nativescript-ui-listview';
    import { RadListViewComponent } from 'nativescript-ui-listview/angular';
    import { ObservableArray } from '@nativescript/core/data/observable-array';
    import { View } from '@nativescript/core/ui/core/view';
    
    import { confirm } from "@nativescript/core/ui";
    import { ToastDuration, ToastPosition, Toasty } from 'nativescript-toasty';
    
    @Component({
        selector: 'app-favorites',
        moduleId: module.id,
        templateUrl: './favorites.component.html',
        styleUrls: ['./favorites.component.css']
    })
    export class FavoritesComponent implements OnInit {
    
        favorites: ObservableArray<Dish>;
        errMess: string;
    
        @ViewChild('myListView') listViewComponent: RadListViewComponent;
    
        constructor(private favoriteservice: FavoriteService,
            @Inject('baseURL') private baseURL) {
        }
    
        ngOnInit() {
            this.favoriteservice.getFavorites()
                .subscribe(favorites => this.favorites = new ObservableArray(favorites),
                    errmess => this.errMess = errmess);
        }
    
        deleteFavorite(id: number) {
            console.log('delete', id);
    
            let options = {
                title: "Confirm Delete",
                message: 'Do you want to delete Dish '+ id,
                okButtonText: "Yes",
                cancelButtonText: "No",
                neutralButtonText: "Cancel"
            };
    
            confirm(options).then((result: boolean) => {
                if(result) {
    
                this.favorites = null;
    
                this.favoriteservice.deleteFavorite(id)
                    .subscribe(favorites => { 
                        const toast = new Toasty({
                            text:"Deleted Dish "+ id, 
                            duration: ToastDuration.SHORT, 
                            position: ToastPosition.BOTTOM
                        });
                        toast.show();
                        this.favorites = new ObservableArray(favorites);
                    },
                    errmess => this.errMess = errmess);
                }
                else {
                console.log('Delete cancelled');
                }
            });
        }
    
        public onCellSwiping(args: ListViewEventData) {
            var swipeLimits = args.data.swipeLimits;
            var currentItemView = args.object;
            var currentView;
    
            if(args.data.x > 200) {
    
            }
            else if (args.data.x < -200) {
    
            }
        }
    
        public onSwipeCellStarted(args: SwipeActionsEventData) {
            const swipeLimits = args.data.swipeLimits;
            const swipeView = args['object'];
            const leftItem = swipeView.getViewById<View>('mark-view');
            const rightItem = swipeView.getViewById<View>('delete-view');
            swipeLimits.left = leftItem.getMeasuredWidth();
            swipeLimits.right = rightItem.getMeasuredWidth();
            swipeLimits.threshold = leftItem.getMeasuredWidth()/2;
        }
    
        public onSwipeCellFinished(args: ListViewEventData) {
    
        }
    
        public onLeftSwipeClick(args: ListViewEventData) {
            console.log('Left swipe click');
            this.listViewComponent.listView.notifySwipeToExecuteFinished();
        }
    
        public onRightSwipeClick(args: ListViewEventData) {
            this.deleteFavorite(args.object.bindingContext.id);
            this.listViewComponent.listView.notifySwipeToExecuteFinished();
        }
    }

    这是模板:

    <ActionBar title="My Favorites" class="action-bar">
    </ActionBar>
    <StackLayout class="page">
        <RadListView #myListView [items]="favorites" *ngIf="favorites"
            selectionBehavior="none" (itemSwipeProgressEnded)="onSwipeCellFinished($event)"
            (itemSwipeProgressStarted)="onSwipeCellStarted($event)"
            (itemSwipeProgressChanged)="onCellSwiping($event)"
            swipeActions="true">
            <ListViewLinearLayout tkListViewLayout scrollDirection="vertical"
                itemInsertAnimation="Default" itemDeleteAnimation="Default">
            </ListViewLinearLayout>
            <ng-template tkListItemTemplate let-item="item">
                <StackLayout orientation="horizontal" class="listItemStackLayout">
                    <Image row="0" col="0" rowSpan="2" height="60" width="60"
                        [src]="baseURL + item.image" class="thumb p-16"></Image>
                    <GridLayout rows="auto, *" columns="*">
                        <Label row="0" col="0" [text]="item.name" class="labelName"></Label>
                        <Label row="1" col="0" [text]="item.description" class="labelText" textWrap="true"></Label>
                    </GridLayout>
                </StackLayout>
            </ng-template>
            <GridLayout *tkListItemSwipeTemplate columns="auto, * , auto" class="listItemSwipeGridLayout">
                <StackLayout id="mark-view" class="markViewStackLayout" col="0"
                    (tap)="onLeftSwipeClick($event)">
                    <Label text="&#xf08d;" class="swipetemplateLabel fas"
                        verticalAlignment="center" horizontalAlignment="center"></Label>
                </StackLayout>
                <StackLayout id="delete-view" class="deleteViewStackLayout" col="2"
                    (tap)="onRightSwipeClick($event)">
                    <Label text="&#xf1f8;" class="swipetemplateLabel fas"
                        verticalAlignment="center" horizontalAlignment="center"></Label>
                </StackLayout>
            </GridLayout>
        </RadListView>
        <ActivityIndicator busy="true" *ngIf="!(favorites || errMess)" width="50"
            height="50" class="activity-indicator"></ActivityIndicator>
        <Label *ngIf="errMess" [text]="'Error: ' + errMess"></Label>
    
    </StackLayout>

    还有 css 类:

    .listItemStackLayout {
        background-color: white;
        padding: 10;
    }
    
    .labelName {
        font-size: 20;
        font-weight: bold;
        margin-bottom: 8;
        margin-left: 16;
    }
    
    .labelTitle {
        font-size: 14;
        font-weight: bold;
    }
    
    .labelText {
        font-size: 12;
        margin-left: 16;
    }
    
    .markViewStackLayout {
        background-color: blue;
        padding: 16;
    }
    
    .deleteViewStackLayout {
        background-color: red;
        padding: 16;
    }
    
    .listItemSwipeGridLayout {
        background-color: white;
    }
    
    .swipetemplateLabel {
        size: 20;
        font-size: 28;
        color: white;
    }

    您可能需要删除几行代码才能正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多