1,让ListView显示分组(用两个layout)
2,让ListView实现sectionIndexer接口
3,代码如下:
1 import java.util.ArrayList; 2 3 import com.example.fastscroll.R; 4 5 import android.content.Context; 6 import android.graphics.Color; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.widget.BaseAdapter; 11 import android.widget.SectionIndexer; 12 import android.widget.TextView; 13 14 public class GroupsFastScrollAdapter extends BaseAdapter implements SectionIndexer{ 15 16 Context context; 17 LayoutInflater inflater; 18 19 /* 20 * 保存当前section的第一条记录的Position 21 * Character是section,Integer是section对应的第一条数据的position 22 */ 23 /* 24 * 保存每个section的第一条数据的position 25 * firstPositions的下标 firstPositions的值 26 * 0 0 27 * 1 2 28 * 2 4 29 * 3 6 30 */ 31 32 Integer firstPositions[]; 33 34 /* 用空间代时间,把数据中的每个position对应的section index存下来 35 * 如: 36 * sectionIndexes的下标 sectionIndexes数组中的值(section的下标) 37 * 0 0 38 * 1 0 39 * 2 1 40 * 3 1 41 * 4 2 42 * 5 2 43 * 6 3 44 * ... ... 45 */ 46 int sectionIndexes[];//保存每个position对应的section index 47 48 Character sections[]; 49 50 /*private String countries[]= { 51 "A", 52 "Afghanistan", "Albania", "Algeria", "American Samoa", 53 "Andorra", "Angola", "Anguilla", "Antarctica", 54 "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", 55 "Australia", "Austria", "Azerbaijan", 56 "B", 57 "Bahrain", "Bangladesh","Barbados", "Belarus", "Belgium", 58 "Belize", "Benin", "Bermuda","Bhutan", "Bolivia", 59 "Bosnia and Herzegovina", "Botswana","Bouvet Island", 60 "Brazil", "British Indian Ocean Territory","British Virgin Islands", 61 "Brunei", "Bulgaria", "Burkina Faso","Burundi", 62 "C", 63 "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada","Cape Verde", 64 "Cayman Islands", "Central African Republic", "Chad", 65 "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", 66 "Colombia", "Comoros", "Congo", "Cook Islands", "Costa Rica", 67 "Croatia", "Cuba", "Cyprus", "Czech Republic", 68 "D", 69 "Democratic Republic of the Congo", "Denmark", "Djibouti", 70 "Dominica", "Dominican Republic", 71 "E", 72 "East Timor", "Ecuador", "Egypt", 73 "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia","Ethiopia", 74 "F", 75 "Faeroe Islands", "Falkland Islands", "Fiji", 76 "Finland", "Former Yugoslav Republic of Macedonia", "France", 77 "French Guiana", "French Polynesia", "French Southern Territories", 78 "G", 79 "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", 80 "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", 81 "Guinea", "Guinea-Bissau", "Guyana", 82 "H", 83 "Haiti","Heard Island and McDonald Islands", "Honduras", "Hong Kong","Hungary", 84 "I", 85 "Iceland", "India", "Indonesia", "Iran", "Iraq","Ireland", "Israel","Italy", 86 "J", 87 "Jamaica", "Japan", "Jordan", 88 "K", 89 "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", 90 "L", 91 "Laos","Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", 92 "Liechtenstein", "Lithuania", "Luxembourg", 93 "M", 94 "Macau", "Madagascar","Malawi", "Malaysia", "Maldives", "Mali", "Malta", 95 "Marshall Islands", "Martinique", "Mauritania", "Mauritius", 96 "Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", 97 "Montserrat", "Morocco", "Mozambique", "Myanmar", 98 "N", 99 "Namibia","Nauru", "Nepal", "Netherlands", "Netherlands Antilles", 100 "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", 101 "Niue", "Norfolk Island", "North Korea", "Northern Marianas", 102 "Norway", 103 "O", 104 "Oman", 105 "P", 106 "Pakistan", "Palau", "Panama", 107 "Papua New Guinea", "Paraguay", "Peru", "Philippines", 108 "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", 109 "Q", 110 "Qatar", 111 "R", 112 "Reunion", "Romania", "Russia", "Rwanda", 113 "S", 114 "Sqo Tome and Principe", 115 "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia", 116 "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", 117 "Samoa", "San Marino", "Saudi Arabia", "Senegal", "Seychelles", 118 "Sierra Leone", "Singapore", "Slovakia", "Slovenia", 119 "Solomon Islands", "Somalia", "South Africa", 120 "South Georgia and the South Sandwich Islands", "South Korea", 121 "Spain", "Sri Lanka", "Sudan", "Suriname", 122 "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland","Syria", 123 "T", 124 "Taiwan", "Tajikistan", "Tanzania", "Thailand", 125 "The Bahamas", "The Gambia", "Togo", "Tokelau", "Tonga", 126 "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", 127 "Turks and Caicos Islands", "Tuvalu", 128 "U", 129 "Uganda","Ukraine", "United Arab Emirates", "United Kingdom", 130 "United States", "United States Minor Outlying Islands", "Uruguay","Uzbekistan", 131 "V", 132 "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Virgin Islands", 133 "W", 134 "Wallis and Futuna", "Western Sahara", 135 "Y", 136 "Yemen", "Yugoslavia", 137 "Z", 138 "Zambia", "Zimbabwe" }; 139 */ 140 private String countries[]= { 141 "A", 142 "啊有", "啊有2", "啊1有", 143 "啊有3", "爱有3", "爱有4", "暗3","暗32", 144 "B", 145 "把工和1", "把工2和","把1工和", "把工2和", "把工4和", 146 "白2为", "冰的","冰的2", "冰的3","布是","部公","爸五","兵妥", 147 "兵妥2","兵妥3","兵妥4", 148 "C", 149 "草莓", "参数", "次国粗", "操类1", "操类2", "操类3", "操类4", "操类5", "操类6", 150 "操类7","粗3","粗33","粗34","粗35","粗36", 151 "D", 152 "吊炮","灯具","都产遥","顶为","大有","端人在","电中工","到这要","当遥1", 153 "当遥2","当遥3","当遥4","当遥5" 154 }; 155 void initSections(){ 156 //假设countries已经排序,本例中确实已经排序 157 //把section对应的第一条数据的position存下来 158 ArrayList<Integer> tmpFirstPositions = new ArrayList<Integer>(); 159 ArrayList<Character> tmpSections = new ArrayList<Character>(); 160 sectionIndexes = new int[countries.length]; 161 char lastChar = '\0'; 162 int index = -1; 163 for (int i = 0; i < countries.length; i++) { 164 String country = countries[i]; 165 char ch = country.charAt(0); 166 if (ch != lastChar) { 167 //只存第一条数据的position,其它的跳过。 168 tmpSections.add(ch);//增加一个section 169 tmpFirstPositions.add(i);//增加该section的第一条记录的位置 170 ++index; 171 } 172 lastChar = ch; 173 sectionIndexes[i] = index; 174 } 175 sections = new Character[tmpSections.size()]; 176 tmpSections.toArray(sections); 177 178 firstPositions = new Integer[tmpFirstPositions.size()]; 179 tmpFirstPositions.toArray(firstPositions); 180 } 181 182 public GroupsFastScrollAdapter(Context ctxt){ 183 context = ctxt; 184 inflater = LayoutInflater.from(context); 185 initSections(); 186 } 187 188 189 @Override 190 public int getCount() { 191 return countries.length; 192 } 193 194 @Override 195 public Object getItem(int position) { 196 return countries[position]; 197 } 198 199 200 @Override 201 public int getItemViewType(int position) { 202 if (countries[position].length() == 1) { 203 return 0; 204 } 205 return 1; 206 } 207 208 209 @Override 210 public int getViewTypeCount() { 211 return 2; 212 } 213 214 215 @Override 216 public long getItemId(int position) { 217 return position; 218 } 219 220 @Override 221 public View getView(int position, View convertView, ViewGroup parent) { 222 223 if (getItemViewType(position) == 0) {//group 224 if(convertView == null){ 225 convertView = inflater.inflate(android.R.layout.simple_list_item_1, parent,false); 226 } 227 convertView.setBackgroundColor(Color.parseColor("#C9C9C9")); 228 TextView group = (TextView) convertView.findViewById(android.R.id.text1); 229 group.setText(countries[position]); 230 ViewGroup.LayoutParams param = convertView.getLayoutParams(); 231 // param.height = ViewGroup.LayoutParams.WRAP_CONTENT; 232 param.height = 40; 233 234 }else{ 235 if(convertView == null){ 236 convertView = inflater.inflate(R.layout.list_item_custom_fs, parent,false); 237 } 238 TextView tv = (TextView) convertView.findViewById(R.id.tv_content); 239 tv.setText(countries[position]); 240 } 241 return convertView; 242 } 243 244 @Override 245 public Object[] getSections() { 246 return sections; 247 } 248 249 @Override 250 public int getPositionForSection(int sectionIndex) { 251 sectionIndex %= sections.length; 252 return firstPositions[sectionIndex]; 253 } 254 255 @Override 256 public int getSectionForPosition(int position) { 257 return sectionIndexes[position]; 258 } 259 }
1 import android.app.Fragment; 2 import android.os.Bundle; 3 import android.view.LayoutInflater; 4 import android.view.View; 5 import android.view.ViewGroup; 6 import android.widget.ListView; 7 8 import com.example.fastscroll.R; 9 10 public class GroupsFastScrollFrgmt extends Fragment { 11 ListView listview; 12 GroupsFastScrollAdapter adapter; 13 14 15 16 @Override 17 public View onCreateView(LayoutInflater inflater, ViewGroup container, 18 Bundle savedInstanceState) { 19 View v = inflater.inflate(R.layout.frgmt_groups_and_fastscroll, container,false); 20 listview = (ListView) v.findViewById(R.id.list_groups_fs); 21 22 adapter = new GroupsFastScrollAdapter(getActivity()); 23 listview.setAdapter(adapter); 24 return v; 25 } 26 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <ListView 8 android:id="@+id/list_groups_fs" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" 11 android:fastScrollEnabled="true" 12 > 13 </ListView> 14 15 </LinearLayout>
fastscroll的样式
1 <style name="CustomFastScrollTheme" parent="AppBaseTheme"> 2 <!-- api21 --> 3 <!-- <item name="android:fastScrollStyle">@style/CustomFastScrollTheme</item> --> 4 5 <item name="android:fastScrollThumbDrawable">@drawable/fastscroll_thumb_aje</item> 6 <item name="android:fastScrollOverlayPosition">atThumb</item> 7 <item name="android:fastScrollTextColor">#DA4A38</item> 8 <!-- <item name="android:fastScrollTrackDrawable">@drawable/us</item> --> 9 <item name="android:fastScrollPreviewBackgroundRight">@drawable/fastscroll_preview_right</item> 10 </style>