標籤:
1.添加不同類型條目
1 class MyAdapter extends BaseAdapter{ 2 3 //擷取資料配接器中項目類型的總數,修改成兩種(純文字,圖片+文字) 4 @Override 5 public int getViewTypeCount() { 6 return super.getViewTypeCount()+1; 7 } 8 9 //指定索引指向的項目類型,項目類型狀態代碼指定(0(複用系統),1)10 @Override11 public int getItemViewType(int position) {12 if(position == 0 || position == mCustomerList.size()+1){13 //返回0,代表純文字條目的狀態代碼14 return 0;15 }else{16 //返回1,代表圖片+文本條目狀態代碼17 return 1;18 }19 }20 21 //listView中添加兩個描述條目22 @Override23 public int getCount() {24 return mCustomerList.size()+mSystemList.size()+2;25 }26 27 @Override28 public AppInfo getItem(int position) {29 if(position == 0 || position == mCustomerList.size()+1){30 return null;31 }else{32 if(position<mCustomerList.size()+1){33 return mCustomerList.get(position-1);34 }else{35 //返回系統應用對應條目的對象36 return mSystemList.get(position - mCustomerList.size()-2);37 }38 }39 }40 41 @Override42 public long getItemId(int position) {43 return position;44 }45 46 @Override47 public View getView(int position, View convertView, ViewGroup parent) {48 int type = getItemViewType(position);49 50 if(type == 0){51 //展示灰色純文字條目52 ViewTitleHolder holder = null;53 if(convertView == null){54 convertView = View.inflate(getApplicationContext(), R.layout.listview_app_item_title, null);55 holder = new ViewTitleHolder();56 holder.tv_title = (TextView)convertView.findViewById(R.id.tv_title);57 convertView.setTag(holder);58 }else{59 holder = (ViewTitleHolder) convertView.getTag();60 }61 if(position == 0){62 holder.tv_title.setText("使用者應用("+mCustomerList.size()+")");63 }else{64 holder.tv_title.setText("系統應用("+mSystemList.size()+")");65 }66 return convertView;67 }else{68 //展示圖片+文字條目69 ViewHolder holder = null;70 if(convertView == null){71 convertView = View.inflate(getApplicationContext(), R.layout.listview_app_item, null);72 holder = new ViewHolder();73 holder.iv_icon = (ImageView)convertView.findViewById(R.id.iv_icon);74 holder.tv_name = (TextView)convertView.findViewById(R.id.tv_name);75 holder.tv_path = (TextView) convertView.findViewById(R.id.tv_path);76 convertView.setTag(holder);77 }else{78 holder = (ViewHolder) convertView.getTag();79 }80 holder.iv_icon.setBackgroundDrawable(getItem(position).icon);81 holder.tv_name.setText(getItem(position).name);82 if(getItem(position).isSdCard){83 holder.tv_path.setText("sd卡應用");84 }else{85 holder.tv_path.setText("手機應用");86 }87 return convertView;88 }89 }90 }
2.常駐懸浮框使用
lv_app_list.setOnScrollListener(new OnScrollListener() {@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {//滾動過程中調用方法//AbsListView中view就是listView對象//firstVisibleItem第一個可見條目索引值//visibleItemCount當前一個螢幕的可見條目數//總共條目總數if(mCustomerList!=null && mSystemList!=null){if(firstVisibleItem>=mCustomerList.size()+1){//滾動到了系統條目tv_des.setText("系統應用("+mSystemList.size()+")");}else{//滾動到了使用者應用條目tv_des.setText("使用者應用("+mCustomerList.size()+")");}}}});
3.activity_app_manager.xml
<FrameLayout android:layout_width="match_parent"android:layout_height="wrap_content"><ListView android:id="@+id/lv_app_list"android:layout_width="match_parent"android:layout_height="wrap_content"></ListView><TextView android:background="#ccc"android:id="@+id/tv_des"android:textColor="#000"android:textSize="18sp"android:layout_width="match_parent"android:layout_height="wrap_content"/></FrameLayout>
Android 手機衛士10--應用管理器