Android: ListView与Button的共存问题解决

Android: ListView与Button的共存问题解决,第1张

概述ListView和其它能触发点击事件的widget无法一起正常工作的原因是加入其它widget后,ListView的itemclick事件将无法触发,被其它widget的click事件屏蔽。 首先,说明一下,ListView中每一行包括以下三项:   一个ImageView,一个TextView,一个ImageButton,依次排开。 以下是layou ListVIEw 和 其它能触发点击事件的Widget无法一起正常工作的原因是加入其它Widget后,ListVIEw的itemclick事件将无法触发,被其它Widget的click事件屏蔽。 首先,说明一下,ListVIEw中每一行包括以下三项:   一个ImageVIEw, 一个TextVIEw,一个Imagebutton,依次排开。 以下是layout的内容,分为两部分:res/layout/main.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"    androID:padding="10dip" androID:orIEntation="vertical">    <ListVIEw androID:ID="@ID/androID:List" androID:layout_wIDth="fill_parent"        androID:layout_height="fill_parent" /></linearLayout>

res/layout/lvitem.xml因为继承了ListActivity,所以ListVIEw 的ID设置为"@ID/androID:List"是必须的

注意:

在<relativeLayout>中

androID:descendantFocusability="blocksDescendants"

和<Imagebutton>中

androID:focusable="false"

这两项的设置很关键,如果不设置,将导致ListVIEw的ItemClick事件将无法触发,该事件被Imagebutton的click事件屏蔽了。

<?xml version="1.0" enCoding="utf-8"?><relativeLayout  xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="fill_parent"  androID:layout_height="wrap_content"  androID:padding="5dip"  androID:descendantFocusability="blocksDescendants" >    <ImageVIEw      androID:ID="@+ID/ItemImage"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:padding="5dip"  />      <!--      把按钮背景设置为透明:     androID:background="#00000000"      把按钮背景设置为半透明:     androID:background="#e0000000"      -->  <Imagebutton     androID:ID="@+ID/ItemCloseWin"          androID:layout_alignParentRight="true"     androID:layout_aligntop="@+ID/ItemWinname"      androID:layout_alignBottom="@+ID/ItemWinname"      androID:layout_wIDth="wrap_content"      androID:layout_height="wrap_content"            androID:background="#e0000000"      androID:gravity="left|center_vertical"      androID:focusable="false"      androID:src="@androID:drawable/ic_menu_close_clear_cancel"  />    <TextVIEw      androID:ID="@+ID/ItemWinname"            androID:layout_toRightOf="@+ID/ItemImage"      androID:layout_toleftOf="@+ID/ItemCloseWin"      androID:layout_aligntop="@+ID/ItemImage"      androID:layout_alignBottom="@+ID/ItemImage"      androID:layout_wIDth="wrap_content"      androID:layout_height="wrap_content"            androID:gravity="left|center_vertical"      androID:textSize="20dip"      androID:text="Title"  />       </relativeLayout>


在lvWithbuttonExt中,为了能处理Imagebutton的click事件,我继承了BaseAdapter类,并重新实现了getVIEw()接口,在其中加入了button的clickListener,详见lvbuttonAdapter类的实现。接下来,我们看看继承ListActivity的实现

public class lvWithbuttonExt extends ListActivity {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        // 关联Layout中的ListVIEw        ListVIEw vncListVIEw = (ListVIEw)findVIEwByID(androID.R.ID.List);                // 生成动态数组,加入数据         ArrayList<HashMap<String, Object>> remoteWindowItem = newArrayList<HashMap<String, Object>>();        for(int i=0;i<10;i++)        {            HashMap<String, Object> map = new HashMap<String, Object>();            map.put("ItemImage", R.drawable.firefox);//图像资源的ID             map.put("ItemWinname", "Window ID "+i);            map.put("ItemCloseWin",androID.R.drawable.ic_menu_close_clear_cancel);            remoteWindowItem.add(map);        }              // 生成适配器的Item和动态数组对应的元素         lvbuttonAdapter ListItemAdapter = new lvbuttonAdapter(            this,            remoteWindowItem,//数据源             R.layout.lvitem,//ListItem的XML实现            //动态数组与ImageItem对应的子项             new String[] {"ItemImage","ItemWinname", "ItemCloseWin"},            //ImageItem的XML文件里面的一个ImageVIEw,两个TextVIEw ID             new int[] {R.ID.ItemImage,R.ID.ItemWinname,R.ID.ItemCloseWin}        );                vncListVIEw.setAdapter(ListItemAdapter);    }    @OverrIDe    protected voID onListItemClick(ListVIEw l, VIEw v, int position, long ID){        // Todo auto-generated method stub        super.onListItemClick(l, v, position, ID);        l.getItemAtposition(position);    }}


为了响应按钮的点击事件,首先要记录按钮的位置,然后为按钮设置clickListener。接下来,我们看看lvbuttonAdapter的实现

在重新实现的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);        }    }}


点击右边的按钮该行将被删除以下是运行效果图:

总结

以上是内存溢出为你收集整理的Android: ListView与Button的共存问题解决全部内容,希望文章能够帮你解决Android: ListView与Button的共存问题解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1114396.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存