列表视图项目点击事件应用程序[英] List view itemclick event appcelerator

本文是小编为大家收集整理的关于列表视图项目点击事件应用程序的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在代码中定义了一个JSON,标题,一个日期和URL.这将用于填充列表视图.在任何列表项的Click事件上,我需要导航到其他控制器,并呈现它具有播放视频的VideoPlayer,根据点击的列表项的URL播放视频,我也希望显示日期和标题. 我不了解如何在itemClick事件中编写此操作.请帮忙!

这是仪表板控制器的.js文件.

Alloy.createController('VideoPlayerController',videoInfo[i]).getView();
var sections = [];
//JSON to populate the listview
var videoInfo = [{
    pic : "/Images/playButton.png",
    info : "This in the the title for the first video.",
    date : "03/07/2017",
    url : "https://youtu.be/zkOSR0ZRb9khttps://youtu.be/zkOSR0ZRb9k"
}, {
    pic : "/Images/playButton.png",
    info : "This in the the title for the second video.",
    date : "03/07/2017",
    url : "https://youtu.be/zkOSR0ZRb9khttps://youtu.be/zkOSR0ZRb9k"
}, {
    pic : "/Images/playButton.png",
    info : "This in the the title for the third video.",
    date : "03/07/2017",
    url : "https://youtu.be/zkOSR0ZRb9khttps://youtu.be/zkOSR0ZRb9k"
}];

function populateListView() {
    Ti.API.trace("[DashboardController.js  >>  populateListView() >>]");
    if (!_.isEmpty(videoInfo)) {
        for (var i = 0; i < videoInfo.length; i++) {
            var item = {
                template : "videoTemplate",
                iconId : {
                    image : videoInfo[i].pic
                },
                titleId : {
                    text : videoInfo[i].info
                },
                dateId : {
                    text : videoInfo[i].date
                },
                urlId : {
                    text : videoInfo[i].url
                },
                properties : {
                    backgroundColor : "transparent"
                }
            };
            sections.push(item);
        }
       $.listSection.setItems(sections);
    }
}  
populateListView();
$.lView.addEventListener('click',function(e){
    var dataItem = $.listSection.getItemAt(e.itemIndex);    
});

和仪表板控制器的.xml文件是:

<Alloy>
    <View id="win2" class="container">
        <View id = "v1" class="view1"  layout ="horizontal" >
            <Button class="btnBack" ></Button>
            <Label  class= "label1">LIST VIEW EXAMPLE</Label>
        </View>
        <View class="view2">
            <TextField id = "txtSearch"></TextField>
        </View>
        <ListView id = "lView" class= "list1" >
             <Templates>
                    <ItemTemplate name="videoTemplate">
                        <View class= "viewTemplate" layout = "horizontal" >
                            <ImageView bindId="iconId"  id="pic"  />
                            <View class= "viewTitle" layout = "vertical" >
                                <Label bindId="titleId" id="info" />
                                <View class="viewDtUrl" layout="horizontal" >
                                    <Label bindId="dateId" id="date" />
                                    <Label bindId="urlId" id ="url" /> 
                                </View>
                            </View>
                        </View>
                    </ItemTemplate> 
            </Templates>
            <ListSection id = "listSection">
            </ListSection>
        </ListView>   
    </View>
</Alloy> 

这是将呈现视频播放器(VideoPlayerController)的控制器的.xml文件是:

<Alloy>
    <View class="container">
        <VideoPlayer class= "video"></VideoPlayer>
        <View class="videoView">
            <Label class="titleInfo"></Label>
            <View class= "infoLabel">
                <Label class="dateInfo"></Label>
                <Label class="urlInfo"></Label>
            </View>>
        </View>
    </View>
</Alloy>

推荐答案

你非常接近.这里有几个项目:

  1. 将DashboarController.js中的第一行移动到lView事件侦听器中.
  2. 我们要侦听的ListView事件称为 itemclick ,不是click,因此改变.
  3. videoInfo数组用作我们的数据收集. itemclick回调将返回单击的行索引,这将匹配我们videoInfo数组的顺序,因此我们可以将其传递给createController为:videoInfo[e.itemIndex]
  4. 最后,createController正在返回一个与.getView()的视图,在我们的情况下,这可能是TiUIWindow对象,因此我们需要使用.open()打开该窗口.

这给了我们:

$.lView.addEventListener('itemclick', function (e) {
    Alloy.createController('VideoPlayerController', videoInfo[e.itemIndex]).getView().open();
});
现在在我们的VideoPlayerController.js中,我们将有类似的东西:

var args = $.args;
console.log('video url:' + args.url);

从那里,您可以使用传递给args的数据来设置窗口的其余部分.

本文地址:https://www.itbaoku.cn/post/1938226.html

问题描述

I have a JSON defined in the code with and image, a title, a date and a url. This will be used to populate the list view. On the click event of any list item, I need to navigate to a different controller and render the view in it having the videoplayer that plays the video according to the clicked list item's url and i want to display the date and title too. I am not understanding how to code this in the itemClick event. Please help!

This is my .js file for DashboardController.

Alloy.createController('VideoPlayerController',videoInfo[i]).getView();
var sections = [];
//JSON to populate the listview
var videoInfo = [{
    pic : "/Images/playButton.png",
    info : "This in the the title for the first video.",
    date : "03/07/2017",
    url : "https://youtu.be/zkOSR0ZRb9khttps://youtu.be/zkOSR0ZRb9k"
}, {
    pic : "/Images/playButton.png",
    info : "This in the the title for the second video.",
    date : "03/07/2017",
    url : "https://youtu.be/zkOSR0ZRb9khttps://youtu.be/zkOSR0ZRb9k"
}, {
    pic : "/Images/playButton.png",
    info : "This in the the title for the third video.",
    date : "03/07/2017",
    url : "https://youtu.be/zkOSR0ZRb9khttps://youtu.be/zkOSR0ZRb9k"
}];

function populateListView() {
    Ti.API.trace("[DashboardController.js  >>  populateListView() >>]");
    if (!_.isEmpty(videoInfo)) {
        for (var i = 0; i < videoInfo.length; i++) {
            var item = {
                template : "videoTemplate",
                iconId : {
                    image : videoInfo[i].pic
                },
                titleId : {
                    text : videoInfo[i].info
                },
                dateId : {
                    text : videoInfo[i].date
                },
                urlId : {
                    text : videoInfo[i].url
                },
                properties : {
                    backgroundColor : "transparent"
                }
            };
            sections.push(item);
        }
       $.listSection.setItems(sections);
    }
}  
populateListView();
$.lView.addEventListener('click',function(e){
    var dataItem = $.listSection.getItemAt(e.itemIndex);    
});

And the .xml file for DashboardController isthis :

<Alloy>
    <View id="win2" class="container">
        <View id = "v1" class="view1"  layout ="horizontal" >
            <Button class="btnBack" ></Button>
            <Label  class= "label1">LIST VIEW EXAMPLE</Label>
        </View>
        <View class="view2">
            <TextField id = "txtSearch"></TextField>
        </View>
        <ListView id = "lView" class= "list1" >
             <Templates>
                    <ItemTemplate name="videoTemplate">
                        <View class= "viewTemplate" layout = "horizontal" >
                            <ImageView bindId="iconId"  id="pic"  />
                            <View class= "viewTitle" layout = "vertical" >
                                <Label bindId="titleId" id="info" />
                                <View class="viewDtUrl" layout="horizontal" >
                                    <Label bindId="dateId" id="date" />
                                    <Label bindId="urlId" id ="url" /> 
                                </View>
                            </View>
                        </View>
                    </ItemTemplate> 
            </Templates>
            <ListSection id = "listSection">
            </ListSection>
        </ListView>   
    </View>
</Alloy> 

This is the .xml file of the controller that will be rendering the video player (VideoPlayerController) is this:

<Alloy>
    <View class="container">
        <VideoPlayer class= "video"></VideoPlayer>
        <View class="videoView">
            <Label class="titleInfo"></Label>
            <View class= "infoLabel">
                <Label class="dateInfo"></Label>
                <Label class="urlInfo"></Label>
            </View>>
        </View>
    </View>
</Alloy>

推荐答案

You are very close. Here are a couple of items:

  1. Move the first line in DashboarController.js, Alloy.createController('VideoPlayerController',videoInfo[i]).getView(); down into the lView event listener.
  2. The listview event we want to listen for is called itemclick, not click, so change that.
  3. The videoInfo array is serving as our data collection. The itemclick callback will be returned the index of the row clicked, and this will match the order of our videoInfo array, so we can just pass it to createController as: videoInfo[e.itemIndex]
  4. Lastly, createController is returning a view with .getView(), in our case, this probably a TiUIWindow object, so we need to open that window with .open().

This gives us:

$.lView.addEventListener('itemclick', function (e) {
    Alloy.createController('VideoPlayerController', videoInfo[e.itemIndex]).getView().open();
});

Now in our VideoPlayerController.js, we'll have something like:

var args = $.args;
console.log('video url:' + args.url);

From there, you can use the data passed to args to set up the rest of your window.