本文主要是介绍listview多种条目展示案例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
public class myAdapter extends BaseAdapter {// 获得当前条目类型public int getItemViewType(int position) {// 如果是标题,返回0if (position == 0 || position == userAppList.size() + 1) {return 0;} else {// 如果是应用条目,返回1return 1;}}//获得条目种类个数@Overridepublic int getViewTypeCount() {return super.getViewTypeCount() + 1;}@Override//所有条目总数public int getCount() {return systemAppList.size() + userAppList.size() + 2;}/** 返回当前条目*/public AppInfo getItem(int position) {if (position == 0 || position == userAppList.size() + 1) {return null;} else if (position < userAppList.size() + 1) {return userAppList.get(position - 1);} else {return systemAppList.get(position - 2 - userAppList.size());}}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {int type = getItemViewType(position);// 如果是纯文本if (type == 0) {ViewTitleHolder vth = null;if (convertView == null) {convertView = View.inflate(getApplicationContext(),R.layout.item_appinfo_title, null);vth = new ViewTitleHolder();vth.title = (TextView) convertView.findViewById(R.id.tv_appinfo_title);convertView.setTag(vth);} else {vth = (ViewTitleHolder) convertView.getTag();}if (position == 0) {vth.title.setText("用户应用(" + userAppList.size() + ")");} else {vth.title.setText("系统应用(" + systemAppList.size() + ")");}return convertView;} else {// 如果是应用介绍ViewHolder vh = null;if (convertView == null) {convertView = View.inflate(getApplicationContext(),R.layout.item_appinfo, null);vh = new ViewHolder();vh.icon = (ImageView) convertView.findViewById(R.id.iv_appIcon);vh.appName = (TextView) convertView.findViewById(R.id.tv_appName);vh.appIsSdcard = (TextView) convertView.findViewById(R.id.tv_appIsSdcard);convertView.setTag(vh);} else {vh = (ViewHolder) convertView.getTag();}AppInfo item = getItem(position);vh.icon.setImageDrawable(item.icon);vh.appName.setText(item.name);if (item.isSdcard) {vh.appIsSdcard.setText("内存储卡");} else {vh.appIsSdcard.setText("SdCard应用");}return convertView;}}}
private ArrayList<AppInfo> systemAppList; //系统应用信息集合private ArrayList<AppInfo> userAppList; //用户应用信息集合
这篇关于listview多种条目展示案例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!