煎茶触摸幻灯片导航项上的单击事件

Sencha Touch click event on slide nav item

本文关键字:单击 事件 导航 触摸 幻灯片      更新时间:2023-09-26

我这里有两个问题:

  1. 单击幻灯片导航中的列表项 加载新页面内容
  2. 单击屏幕底部的主页图标,将使用带回主视图

我按照以下代码示例获取Facebook喜欢的菜单

  1. 步骤:描述的步骤
  2. Git 代码:存储库网址
  3. 演示
  4. 站点:教程演示网址

我成功地按照演示使滑动菜单工作,但我无法为菜单和主页图标中的所有项目设置单击事件。

这就是我想做的。应用程序/视图/导航.js(根据演示代码结构(

Ext.define('SlideNav.view.Navigation', {
                extend: 'Ext.List',
                xtype: 'navigation',
                requires : ['Ext.data.Store'],
                config: {
                                cls : 'nav-list',
                                itemTpl : '{title}',
                                data : [{
                                                title : 'About'
                                },{
                                                title : 'Item 2'
                                },{
                                                title : 'Item 3'
                                }]
                },
 // This part is new
    listeners: {
            itemtap: function (list, index, item, record) {
                //Ext.Msg.alert('Selected Link', record.data.itemId);
                switch (record.data.itemId) {
                    default: {
                        this.onAboutButtonTap();
                        break;
                    }
                }
            }
        },
        onAboutButtonTap: function () {
            this.fireEvent("goToAboutMeCommand", this);
        }
    });

单击此列表中的每个项目后,我想在页面上加载新内容。到目前为止,我能够做到这一点:app/controller/app.js(根据 Git 结构(

 refs: {
            main : 'main',
            navigation : 'navigation',
            navBtn: 'button[name="nav_btn"]',
            aboutView: "AboutViewType"
        },
 navigationView: {
                    itemtap: function (list, index, target, record) {
                        this.toggleNav();
                    },
                   // This is new line
                    goToAboutMeCommand: "onGoToAboutMeCommand"
                },
.....
  ......
 onGoToAboutMeCommand: function () {
        console.log("onGoToAboutMeCommand");
        this.showAboutMe();
    },
showAboutMe: function () {
        Ext.Viewport.setActiveItem(this.getAboutView());
    }

当用户单击幻灯片菜单中的任何项目时,它将触发">onAboutButtonTap"并加载 AboutView 内容,但导航菜单在此之后不起作用,不确定我是否这样做正确。此外,如果您看到 GIT 代码和演示,演示上有主页图标,我想为该主页图标附加一个点击事件,当用户单击它时,它应该进入主视图。

这是我的应用程序/视图/关于视图.js

Ext.define('SlideNav.view.AboutView', {
    extend: 'Ext.TabPanel',
    xtype: 'AboutViewType',
    config: {
        tabBarPosition: 'bottom',
        items: [{
            title: 'Home',
            iconCls: 'home',
            xtype: 'formpanel',
            html: ['About me page'].join(''),
            styleHtmlContent: true,
            items: [{
                xtype: 'titlebar',
                title: 'Page Title',
                docked: 'top',
                items: [{
                    align: 'left',
                    name: 'nav_btn',
                    iconCls: 'list',
                    ui: 'plain'
                }]
            }]
        }]
    }
});

根据@Anand建议更新:

 navigationView: {
                itemtap: function (list, index, target, record) {
                    this.navigationHandler(record);
                    this.toggleNav();
                }
 navigationHandler: function (record) {
        switch (record.get('action')) {
            default:
                {
                    Ext.Msg.alert("Action Name", record.get('action'));
                    Ext.Viewport.setActiveItem(this.getAboutView(), this.slideRightTransition);
                }
        }
    },

用关于我的内容更改了页面的内容并且看起来不错,但是在左上角我有这个幻灯片菜单图标,当我单击它时,它不会触发toggleNav()功能

注意到的问题

在下面的函数中,我正在添加加载良好的"关于"视图,但我没有将导航视图加载到屏幕上,这就是为什么我看不到那里的导航视图项目的原因。如何在将两个视图设置为活动状态时将两个视图加载到视口上。

navigationHandler: function (record( { switch (record.get('action'(( { 违约: { Ext.Viewport.setActiveItem(this.getAboutView((, this.slideRightTransition(; Ext.Viewport.add(this.getNavigationView((, this.slideRightTransition(; } } },

在给定的存储库中,您可以在控制器中看到 App.js 文件。有

navigation : {
        itemtap : function(list, index, target, record){
                      this.toggleNav(); 
                      this.navigationHandler(record);       // ADDED BY ANAND for handling the navigation                                                                           
                  }
            }

添加 navigationHandler 方法,通过该方法可以导航到不同的视图,如下所示:

navigationHandler: function(record) {
        switch(record.get('action')) {
            case 'about': // Do what you want
                         .......
        }
}

--------------编辑-------------------------------

据我了解,您无法在顶部工具栏上的导航按钮上切换。为此,您可以看到应用程序.js文件,其中

refs:{
        main : 'main',
        navigation : 'navigation',
        navBtn : 'button[name="nav_btn"]'
     },
control : {
        navBtn : {
             tap : 'toggleNav' // THIS IS responsible for toggling ON TAP OF NAV BUTTON
                 },
        navigation : {
            itemtap : function(list, index, target, record){
                            this.toggleNav();   
                            this.navigationHandler(record);                                  
                           }
                     }
         }

navigationHandler 中,您可以像下面这样对不同的视图进行动画处理:

parentPanel.animateActiveItem(childPanel, animation);

注意:父面板应采用card布局。

祝您编码愉快! :)

这就是我如何使其完全工作。

应用.js

Ext.Loader.setPath({
    'Ext': 'Lib/touch-2.4.1/src',
    'HaBoMobile': 'app'
});
Ext.application({
    name: 'HaBoMobile',
    controllers: ['AppController'],
    launch: function () {
    }
});

应用控制器.js

Ext.define('HaBoMobile.controller.AppController', {
    extend: 'Ext.app.Controller',
    config: {
        views: ['NavigationMenu','MainView', 'AboutView'],
        refs: {
            nav: {
                selector: 'navigationMenuType',
                xtype: 'navigationMenuType',
                autoCreate: true
            },
            main: {
                selector: 'mainViewType',
                xtype: 'mainViewType',
                autoCreate: true
            },
            about: {
                selector: 'aboutViewType',
                xtype: 'aboutViewType',
                autoCreate: true
            }
        },
        control: {
            'viewport': {
                beforehidemenu: 'beforeHideNav'
            },
            'button[action=nav]': {
                tap: 'showNav'
            },
            'button[ui=decline]': {
                tap: 'hideNav'
            },
            'button[action=nav_option]': {
                tap: 'handleNavigation'
            }
        }
    },
    init: function () {
        var navMenu = this.getNav();
        Ext.Viewport.setMenu(navMenu, {
            side: 'left',
            reveal: true
        });
        Ext.Viewport.add(this.getMain());
    },
    showNav: function () {
        Ext.Viewport.showMenu('left');
        Ext.fly('appLoadingIndicator').destroy();
    },
    beforeHideNav: function () {
        console.log('beforehidemenu returning false');
        // returning false will prevent the hide
        return false;
    },
    hideNav: function () {
        Ext.Viewport.hideMenu('left');
    },
    handleNavigation: function (btn) {
        switch (btn.getItemId()) {
            case 'aboutLink':
                var aboutView = Ext.Viewport.child('aboutViewType');
                if (!aboutView) {
                    aboutView = this.getAbout();
                    Ext.Viewport.add(aboutView);
                }
                Ext.Viewport.setActiveItem(aboutView);
                break;
            default:
                var mainView = Ext.Viewport.child('mainViewType');
                if (!mainView) {
                    mainView = this.getMain();
                    Ext.Viewport.add(mainView);
                }
                Ext.Viewport.setActiveItem(mainView);
        }
        this.hideNav();
    }
});

视图/导航菜单.js

Ext.define('HaBoMobile.view.NavigationMenu', {
    extend:'Ext.Menu',
    xtype: 'navigationMenuType',
    config: {
        scrollable: {
            direction: 'vertical',
            directionLock: true
        },
        width: 180,  // Need to set a width 
        items: [{
            text: 'Close',
            ui: 'decline'
        }, {
            action: 'nav_option',
            itemId: 'homeLink',
            title: 'Home',
            iconCls: 'home',
            text: 'Home'
        }, {
            action: 'nav_option',
            itemId: 'aboutLink',
            title: 'About Me',
            iconCls: 'info',
            text: 'About'
        }]
    }
});

主视图.js

Ext.define('HaBoMobile.view.MainView', {
    extend: 'Ext.Container',
    xtype: 'mainViewType',
    html: ['Welcome to my Main page'].join(''),
    config: {
        scrollable: {
            direction: 'vertical',
            directionLock: true
        },
        fullscreen: true,
        items: [{
            docked: 'top',
            xtype: 'titlebar',
            title: 'Harsha Bopuri',
            items: [{
                text: 'Menu',
                action: 'nav',
                iconCls: 'list'
            }]
        }]
    }
});

关于视图.js

Ext.define('HaBoMobile.view.AboutView', {
    extend: 'Ext.Container',
    xtype: 'aboutViewType',
    html:'Learn more about me',
    config: {
        scrollable: {
            direction: 'vertical',
            directionLock: true
        },
        fullscreen: true,
        items: [{
            docked: 'top',
            xtype: 'titlebar',
            items: [{
                text: 'Menu',
                action: 'nav',
                iconCls: 'list'
            }]
        }]
    }
});

索引.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Harsha Bopuri</title>
    <script src="Lib/touch-2.4.1/sencha-touch-debug.js"></script>
    <link href="Lib/touch-2.4.1/resources/css/sencha-touch.css" rel="stylesheet" />
    <script src="app.js"></script>
    <script src="Lib/touch-2.4.1/microloader/development.js"></script>
    <link href="app/styles.css" rel="stylesheet" />
</head>
<body>
    <div id="appLoadingIndicator">
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
</html>

希望此解决方案对使用Sencha touch寻找滑出菜单的人有用

我还有一篇文章谈到jQuery Mobile与Sencha Touch