为listview的item中的元素设置onclick事件
表达能力比较差,所以现在解释一下标题的意思:
listview的列表项,点击的时候触发的是itemOnClick事件,点击后转向到A页;那么,假如在子项中有一个连接是想转到B页,我们该怎么办呢。这样能明白了吧,好,现在我说一下解决的办法:
有两种办法,一种是重写adapter,一种是在你的子项中的元素中设置onclick事件(注意,是在xml中设置onclick元素)
我只做了第一种方法,演示如下:
我们继承adapter的基类,然后重写getView
@SuppressWarnings("unchecked")
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(this.resource, null);
Map item = data.get(position);
int count = to.length;
for (int i = 0; i < count; i++) {
View v = convertView.findViewById(to);
bindView(v, item, from);
}
//下面的几行是亮点
View avatar = convertView.findViewById(R.id.row_avatar);//获取你要点击的组件
if (null != avatar) {//判断一下,省得出现什么错误
avatar.setTag(item);//我们可以利用setTag来存储数据,传给触发事件
avatar.setOnClickListener(avatarClickListener);//触发事件代码往下看
}
return convertView;
}
//这个就是触发的事件,根据自己的要求写
private OnClickListener avatarClickListener = new OnClickListener() {
@SuppressWarnings("unchecked")
public void onClick(View v) {
Map item = (Map) v.getTag();//还记得上面的那个setTag么?我们在这里给丫取出来
Intent mIntent = new Intent(AdapterWithHashMap.this.context,
MySelf.class);
mIntent.putExtra("user_id", item.get("uid").toString());
AdapterWithHashMap.this.context.startActivity(mIntent);
}
};
为listview的item中的元素设置onclick事件的更多相关文章
- react中使用map时onClick事件失效
分享一些踩过的坑 react中使用map时onClick事件失效 <span> { count.map( (item,index)=>{ return <span style= ...
- android 在 ListView 的 item 中插入 GridView 仿微信朋友圈图片显示。
转载请声明出处(http://www.cnblogs.com/linguanh/) 先上张效果图: 1,思路简述 这个肯定是要重写 baseAdapter的了,这里我分了两个数据适配器,一个是自定义的 ...
- jQuery1.9及其以上版本中动态元素on绑定事件无效解决方案
jQuery 1.9/2.0/2.1及其以上版本无法使用live函数了,然而jQuery 1.9及其以上版本提供了on函数来代替.本文讲解了jQuery on函数的使用方法,以及在使用jQuery函数 ...
- 区分listview的item和Button的点击事件
这两天修改领导通的ListView widget,在ListView中加入Button这类的有 “点击” 事件的widget,发现原来listview的itemclick居然失效了, 后来在网上查资料 ...
- 安卓Android控件ListView获取item中EditText值
可以明确,现在没有直接方法可以获得ListView中每一行EditText的值. 解决方案:重写BaseAdapter,然后自行获取ListView中每行输入的EditText值. 大概算法:重写Ba ...
- Android控件ListView获取item中EditText值
能够明白,如今没有直接方法能够获得ListView中每一行EditText的值. 解决方式:重写BaseAdapter,然后自行获取ListView中每行输入的EditText值. 大概算法:重写Ba ...
- vue 给v-html中的元素设置样式
解决方案:写样式的时候添加>>>
- android ListView子布局中按钮响应点击事件
只需要在子布局的根布局中添加以下属性即可: android:descendantFocusability="blocksDescendants"
- JavaScript例子1-给网页中所有<p>元素添加onclick事件
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
随机推荐
- 高性能高并发网络库:StateThreads
StateThreads是一个C的网络程序开发库,提供了编写高性能.高并发.高可读性的网络程序的开发库,轻量级网络应用框架 共也就3000行C代码 网络程序(Internet Application) ...
- ElasticSearch使用代码
package elasticsearch01; import static org.junit.Assert.*; import java.util.HashMap; import java.uti ...
- Hash表的表大小
hash表的出现主要是为了对内存中数据的快速.随机的访问.它主要有三个关键点:Hash表的大小.Hash函数.冲突的解决. 这里首先谈谈第一点:Hash表的大小. Hash表的大小一般是定长的,如果太 ...
- OpenMediaVault的OwnCloud扩展不支持NTFS格式硬盘
来源https://forum.openmediavault.org/index.php/Thread/15510-OwnCloud-Operation-not-supported-setfacl/ ...
- mongodb自动关闭:页面太小,无法完成操作
解决方法: 增大虚拟内存
- e682. 获得打印页的尺寸
Note that (0, 0) of the Graphics object is at the top-left of the actual page, which is outside the ...
- (转)获取android源码时repo的错误
获取android源码时repo的错误 今天用repo获取android源码:../bin/repo init -u git://android.git.kernel.org/platform/man ...
- centos 6.5配置samba
Samba简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件,而SMB是Server Message Block的缩写,即为服务器消息块 ,SMB主要是作为Microso ...
- linux 访问远程务器代码
比如用SSH 访问远程 登陆名为hadoop 的IP为192.168.1.35的主机,则用ssh hadoop@192.168.1.35,然后依据提示输入密码即可.
- Webpack vs Gulp(转载)
理想的前端开发流程 在说构建工具之前得先说说咱期望的前端开发流程是怎样的? 写业务逻辑代码(例如 es6,scss,pug 等) 处理成浏览器认识的(js,css,html) 浏览器自动刷新看到效果 ...