在ListView中实现对ImageView的监听

来源:互联网 发布:按层次遍历二叉树算法 编辑:程序博客网 时间:2024/06/05 10:22
this.storeUser = (ImageView) findViewById(R.id.store_user);this.storeUser.setOnClickListener(new ViewOcl());


最近在做毕设一个android通讯录,对android知识点了解比较少,所以这次要实现在listView中的ImageView实现监听是遇到一点麻烦,无法对ImageView实现监听,虽然也写了监听函数

我的ListView中Item是这样写的

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <ImageView        android:id="@+id/user_phote"        android:layout_width="40dp"        android:layout_height="40dp"        android:layout_margin="3dp"        android:focusable="false" >    </ImageView>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TableLayout            android:layout_width="230dp"            android:layout_height="wrap_content"            android:collapseColumns="1" >            <TableRow                android:layout_width="fill_parent"                android:layout_height="wrap_content" >                <TextView                    android:id="@+id/user_name"                    android:layout_width="150dp"                    android:layout_height="20dp" />            </TableRow>            <TableRow                android:layout_width="fill_parent"                android:layout_height="wrap_content" >                <TextView                    android:id="@+id/user_tel"                    android:layout_width="150dp"                    android:layout_height="20dp"                    android:singleLine="true" />            </TableRow>        </TableLayout>        <ImageView            android:id="@+id/store_user"            android:layout_width="30dp"            android:layout_height="30dp"            android:layout_margin="5dp"            android:descendantFocusability="blocksDescendants"            android:focusable="false"          >        </ImageView>    </LinearLayout></LinearLayout>

效果图


然后在UserListActivity中添加的监听事件代码如下

1 实现对ListItem的监听

this.userlist = (ListView) findViewById(R.id.userlist);this.userlist.setOnItemClickListener(new AdapterOcl());

2 对ImageView实现监听

this.storeUser= (ImageView) findViewById(R.id.store_user);this.storeUser.setOnClickListener(new ViewOcl());

其实最初是想写ImageButton,但总点击不了,网上有人说是ListView把Button的焦点吃掉了,后来改为ImageView,但会在ImageView添加监听处报空指针,后来又查了一些信息,才知道用到了SimpleAdapter的getView()方法才能实现,后来又新建一个 MyAdapter类重写了一下SimpleAdapter和它的getView方法才实现

MyAdapter中方法如下

public class MyAdapter extends SimpleAdapter {Context context;List<? extends Map<String, ?>> data;int resource;String[] from;int[] to;private LayoutInflater mInflater;public MyAdapter(Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to) {super(context, data, resource, from, to);this.mInflater = LayoutInflater.from(context);this.context=context;this.data=data;this.resource=resource;this.from=from;this.to=to;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {convertView = mInflater.inflate(resource, null);for (int i = 0; i < from.length; i++) {//备注1if (convertView.findViewById(to[i]) instanceof ImageView) {ImageView iv = (ImageView) convertView.findViewById(to[i]);iv.setBackgroundResource((Integer) data.get(position).get(from[i]));}else if (convertView.findViewById(to[i]) instanceof TextView) {TextView tv = (TextView) convertView.findViewById(to[i]);tv.setText((String) data.get(position).get(from[i]));}}addListener(convertView,position);return convertView;}public void addListener(View convertView,int arg) {final int arg2=arg;((ImageView)convertView.findViewById(R.id.store_user)).setOnClickListener(new View.OnClickListener() {public void onClick(View v) {Toast.makeText(context, "点击了第"+arg2+"项",Toast.LENGTH_SHORT).show();/* Intent intent = new Intent(myActivity.ma,A.class); myActivity.ma.startActivity(intent);*/}});}


然后在UserListActivity中改写如下,并将之前对ImageView的监听删掉

// 为联系人创建listitem适配器SimpleAdapter adapter = new MyAdapter(this, this.userdata,R.layout.userlist_item_layout, new String[] { "user_photo","user_name", "user_tel", "user_collect" }, new int[] {R.id.user_phote, R.id.user_name, R.id.user_tel,R.id.store_user });// 将listitem绑定到适配器上this.userlist = (ListView) findViewById(R.id.userlist);this.userlist.setAdapter(adapter);// 将listitem绑定到适配器监听器this.userlist.setOnItemClickListener(new AdapterOcl());


最后实现了我想要的结果,嘎嘎,挺费功夫,还好有小乔在身边帮忙,不然我是搞不定的微笑

 

 

 

 

原创粉丝点击