1.当ListView中有Button的时候往往会遇到很多问题,比较常见的一个问题是:
假设:在ListView中有N个Item当点击其中某个Item中的Button的时候,需要改变当前Button的状态,在你点击以后确实最后一个Item的Button的状态改变了,不管点击哪一个Button都是最后一个
Item的中的Button的状态被改变了,这里涉及到ListView重绘的问题。
要解决上面的问题的方法是将点击的监听事件提出来进行封装,在用就可以解决上述问题了。
给个示例代码:
在ListView的自定义Adapter的getView方法中:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOMAAAA6CAIAAADN3Xn8AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAQN0lEQVR4nO2dfXQT15XA75ONy4exHWwIH9mAiORDiNukm0JZOekJNoZKPrvYKXHCWYrdnBMJFhxrISawZbvbPSZQnA+pfKTWScEQtml8iNEmtmRMYJO01gGH3XS7LvnQKHxsHQgBgSjmw555d/8YzWgkzUgytmXZzO/MOR69d999dzRXb94bz51LEBESgCL0IfRS7KPAIiKChkCGhmRoYFwakW3y0caC6T9cP358GgB8fe7iHzr+6/Gli8WPfznZ+INtXYl0PWJxW4izDBuMccQYe6H+1Ob4cgNrMuLRJCKEACxCL8VbHPZw2MPC+1foi1+ytyn2Kfs5pTh+fFrenOljx44ZNzETAKQfKU3oF6KiwpOQp7I06KY3OLjB4tEr3D+fZo/4aS8Fjiq3YgEArp/5BgCABMdd8SNfq4DbQoIU2pmIEkIs7pBYod1tLxRLBSmhVSydjL1QVOW2CLtMUJukm7i9MPZCYnGH9Iv2xTwisVxv9YDDJFbISIYbFd3kriC+p3IIHGIfhdsUbnH44VW65RxXOS2dr1J2VGBZ+vW5ixfPBy5euHb1/EXv6QvSjyyr1NRtIaYumxcREbGjRhdRgi4whZzBY62DfYjoMjtMhDjLEBG9NrDWuyN11s0NKvBWNOktbtDVdLjMjjo7A26LqcvmbTACMPZ62Bfsxeww9aMXh4kvjWio2LuAsQG9NgOYXfyhykky9kprgSt48A3GyCZ3DRiPPorX++jFW9zpH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Cnq14lihIRqfIUkjQZay18d9hcBwYQB6vxEmtOCqeXtTwz1R1/fGcbBxVAr0Za20GkL2862o2FzgcktxrupoO/s5UEH1ThTfO7FFX04GuAqvYxFmmMEQZG7y2LlNQKF8qo6vpQBeYQisQh0lu5aWryXcKl+wC/tejK60wCCsqqX7iLIv160pYUtqo1gbBo6w8VRAaLHWlFeBwFAjjgJLYoJIScVQRvPTC4ys2vA8ETl24BADRcVQ/rf/dQIzhFxfqfSVFUjLBVqrEUUlhKVBERDJnSi4AiY6jGhD8/QBU3VQRprXJY6hIteDAVHxChSJ5Y+sS/vF/PoIqIo7qjjW7LcTkEFI6qkTDf0F3ku9zyFGfpFYZGaTiEyoqKtGonqoyMlA9VWVkoHqqysgg/trf/HKB9KPjha4hM0ZFRZE4nmp+uUDqmhFeq6KSNP4f9QBcfiaOYsUAAAAASUVORK5CYII=" alt="" />
MianActivity自定义Adapter中
@Override
public View getView(int position, View convertView, ViewGroup parent) { System.out.println("getViewgetViewgetViewgetViewgetView");
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.item, null);
viewHolder.left = (Button) convertView
.findViewById(R.id.btn_left);
viewHolder.right = (Button) convertView
.findViewById(R.id.btn_right);
mOnClickListener = MyOnClickListener.instance();
mOnClickListener.setContext(MainActivity.this);
viewHolder.left.setOnClickListener(mOnClickListener);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
} return convertView;
}
viewHolder.left这个Button的点击事件封装一下就可以消除上述问题了
MyOnClickListenter:
public class MyOnClickListener implements OnClickListener { private static MyOnClickListener mOnClickListener; private Button btn; private Context context; public static MyOnClickListener instance() {
if (mOnClickListener == null) {
mOnClickListener = new MyOnClickListener();
} return mOnClickListener;
} public void setContext(Context context) {
this.context = context; } @Override
public void onClick(View v) {
btn = (Button) v;
final EditText edtextView = new EditText(context);
new AlertDialog.Builder(context).setTitle("请输入密码").setView(edtextView)
.setPositiveButton("确定", new AlertDialog.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
System.out
.println("setPositiveButtonsetPositiveButtonsetPositiveButton");
String passwaord = edtextView.getText().toString()
.trim();
System.out.println("获取到的密码是多少-->>" + passwaord);
if (passwaord.equals("123")) {
System.out
.println("执行了123132123132123132132131313");
btn.setText("关闭中");
btn.setBackgroundResource(R.drawable.toggle_bg_on);
} }
}).create().show(); } }