我有一个带有自定义BaseAdapter的listView.每个列表项都有一个可点击的按钮. private List items = new ArrayList(); public View getView(final int position, View convertView, ViewGroup parent) { View v = null; try { v = in
private List items = new ArrayList();
public View getView(final int position, View convertView, ViewGroup parent) { View v = null; try { v = inflater.inflate(R.layout.row, null); TextView name = (TextView)v.findViewById(R.id.textView); name.setText(items.get(position).getName()); imageButton = (ImageButton) v.findViewById(R.id.Button); imageButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // do something } }); } catch (Exception e) { e.printStackTrace(); } return v; }
我的问题是当我滚动列表时应用程序冻结.
我该如何解决?
删除View v = null并使用传入的convertView参数.
public View getView(final int position, View convertView, ViewGroup parent) { if(convertView == null){ convertView = inflater.inflate(R.layout.row, null); } TextView name = (TextView)convertView.findViewById(R.id.textView); name.setText(items.get(position).getName()); imageButton = (ImageButton) convertView.findViewById(R.id.Button); /* NB For efficiency sake see the link in Sparky's answer about itemClickListeners instead of using onClickListeners */ imageButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // do something } }); return convertView; }