在Android listview的setOnItemClickListener上设置item点击事件
我使用自定义适配器在列表视图的一行中有三个文本视图,单击该行时,我想执行所选文本视图的单击事件。
下面是我的点击事件的示例代码。在第一次单击时,监听器被设置,只有在第二次单击时才会发生实际的单击事件,我想在第一次单击本身上找到它。是否可以?
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int pos, long id) {
final Order orderBooking = (Order)adapter.getItemAtPosition(pos);
// SKU listener
final TextView tvSkuId = (TextView) view.findViewById(orderBooking.getSelectedSkuId());
tvSkuId.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
onSkuClickListener(view, orderBooking);
}
});
// SO listener
final TextView tvSoId = (TextView) view.findViewById(orderBooking.getSelectedSoId());
tvSoId.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
onSoClickListener(view, orderBooking);
}
});
// OR listener
final TextView txtOrId = (TextView) view.findViewById(orderBooking.getSelectedOrId());
//onOrClickListener(view, orderBooking);
txtOrId.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
onOrClickListener(view, orderBooking);
}
});
}
});
I have three textviews in a row of a listview using custom adapter and on click of the row, i want to perform the click event of the selected text view.
Below is my sample code for the click event. Here on the first click, the listeners are set and only on the second click the actual click event happens, I want to find this on the first click itself. Is it possible?
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int pos, long id) {
final Order orderBooking = (Order)adapter.getItemAtPosition(pos);
// SKU listener
final TextView tvSkuId = (TextView) view.findViewById(orderBooking.getSelectedSkuId());
tvSkuId.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
onSkuClickListener(view, orderBooking);
}
});
// SO listener
final TextView tvSoId = (TextView) view.findViewById(orderBooking.getSelectedSoId());
tvSoId.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
onSoClickListener(view, orderBooking);
}
});
// OR listener
final TextView txtOrId = (TextView) view.findViewById(orderBooking.getSelectedOrId());
//onOrClickListener(view, orderBooking);
txtOrId.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
onOrClickListener(view, orderBooking);
}
});
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不在初始化时设置这些 clickListener?当您将数据绑定到视图时,您应该在适配器中进行设置。您的代码是错误的,因为每次用户单击列表行时它都会初始化您的视图。
Why not just set those clickListeners in time of initialization? You should make it in your Adapter, when you bind your data to the views. Your code is wrong, cause it will initialize your views every time the user clicks a list's row.
在适配器的 getview 方法中执行此操作。为每个文本视图设置一个 onclick 监听器。
以上是画廊视图。做同样的事情,除了对文本视图做同样的事情
Do it in your adapater, in the getview method. Set an onclick listner for every text view.
The above is for a gallery view. Do the same thing except do it for a textview