List Implementation of multiple ANDROID la s
I also asked my colleagues and friends in the Development Group, but they didn't get the optimal implementation method. It seems that there are still few requirements for this complex list, I also made some detours and sorted out some of my implementation methods. I hope it will be helpful to some friends who do not know about it. Implementation Method 1: (re-inflate itemView upon getView, convertView is not reused, performance is low, and running is OK) private class MyAdapter extends BaseAdapter {private List <Object> datas = Collections. EMPTY_LIST; public void setDatas (List <Object> datas) {if (datas = null) {datas = Collections. EMPTY_LIST;} this. datas = datas; notifyDataSetChanged () ;}@ Override public int getCount () {return datas. size () ;}@ Override public Object getItem (int posi Tion) {return datas. get (position) ;}@ Override public long getItemId (int position) {return 0 ;}@ Override public View getView (int position, View convertView, ViewGroup parent) {Object data = getItem (position); if (data instanceof Folder) {FolderViewHolder holder = null; if (convertView! = Null & convertView. getTag () instanceof FolderViewHolder) {// consistent View and Data Type holder = (FolderViewHolder) convertView. getTag ();} else {convertView = mInflater. inflate (R. layout. listitem1, null); holder = new FolderViewHolder (convertView); convertView. setTag (holder);} holder. setData (Folder) data);} else {FileViewHolder holder = null; if (convertView! = Null & convertView. getTag () instanceof FileViewHolder) {// View consistent with the data type holder = (FileViewHolder) convertView. getTag ();} else {convertView = mInflater. inflate (R. layout. listitem2, null); holder = new FileViewHolder (convertView); convertView. setTag (holder);} holder. setData (File) data);} return convertView;} private class FolderViewHolder {public TextView tvName; public FolderViewHolder (View ite MView) {tvName = (TextView) itemView. findViewById (R. id. tvName);} public void setData (Folder data) {tvName. setText (data. name) ;}} private class FileViewHolder {public TextView tvName; public FileViewHolder (View itemView) {tvName = (TextView) itemView. findViewById (R. id. tvName);} public void setData (File data) {tvName. setText (data. name) ;}} Implementation Method 2: (because method 1 constantly inflate the view and affects the performance, consider whether the inflate View, so a cache is added. However, an exception is displayed when sliding or switching data quickly in the actual test. It should be the reason why AbsListView # RecycleBin is cached, looking at other people's code is the most painful ...) Private class MyAdapter extends BaseAdapter {private List <View> folderViewCaches = new ArrayList <View> (5); private List <View> fileViewCaches = new ArrayList <View> (5 ); private List <Object> datas = Collections. EMPTY_LIST; public void setDatas (List <Object> datas) {if (datas = null) {datas = Collections. EMPTY_LIST;} this. datas = datas; notifyDataSetChanged () ;}@ Override public int getCount () {retu Rn datas. size () ;}@ Override public Object getItem (int position) {return datas. get (position) ;}@ Override public long getItemId (int position) {return 0 ;}@ Override public View getView (int position, View convertView, ViewGroup parent) {Object data = getItem (position); if (data instanceof Folder) {// Folder, R. layout. view FolderViewHolder holder = null for listitem1; if (convertView! = Null & convertView. getTag () instanceof FolderViewHolder) {// consistent View and Data Type holder = (FolderViewHolder) convertView. getTag ();} else {if (convertView! = Null) {// cached to the file list fileViewCaches. add (convertView); convertView = null;} // obtain the cache that has been removed from the ListView from the cache (comment out this part of the code and it will be normal) if (! FolderViewCaches. isEmpty () {for (View cache: folderViewCaches) {if (cache. getParent () = null) {// The cached View has been removed from the listView convertView = cache; holder = (FolderViewHolder) convertView. getTag (); folderViewCaches. remove (cache); break ;}}// still not, re-inflate if (convertView = null) {convertView = mInflater. inflate (R. layout. listitem1, null); holder = new FolderViewHolder (convertView); convertView. setTag (h Older) ;}} holder. setData (Folder) data);} else {// file, which should return R. layout. view FileViewHolder holder = null for listitem2; if (convertView! = Null & convertView. getTag () instanceof FileViewHolder) {// View and Data Type consistent holder = (FileViewHolder) convertView. getTag ();} else {if (convertView! = Null) {// cache to the folder list folderViewCaches. add (convertView); convertView = null;} // obtain the cache that has been removed from the ListView from the cache (comment out this part of the code and it will be normal) if (! FileViewCaches. isEmpty () {for (View cache: fileViewCaches) {if (cache. getParent () = null) {// The cached View has been removed from the listView convertView = cache; holder = (FileViewHolder) convertView. getTag (); fileViewCaches. remove (cache); break ;}}// still not, re-inflate if (convertView = null) {convertView = mInflater. inflate (R. layout. listitem2, null); holder = new FileViewHolder (convertView); convertView. setTag (holder );}} Holder. setData (File) data);} return convertView;} method 3: (optimal implementation, normal operation) read the source code of ListView carefully, only then can the Adapter itself support different la S, and AbsListView # RecycleBin also supports cache policies for different la S, RecycleBin. mViewTypeCount indicates the number of View types. We need to rewrite the following three methods of the Adapter: 1. getViewTypeCount:/*** number of views with different la S */@ Override public int getViewTypeCount () {return 2;} 2. getItemViewType/*** View type corresponding to position */@ Override public int getItemViewType (int position) {if (getItem (position) instanceof Folder) {return TYPE_FOLDER ;} else {return TYPE_FILE;} 3. getView: returns the view of the corresponding type by determining the corresponding position type: @ Override public View getView (int position, Vi Ew convertView, ViewGroup parent) {Object data = getItem (position); if (data instanceof Folder) {// TYPE_FOLDER, Folder, which should return R. layout. view FolderViewHolder holder = null for listitem1; if (convertView! = Null) {holder = (FolderViewHolder) convertView. getTag ();} else {convertView = mInflater. inflate (R. layout. listitem1, null); holder = new FolderViewHolder (convertView); convertView. setTag (holder);} holder. setData (Folder) data);} else {// TYPE_FILE, file, should return R. layout. view FileViewHolder holder = null for listitem2; if (convertView! = Null) {holder = (FileViewHolder) convertView. getTag ();} else {convertView = mInflater. inflate (R. layout. listitem2, null); holder = new FileViewHolder (convertView); convertView. setTag (holder);} holder. setData (File) data);} return convertView ;}