Android中ListView实现子控件点击事件后ListView点击事件失效解决办法

http://blog.csdn.net/lxzh12345/article/details/9526483

最近有人问我,在Android里:

在自定义listView里面有按钮,然后setItemClickListener时不响应,网上很多关于这个的,但我都试过,貌似都解决不了。
其实刚开始学Android接触ListView时我也遇到此问题,在网上到处搜也难以找到相关解决方案,原因之一是网上原创文章少,转载居多,很多文章都雷同;原因之二在于自己英语不好,不敢上外文论坛去找。
后来经过自己的慢慢摸索终于解决了这个问题。
当朋友问我时,我一番简单讲解加几张代码截图就给他解决问题啦~
废话不多说,先上源代码: 源代码
代码片段:
main.xml
[html]  view plain copy print ?
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@drawable/background" >  
  6.   
  7.     <ListView  
  8.         android:id="@+id/lv_list"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_below="@+id/numxback"  
  13.         android:cacheColorHint="#00000000"  
  14.         android:drawSelectorOnTop="false"  
  15.         android:fadingEdge="none"  
  16.         android:fastScrollEnabled="true"  
  17.         android:focusable="false"  
  18.         android:divider="@null"  
  19.         android:focusableInTouchMode="true"  
  20.         android:listSelector="@drawable/itemselected" />  
  21.   
  22. </RelativeLayout>  
在main布局里加入了一个列表控件,ListView,它的各个属性在此处不是重点,这里就不多说,看不懂的读者自己去网上搜吧!
datalist.xml
[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/widget_datalist"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:descendantFocusability="blocksDescendants">  
  7.   
  8.     <TextView  
  9.         android:id="@+id/tv_num"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="60px"  
  12.         android:layout_centerVertical="true"  
  13.         android:background="@drawable/number"  
  14.         android:gravity="center"  
  15.         android:paddingLeft="5px"  
  16.         android:paddingRight="8px"  
  17.         android:textColor="@android:color/white"  
  18.         android:textSize="24sp" />  
  19.   
  20.     <ImageButton  
  21.         android:id="@+id/ib_edit"  
  22.         android:layout_width="60px"  
  23.         android:layout_height="60px"  
  24.         android:layout_alignParentRight="true"  
  25.         android:background="@drawable/edit"  
  26.         <span style="color:#ff0000">android:onClick="OnItemEditClick"  
  27. </span>        android:paddingRight="5px" />  
  28.   
  29.     <TextView  
  30.         android:id="@+id/tv_numx"  
  31.         android:layout_width="150px"  
  32.         android:layout_height="60px"  
  33.         android:layout_toRightOf="@+id/tv_num"  
  34.         android:background="@drawable/number"  
  35.         android:gravity="center_vertical"  
  36.         android:singleLine="true"  
  37.         android:textColor="@android:color/white"  
  38.         android:textSize="24sp" />  
  39.   
  40.     <TextView  
  41.         android:id="@+id/tv_numy"  
  42.         android:layout_width="150px"  
  43.         android:layout_height="60px"  
  44.         android:layout_toRightOf="@id/tv_numx"  
  45.         android:layout_toLeftOf="@id/ib_edit"  
  46.         android:background="@drawable/number"  
  47.         android:gravity="center_vertical"  
  48.         android:singleLine="true"  
  49.         android:textColor="@android:color/white"  
  50.         android:textSize="24sp" />  
  51.   
  52. </RelativeLayout>  
列表控件单项布局,在里面加入了三个TextView,一个ImageButton,显示时三个TextView 在前面,最后一个ImageButton。
之前说到的按钮点击事件在Activity里用button.setOnClickListener的方式实现时会屏蔽/占用ListView单项单击事件,这里就在布局里加了一个 android:onClick="OnItemEditClick" ,用于绑定按钮点击事件,然后在java代码中实现这个方法,记得须有一个View类型的参数。
MainActivity.java
[java]  view plain copy print ?
  1. package com.test.customlistview;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import android.os.Bundle;  
  7. import android.os.Handler;  
  8. import android.os.Message;  
  9. import android.app.Activity;  
  10. import android.app.AlertDialog;  
  11. import android.content.DialogInterface;  
  12. import android.content.DialogInterface.OnClickListener;  
  13. import android.view.View;  
  14. import android.widget.AdapterView;  
  15. import android.widget.ImageButton;  
  16. import android.widget.ListView;  
  17. import android.widget.RelativeLayout;  
  18. import android.widget.SimpleAdapter;  
  19. import android.widget.TextView;  
  20. import android.widget.Toast;  
  21. import android.widget.AdapterView.OnItemLongClickListener;  
  22.   
  23. public class MainActivity extends Activity {  
  24.       
  25.     ListView dataListView;  
  26.     SimpleAdapter listAdapter;  
  27.     ArrayList<HashMap<String, Object>> list;  
  28.     private int location;  
  29.     Handler mHandler = new Handler() {  
  30.   
  31.         @Override  
  32.         public void handleMessage(Message msg) {  
  33.             if (msg.arg1 == 1) {  
  34.   
  35.             } else if (msg.arg1 == 2) {  
  36.   
  37.             } else if (msg.arg1 == 4) {  
  38.                 if (msg.what == 1) {  
  39.                     dataListView.getChildAt(msg.arg2).setBackgroundResource(  
  40.                             R.drawable.itemback);  
  41.                 }  
  42.             }  
  43.             super.handleMessage(msg);  
  44.         }  
  45.     };  
  46.     @Override  
  47.     protected void onCreate(Bundle savedInstanceState) {  
  48.         super.onCreate(savedInstanceState);  
  49.         setContentView(R.layout.main);  
  50.         dataListView = (ListView) findViewById(R.id.lv_list);  
  51.         list = new ArrayList<HashMap<String, Object>>();  
  52.         int count = 20;  
  53.         HashMap<String, Object> map;  
  54.         for (int i = 1; i <= count; i++) {  
  55.             map = new HashMap<String, Object>();  
  56.             map.put("num", i+ "");  
  57.             map.put("a1""²=");  
  58.             map.put("a2"""+i*i);  
  59.             list.add(map);  
  60.         }  
  61.         listAdapter = new SimpleAdapter(this, list, R.layout.datalist,  
  62.                 new String[] { "num""a1""a2" }, new int[] { R.id.tv_num,  
  63.                         R.id.tv_numx, R.id.tv_numy });  
  64.         dataListView.setAdapter(listAdapter);  
  65.         dataListView.setOnItemLongClickListener(new OnItemLongClickListener() {  
  66.             public boolean onItemLongClick(AdapterView<?> arg0, View view,  
  67.                     int position, long id) {  
  68.                 location = position;  
  69.                 final View nowView = (View) view.findViewById(R.id.ib_edit);  
  70.                 Toast.makeText(getBaseContext(), "location=" + location, 2000)  
  71.                         .show();  
  72.                 OnClickListener listener = new DialogInterface.OnClickListener() {  
  73.                     public void onClick(DialogInterface dialog, int which) {  
  74.                         if (which == 0) {  
  75.                             Toast.makeText(getBaseContext(), "编辑", Toast.LENGTH_LONG).show();  
  76.                         } else if (which == 1) {  
  77.                             Toast.makeText(getBaseContext(), "删除", Toast.LENGTH_LONG).show();  
  78.                         }  
  79.                     }  
  80.                 };  
  81.                 dataListView.getChildAt(location).setBackgroundResource(  
  82.                         R.drawable.item_frame);  
  83.                 Message message = new Message();  
  84.                 message.arg1 = 4;  
  85.                 message.arg2 = location;  
  86.                 message.what = 1;  
  87.                 mHandler.sendMessageDelayed(message, 500);  
  88.                 String[] Menu = { "编辑""删除" };  
  89.               
  90.                 new AlertDialog.Builder(MainActivity.this).setItems(Menu,  
  91.                         listener).show();  
  92.                 return false;  
  93.             }  
  94.         });  
  95.     }  
  96.       
  97.     public void OnItemEditClick(View v) {  
  98.         RelativeLayout layout = (RelativeLayout) v.getParent();  
  99.         TextView tv1=(TextView)layout.findViewById(R.id.tv_num);  
  100.         TextView tv2=(TextView)layout.findViewById(R.id.tv_numx);  
  101.         TextView tv3=(TextView)layout.findViewById(R.id.tv_numy);  
  102.         ImageButton ib=(ImageButton)layout.findViewById(R.id.ib_edit);  
  103.         String str="按钮"+tv1.getText()+"点击啦\n"+tv1.getText()+tv2.getText()+tv3.getText();  
  104.         Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();  
  105.     }  
  106. }  
OnItemEditClick(View v)方法中通过v.getParent();找到她的容器控件,前面的(RelativeLayout)根据你自己的布局里的容器控件而定。然后即可使用该容器控件找到他的所有自控件,然后读者想干嘛就干嘛啦~
上面的dataListView.setOnItemLongClickListener实现ListView的item长按事件,我在这里加了一个显示两条菜单的对话框,用于进行其他操作,如编辑、删除等。
这里也可以实现ListView的点击事件,PS:这段代码是后来加的,直接嵌入源代码中即可,已调试无误。
 
[java]  view plain copy print ?
  1. dataListView.setOnItemClickListener(new OnItemClickListener() {  
  2.   
  3.             @Override  
  4.             public void onItemClick(AdapterView<?> arg0, View view, int arg2,  
  5.                     long arg3) {  
  6.                 TextView tv=(TextView)view.findViewById(R.id.tv_num);  
  7.                 Toast.makeText(getBaseContext(), "您点击了第" + tv.getText()+"行"2000).show();  
  8.             }  
  9.         });  
修改后的MainActivity.java代码如下
[java]  view plain copy print ?
  1. package com.test.customlistview;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import android.os.Bundle;  
  7. import android.os.Handler;  
  8. import android.os.Message;  
  9. import android.app.Activity;  
  10. import android.app.AlertDialog;  
  11. import android.content.DialogInterface;  
  12. import android.content.DialogInterface.OnClickListener;  
  13. import android.view.View;  
  14. import android.widget.AdapterView;  
  15. import android.widget.AdapterView.OnItemClickListener;  
  16. import android.widget.ImageButton;  
  17. import android.widget.ListView;  
  18. import android.widget.RelativeLayout;  
  19. import android.widget.SimpleAdapter;  
  20. import android.widget.TextView;  
  21. import android.widget.Toast;  
  22. import android.widget.AdapterView.OnItemLongClickListener;  
  23.   
  24. public class MainActivity extends Activity {  
  25.       
  26.     ListView dataListView;  
  27.     SimpleAdapter listAdapter;  
  28.     ArrayList<HashMap<String, Object>> list;  
  29.     private int location;  
  30.     Handler mHandler = new Handler() {  
  31.   
  32.         @Override  
  33.         public void handleMessage(Message msg) {  
  34.             if (msg.arg1 == 1) {  
  35.   
  36.             } else if (msg.arg1 == 2) {  
  37.   
  38.             } else if (msg.arg1 == 4) {  
  39.                 if (msg.what == 1) {  
  40.                     dataListView.getChildAt(msg.arg2).setBackgroundResource(  
  41.                             R.drawable.itemback);  
  42.                 }  
  43.             }  
  44.             super.handleMessage(msg);  
  45.         }  
  46.     };  
  47.     @Override  
  48.     protected void onCreate(Bundle savedInstanceState) {  
  49.         super.onCreate(savedInstanceState);  
  50.         setContentView(R.layout.main);  
  51.         dataListView = (ListView) findViewById(R.id.lv_list);  
  52.         list = new ArrayList<HashMap<String, Object>>();  
  53.         int count = 20;  
  54.         HashMap<String, Object> map;  
  55.         for (int i = 1; i <= count; i++) {  
  56.             map = new HashMap<String, Object>();  
  57.             map.put("num", i+ "");  
  58.             map.put("a1""²=");  
  59.             map.put("a2"""+i*i);  
  60.             list.add(map);  
  61.         }  
  62.         listAdapter = new SimpleAdapter(this, list, R.layout.datalist,  
  63.                 new String[] { "num""a1""a2" }, new int[] { R.id.tv_num,  
  64.                         R.id.tv_numx, R.id.tv_numy });  
  65.         dataListView.setAdapter(listAdapter);  
  66.         dataListView.setOnItemClickListener(new OnItemClickListener() {  
  67.   
  68.             @Override  
  69.             public void onItemClick(AdapterView<?> arg0, View view, int arg2,  
  70.                     long arg3) {  
  71.                 TextView tv=(TextView)view.findViewById(R.id.tv_num);  
  72.                 Toast.makeText(getBaseContext(), "您点击了第" + tv.getText()+"行"2000).show();  
  73.             }  
  74.         });  
  75.         dataListView.setOnItemLongClickListener(new OnItemLongClickListener() {  
  76.             public boolean onItemLongClick(AdapterView<?> arg0, View view,  
  77.                     int position, long id) {  
  78.                 location = position;  
  79.                 final View nowView = (View) view.findViewById(R.id.ib_edit);  
  80.                 Toast.makeText(getBaseContext(), "location=" + location, 2000)  
  81.                         .show();  
  82.                 OnClickListener listener = new DialogInterface.OnClickListener() {  
  83.                     public void onClick(DialogInterface dialog, int which) {  
  84.                         if (which == 0) {  
  85.                             Toast.makeText(getBaseContext(), "编辑", Toast.LENGTH_LONG).show();  
  86.                         } else if (which == 1) {  
  87.                             Toast.makeText(getBaseContext(), "删除", Toast.LENGTH_LONG).show();  
  88.                         }  
  89.                     }  
  90.                 };  
  91.                 dataListView.getChildAt(location).setBackgroundResource(  
  92.                         R.drawable.item_frame);  
  93.                 Message message = new Message();  
  94.                 message.arg1 = 4;  
  95.                 message.arg2 = location;  
  96.                 message.what = 1;  
  97.                 mHandler.sendMessageDelayed(message, 500);  
  98.                 String[] Menu = { "编辑""删除" };  
  99.               
  100.                 new AlertDialog.Builder(MainActivity.this).setItems(Menu,  
  101.                         listener).show();  
  102.                 return false;  
  103.             }  
  104.         });  
  105.     }  
  106.       
  107.     public void OnItemEditClick(View v) {  
  108.         RelativeLayout layout = (RelativeLayout) v.getParent();  
  109.         TextView tv1=(TextView)layout.findViewById(R.id.tv_num);  
  110.         TextView tv2=(TextView)layout.findViewById(R.id.tv_numx);  
  111.         TextView tv3=(TextView)layout.findViewById(R.id.tv_numy);  
  112.         ImageButton ib=(ImageButton)layout.findViewById(R.id.ib_edit);  
  113.         String str="按钮"+tv1.getText()+"点击啦\n"+tv1.getText()+tv2.getText()+tv3.getText();  
  114.         Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();  
  115.     }  
  116. }  

注:这个小demo根本算不上自定义ListView,只是稍微给他丰富了一下子元素,以实现更好看的界面效果,当然,我这个不好看哈,只是为了写博文的演示罢了。
运行截图:
截图一 截图二  截图三
 
代码都贴出来了,说明文字不多,但应该基本说清楚了,读者慢慢看吧,主要还是研读代码,尝试着慢慢修改变成自己的并从中学到一些东西才是重点!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值