我在ListVIEw的每一行都有button,而button的onClickListener.我想将onItemSelectListener添加到我的ListVIEw中.可能吗?如果是,我该怎么做?
任何帮助将不胜感激.
解决方法:
对的,这是可能的 …
在为ListvIEw创建自定义视图适配器时,您必须在按钮上添加onclickListener
你还需要在ListvIEw上添加onItemSelectListener.
它会工作.
使用ListvIEw代码作为
ListVIEw = (ListVIEw) findVIEwByID(R.ID.ListVIEw2); ListVIEw .setAdapter(new Customlistadapter (this,userIDArr)); ListVIEw.setonItemClickListener(new AdapterVIEw.OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> arg0, VIEw arg1, int position, long arg3) { Toast.makeText(Activity.this, "Item in position " + position + " clicked", Toast.LENGTH_LONG).show(); } });
并创建适配器
public class Customlistadapter extends ArrayAdapter<String>{ Activity context; public Customlistadapter (Activity context, ArrayList<String> names) { super(context, R.layout.List_item, names); this.context = context; } private class VIEwHolder { public TextVIEw Description; public button UploadBtn; } @OverrIDe public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { VIEwHolder holder; VIEw rowVIEw = convertVIEw; if (rowVIEw == null) { LayoutInflater inflater = context.getLayoutInflater(); rowVIEw = inflater.inflate(R.layout.List_item, null, true); holder = new VIEwHolder(); holder.Description = (TextVIEw) rowVIEw.findVIEwByID(R.ID.User_status); holder.UploadBtn = (button) rowVIEw.findVIEwByID(R.ID.uploadbutton); holder.UploadBtn.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { Toast.makeText(Activity.this," button clicked",Toast.LENGTH_SHORT).show(); } }); rowVIEw.setTag(holder); } else { holder = (VIEwHolder) rowVIEw.getTag(); } holder.Description.setText("U r in mIDdle"); return rowVIEw; }}
现在要处理列表项中的单击,请使用以下代码
androID:focusable="false"androID:focusableIntouchMode="false"
在创建button标签时设置这些行
它会起作用……
请告诉我您的反馈意见..
以上是内存溢出为你收集整理的我们可以在Android的ListView中同时拥有Button和onItemClick监听器吗?全部内容,希望文章能够帮你解决我们可以在Android的ListView中同时拥有Button和onItemClick监听器吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)