问题描述
创建自定义ListViewAdapter
I create custom ListViewAdapter
在适配器我使用setOnClickListener
In adapter I use setOnClickListener
public View getView(int position, View convertView, ViewGroup parent)
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
row.setOnClickListener(new View.OnClickListener() {})
在活动创建列表视图
listView.setAdapter(new MyAdaper(this));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {})
但setOnItemClickListener没有提出如果使用setOnClickListener
But setOnItemClickListener not raised if use setOnClickListener
如果评论setOnClickListener setOnItemClickListener加薪
If comment setOnClickListener setOnItemClickListener raise
如何使这项工作,setOnClickListener和setOnItemClickListener?
How to make that work, and setOnClickListener and setOnItemClickListener?
推荐答案
如果您使用自定义listAdapter您可以设置onclickListener在你基地查看子视图(在你的例子 - 行);在调用堆栈listView.setOnItemClickListener凌驾于row.setOnclickListener。实际上,这两个产生相同的结果,因为行(在这方面)是默认的ListView一个简单的项目。
If you use custom listAdapter you may set onclickListener to child Views in you're base View(in you're example - row); In call stack listView.setOnItemClickListener stands above row.setOnclickListener. Actually both give the same result because row(in this context) is a simple item in default ListView.
因此,在这种情况下,解决方案是:
So solution in this case is :
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
((Button)row.findViewById(R.id.btn1)).setOnClickListener(new View.OnClickListener() {});
}
在上面的例子中,我使用xml文件像listView.layoutResourceId的项目包含一个按钮(R.id.btn1)。之后,你可以点击设置听众的ListView和Button.And这将是会产生不同的\\相同的结果不同的听众(这完全取决于执行)
In above example i use xml file like a item of listView.layoutResourceId contains a Button(R.id.btn1). After that you can set click listeners to ListView and to Button.And it will be different listeners that will produce different \ the same result (it all depends on the implementation)
这篇关于Android的定制ListViewAdapter使用setOnClickListener不会提高listView.setOnItemClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!