首先,我的以爲這種問題壓根就不是問題,由於你將一個TextView的Backgroud設置成一個selector,也能夠將一個TextView設計成一個按鈕的樣子。。。這樣徹底能夠繞過ListView和Button的衝突問題。。。。 android
非要解決的話,如下是一種解決方法: 數組
<?xml version="1.0" encoding="utf-8"?> |
由於繼承了ListActivity,因此ListView 的id設置爲"@id/android:list"是必須的 ide
注意: 函數
在<RelativeLayout>中 this
android:descendantFocusability="blocksDescendants" spa
和<ImageButton>中 .net
android:focusable="false" firefox
這兩項的設置很關鍵,若是不設置,將致使ListView的ItemClick事件將沒法觸發,該事件被ImageButton的click事件屏蔽了。
設計
<?xml version="1.0" encoding="utf-8"?> |
在lvWithButtonExt中,爲了能處理ImageButton的click事件,我繼承了BaseAdapter類,並從新實現了getView()接口,在其中加入了Button的clicklistener,詳見lvButtonAdapter類的實現。
public class lvWithButtonExt extends ListActivity { |
爲了響應按鈕的點擊事件,首先要記錄按鈕的位置,而後爲按鈕設置clicklistener。
在從新實現的getView()接口中,我使用了lvButtonListener監聽類,在構造函數中,記錄行號,以便在OnClick接口中能準確的定位按鈕所在的位置,進而對相應的行進行處理。
public class lvButtonAdapter extends BaseAdapter { private class buttonViewHolder { ImageView appIcon; TextView appName; ImageButton buttonClose; } private ArrayList<HashMap<String, Object>> mAppList; private LayoutInflater mInflater; private Context mContext; private String[] keyString; private int[] valueViewID; private buttonViewHolder holder; public lvButtonAdapter(Context c, ArrayList<HashMap<String, Object>> appList, int resource, String[] from, int[] to) { mAppList = appList; mContext = c; mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); keyString = new String[from.length]; valueViewID = new int[to.length]; System.arraycopy(from, 0, keyString, 0, from.length); System.arraycopy(to, 0, valueViewID, 0, to.length); } @Override public int getCount() { return mAppList.size(); } @Override public Object getItem(int position) { return mAppList.get(position); } @Override public long getItemId(int position) { return position; } public void removeItem(int position){ mAppList.remove(position); this.notifyDataSetChanged(); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView != null) { holder = (buttonViewHolder) convertView.getTag(); } else { convertView = mInflater.inflate(R.layout.lvitem, null); holder = new buttonViewHolder(); holder.appIcon = (ImageView)convertView.findViewById(valueViewID[0]); holder.appName = (TextView)convertView.findViewById(valueViewID[1]); holder.buttonClose = (ImageButton)convertView.findViewById(valueViewID[2]); convertView.setTag(holder); } HashMap<String, Object> appInfo = mAppList.get(position); if (appInfo != null) { String aname = (String) appInfo.get(keyString[1]); int mid = (Integer)appInfo.get(keyString[0]); int bid = (Integer)appInfo.get(keyString[2]); holder.appName.setText(aname); holder.appIcon.setImageDrawable(holder.appIcon.getResources().getDrawable(mid)); holder.buttonClose.setImageDrawable(holder.buttonClose.getResources().getDrawable(bid)); holder.buttonClose.setOnClickListener(new lvButtonListener(position)); } return convertView; } class lvButtonListener implements OnClickListener { private int position; lvButtonListener(int pos) { position = pos; } @Override public void onClick(View v) { int vid=v.getId(); if (vid == holder.buttonClose.getId()) removeItem(position); } } } |