ListView需要注意的问题

来源:互联网 发布:软件源代码下载 编辑:程序博客网 时间:2024/05/22 05:18

一、当ListView的item中有chexbox、button时会抢占焦点,解决方法如下

(1)设置其为android:focusable="false"

(2)在最外层布局中设置 android:descendantFocusability = "blocksDescendants"


二、ListView复用导致内容错乱

(1)在bean中设置一个属性 private boolean isChecked用来记录是否被选中; 并为其设置get、set方法,在Activity中设置botton.setChecked(bean.isChecked());使得控件复用但是bean不复用。

(2)在Adapter中设置以下代码

private List<Integer> mPos = new ArrayList<Interger>();//用来保存选中的状态 botton.setChecked(false); if(mPos.contains(holder.getPosition())){botton.setChecked(true); }

public void onClick(View v){if(botton.isChecked()){mPos.add(holder.getPosition();}else{mPost.remove((Integer)holder.getPosition());   }}

3、要使用长按事件首先是该Activity类要implements OnItemLongClickListener,


[java] view plaincopy
  1. @Override  
  2.  public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,  
  3.    long arg3) {  
  4.    //TO DO STH HERE  
  5.     return true;  
  6.  }  



不要以为这样就完了,这样写还缺少了一步,还需要在oncreat()  方法中加上如下一句话

[java] view plaincopy
  1. this.getListView().setOnItemLongClickListener(this);  



还需要注意的这句话需要加到SetContentView之后,不然连本来该有的View都还没有得到,怎么又可能有所响应呢!

好了,这些都加好后,就可以在长按事件中写你需要的功能了,不管是弹出窗口还是跳转或者怎么样都随意了。





 

1 0
原创粉丝点击