是否可以在列表视图中获取抽头项的上下文?例如,如果我有一个包含三个项目的列表,我如何判断它是第二个单击项目并从对象中获取与可观察数组中该项目相关的数据?
EX:这是我的列表模板,如果用户点击连接列表中的第二个连接,我如何根据点击的项目索引获取与该连接相关的数据?
<ListView items="{{ connections }}" loaded="" itemLoading="" itemTap="">
<ListView.itemTemplate>
<StackLayout class='connection-li'>
<GridLayout class="connection-info" rows="" columns="auto, *, *" tap="goToMeasurements">
<Image col="0" src="res://ic_person_black_36dp" stretch ="none" />
<Label col="1" class="connection-name" text="{{ PatientFirstName + ' ' + PatientLastName }}" textWrap="false" />
<Image col="2" verticalAlignment="middle" horizontalAlignment="right" src="res://ic_cancel_black_18dp" stretch ="none" />
</GridLayout>
</StackLayout>
</ListView.itemTemplate>
</ListView>
编辑: 根据文档,我发现了以下可以挂钩的事件来获取索引数据,但是它引用了ListView,我如何能够对我感兴趣的那个具体引用,我可以给它一个类吗?并以这种方式访问它?
listView.on(listViewModule.ListView.itemTapEvent, function (args: listViewModule.ItemEventData) {
var tappedItemIndex = args.index;
var tappedItemView = args.view;
//// Do someting
});
答案 0 :(得分:6)
您不需要单独的类,只需要一个与此页面对应的javascript文件(即main-page.xml和main-page.js)。然后,您可以在itemTap
属性中连接事件:
<ListView items="{{ connections }}" itemTap="onConnectionTapped">
然后从相应的javascript文件中导出该事件,您可以通过几种不同的方式访问该项的数据对象:
export function onConnectionTapped(args) {
var tappedView = args.view,
//the View will have a bindingContext
// set to the individual item from the list
tappedItem = tappedView.bindingContext;
//or, if you need the entire list as well,
// get it from the Page's bindingContext
// as each View has a ref to the Page it's on
var pageBindingContext = tappedView.page.bindingContext,
fullItemsList = pageBindingContext.connections,
itemForTap = fullItemsList[args.index];
}
答案 1 :(得分:2)
"ProjectB": "1.0.0"
处理程序class Widget
{
private:
char* pDataText { NULL };
int idNumber { 0 };
public:
void Setup() { pDataText = new char[100]; }
~Widget() { delete pDataText; }
void Reset()
{
Widget blankWidget;
this->~Widget(); // Manually delete the current object using the dtor
*this = blankObject; // Copy a blank object to the this-object.
}
};
将goToMeasurements
。其args.object
属性将是GridLayout
数组中的数据项。如果您需要索引,.bindingContext
将返回此数据项的索引。