在ListView中實現對ImageView的監聽

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;	
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = mInflater.inflate(resource, null);
		for (int i = 0; i < from.length; i++) {//備註1
			if (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());


最後實現了我想要的結果,嘎嘎,挺費功夫,還好有小喬在身邊幫忙,不然我是搞不定的微笑

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章