Android之ListView性能优化
如果有看过我写过的15k面试题的朋友们一定知道,ListView的优化方式有以下几种:
public View getView(int position,View convertView,View Groupparent) { if(convertView==null) { convertView=LayoutInflater.from(context).inflate(R.layout.section_list_item1,null); } TextViewtv_name=(TextView)convertView.findViewById(R.id.contact_contactinfoitem_tv_name); TextViewtv_phone=(TextView)convertView.findViewById(R.id.contact_contactinfoitem_tv_phoneNum); ContactInfo1confo=contacts.get(position); if(confo!=null){//toseteveryitem'stext tv_name.setText(confo.getContactName()); tv_phone.setText(confo.getContact_Phone()); } return convertView; }ViewHolder
public View getView(int position,View convertView,View Groupparent) { ViewHolderholder; if(convertView==null){ convertView=LayoutInflater.from(context).inflate(R.layout.section_list_item1,null); holder=newViewHolder(); holder.tv_name=(TextView)convertView.findViewById(R.id.contact_contactinfoitem_tv_name); holder.tv_phone=(TextView)convertView.findViewById(R.id.contact_contactinfoitem_tv_phoneNum); convertView.setTag(holder); } else { holder=(ViewHolder)convertView.getTag(); } ContactInfo1confo=contacts.get(position); if(confo!=null){//toseteveryitem'stext holder.tv_name.setText(confo.getContactName()); holder.tv_phone.setText(confo.getContact_Phone()); } return convertView; } classViewHolder { TextView tv_name,tv_phone; }
1、判断listView状态 AbsListView.OnScrollListener onScrollListener = new AbsListView.OnScrollListener() {// ListView // 触摸事件 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { case AbsListView.OnScrollListener.SCROLL_STATE_FLING:// 滑动状态 threadFlag = false; break; case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:// 停止 threadFlag = true; startThread(); break; case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:// 触摸listView threadFlag = false; break; default: // Toast.makeText(contextt, "default", // Toast.LENGTH_SHORT).show(); break; } } }; 2、获取当前屏幕上显示的items: mListView.getFirstVisiblePosition(); mListView.getLastVisiblePosition();
原文地址:http://blog.csdn.net/cym492224103/article/details/39639047