FastScroll(3)分组的listview 打开fastscroll的分组提示功能
1,让ListView显示分组(用两个layout)
2,让ListView实现sectionIndexer接口
3,代码如下:
import java.util.ArrayList; import com.example.fastscroll.R; import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView; public class GroupsFastScrollAdapter extends BaseAdapter implements SectionIndexer{ Context context;
LayoutInflater inflater; /*
* 保存当前section的第一条记录的Position
* Character是section,Integer是section对应的第一条数据的position
*/
/*
* 保存每个section的第一条数据的position
* firstPositions的下标 firstPositions的值
* 0 0
* 1 2
* 2 4
* 3 6
*/ Integer firstPositions[]; /* 用空间代时间,把数据中的每个position对应的section index存下来
* 如:
* sectionIndexes的下标 sectionIndexes数组中的值(section的下标)
* 0 0
* 1 0
* 2 1
* 3 1
* 4 2
* 5 2
* 6 3
* ... ...
*/
int sectionIndexes[];//保存每个position对应的section index Character sections[]; /*private String countries[]= {
"A",
"Afghanistan", "Albania", "Algeria", "American Samoa",
"Andorra", "Angola", "Anguilla", "Antarctica",
"Antigua and Barbuda", "Argentina", "Armenia", "Aruba",
"Australia", "Austria", "Azerbaijan",
"B",
"Bahrain", "Bangladesh","Barbados", "Belarus", "Belgium",
"Belize", "Benin", "Bermuda","Bhutan", "Bolivia",
"Bosnia and Herzegovina", "Botswana","Bouvet Island",
"Brazil", "British Indian Ocean Territory","British Virgin Islands",
"Brunei", "Bulgaria", "Burkina Faso","Burundi",
"C",
"Cote d'Ivoire", "Cambodia", "Cameroon", "Canada","Cape Verde",
"Cayman Islands", "Central African Republic", "Chad",
"Chile", "China", "Christmas Island", "Cocos (Keeling) Islands",
"Colombia", "Comoros", "Congo", "Cook Islands", "Costa Rica",
"Croatia", "Cuba", "Cyprus", "Czech Republic",
"D",
"Democratic Republic of the Congo", "Denmark", "Djibouti",
"Dominica", "Dominican Republic",
"E",
"East Timor", "Ecuador", "Egypt",
"El Salvador", "Equatorial Guinea", "Eritrea", "Estonia","Ethiopia",
"F",
"Faeroe Islands", "Falkland Islands", "Fiji",
"Finland", "Former Yugoslav Republic of Macedonia", "France",
"French Guiana", "French Polynesia", "French Southern Territories",
"G",
"Gabon", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece",
"Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala",
"Guinea", "Guinea-Bissau", "Guyana",
"H",
"Haiti","Heard Island and McDonald Islands", "Honduras", "Hong Kong","Hungary",
"I",
"Iceland", "India", "Indonesia", "Iran", "Iraq","Ireland", "Israel","Italy",
"J",
"Jamaica", "Japan", "Jordan",
"K",
"Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan",
"L",
"Laos","Latvia", "Lebanon", "Lesotho", "Liberia", "Libya",
"Liechtenstein", "Lithuania", "Luxembourg",
"M",
"Macau", "Madagascar","Malawi", "Malaysia", "Maldives", "Mali", "Malta",
"Marshall Islands", "Martinique", "Mauritania", "Mauritius",
"Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia",
"Montserrat", "Morocco", "Mozambique", "Myanmar",
"N",
"Namibia","Nauru", "Nepal", "Netherlands", "Netherlands Antilles",
"New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria",
"Niue", "Norfolk Island", "North Korea", "Northern Marianas",
"Norway",
"O",
"Oman",
"P",
"Pakistan", "Palau", "Panama",
"Papua New Guinea", "Paraguay", "Peru", "Philippines",
"Pitcairn Islands", "Poland", "Portugal", "Puerto Rico",
"Q",
"Qatar",
"R",
"Reunion", "Romania", "Russia", "Rwanda",
"S",
"Sqo Tome and Principe",
"Saint Helena", "Saint Kitts and Nevis", "Saint Lucia",
"Saint Pierre and Miquelon", "Saint Vincent and the Grenadines",
"Samoa", "San Marino", "Saudi Arabia", "Senegal", "Seychelles",
"Sierra Leone", "Singapore", "Slovakia", "Slovenia",
"Solomon Islands", "Somalia", "South Africa",
"South Georgia and the South Sandwich Islands", "South Korea",
"Spain", "Sri Lanka", "Sudan", "Suriname",
"Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland","Syria",
"T",
"Taiwan", "Tajikistan", "Tanzania", "Thailand",
"The Bahamas", "The Gambia", "Togo", "Tokelau", "Tonga",
"Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan",
"Turks and Caicos Islands", "Tuvalu",
"U",
"Uganda","Ukraine", "United Arab Emirates", "United Kingdom",
"United States", "United States Minor Outlying Islands", "Uruguay","Uzbekistan",
"V",
"Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Virgin Islands",
"W",
"Wallis and Futuna", "Western Sahara",
"Y",
"Yemen", "Yugoslavia",
"Z",
"Zambia", "Zimbabwe" };
*/
private String countries[]= {
"A",
"啊有", "啊有2", "啊1有",
"啊有3", "爱有3", "爱有4", "暗3","暗32",
"B",
"把工和1", "把工2和","把1工和", "把工2和", "把工4和",
"白2为", "冰的","冰的2", "冰的3","布是","部公","爸五","兵妥",
"兵妥2","兵妥3","兵妥4",
"C",
"草莓", "参数", "次国粗", "操类1", "操类2", "操类3", "操类4", "操类5", "操类6",
"操类7","粗3","粗33","粗34","粗35","粗36",
"D",
"吊炮","灯具","都产遥","顶为","大有","端人在","电中工","到这要","当遥1",
"当遥2","当遥3","当遥4","当遥5"
};
void initSections(){
//假设countries已经排序,本例中确实已经排序
//把section对应的第一条数据的position存下来
ArrayList<Integer> tmpFirstPositions = new ArrayList<Integer>();
ArrayList<Character> tmpSections = new ArrayList<Character>();
sectionIndexes = new int[countries.length];
char lastChar = '\0';
int index = -;
for (int i = ; i < countries.length; i++) {
String country = countries[i];
char ch = country.charAt();
if (ch != lastChar) {
//只存第一条数据的position,其它的跳过。
tmpSections.add(ch);//增加一个section
tmpFirstPositions.add(i);//增加该section的第一条记录的位置
++index;
}
lastChar = ch;
sectionIndexes[i] = index;
}
sections = new Character[tmpSections.size()];
tmpSections.toArray(sections); firstPositions = new Integer[tmpFirstPositions.size()];
tmpFirstPositions.toArray(firstPositions);
} public GroupsFastScrollAdapter(Context ctxt){
context = ctxt;
inflater = LayoutInflater.from(context);
initSections();
} @Override
public int getCount() {
return countries.length;
} @Override
public Object getItem(int position) {
return countries[position];
} @Override
public int getItemViewType(int position) {
if (countries[position].length() == ) {
return ;
}
return ;
} @Override
public int getViewTypeCount() {
return ;
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) { if (getItemViewType(position) == ) {//group
if(convertView == null){
convertView = inflater.inflate(android.R.layout.simple_list_item_1, parent,false);
}
convertView.setBackgroundColor(Color.parseColor("#C9C9C9"));
TextView group = (TextView) convertView.findViewById(android.R.id.text1);
group.setText(countries[position]);
ViewGroup.LayoutParams param = convertView.getLayoutParams();
// param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
param.height = ; }else{
if(convertView == null){
convertView = inflater.inflate(R.layout.list_item_custom_fs, parent,false);
}
TextView tv = (TextView) convertView.findViewById(R.id.tv_content);
tv.setText(countries[position]);
}
return convertView;
} @Override
public Object[] getSections() {
return sections;
} @Override
public int getPositionForSection(int sectionIndex) {
sectionIndex %= sections.length;
return firstPositions[sectionIndex];
} @Override
public int getSectionForPosition(int position) {
return sectionIndexes[position];
}
}
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView; import com.example.fastscroll.R; public class GroupsFastScrollFrgmt extends Fragment {
ListView listview;
GroupsFastScrollAdapter adapter; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frgmt_groups_and_fastscroll, container,false);
listview = (ListView) v.findViewById(R.id.list_groups_fs); adapter = new GroupsFastScrollAdapter(getActivity());
listview.setAdapter(adapter);
return v;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ListView
android:id="@+id/list_groups_fs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fastScrollEnabled="true"
>
</ListView> </LinearLayout>
fastscroll的样式
<style name="CustomFastScrollTheme" parent="AppBaseTheme">
<!-- api21 -->
<!-- <item name="android:fastScrollStyle">@style/CustomFastScrollTheme</item> --> <item name="android:fastScrollThumbDrawable">@drawable/fastscroll_thumb_aje</item>
<item name="android:fastScrollOverlayPosition">atThumb</item>
<item name="android:fastScrollTextColor">#DA4A38</item>
<!-- <item name="android:fastScrollTrackDrawable">@drawable/us</item> -->
<item name="android:fastScrollPreviewBackgroundRight">@drawable/fastscroll_preview_right</item>
</style>
FastScroll(3)分组的listview 打开fastscroll的分组提示功能的更多相关文章
- FastScroll(2)不分组的listview 打开fastscroll的分组提示功能
本文只让fastscroll具有提示分组功能,但listview并不显示分组,如果想让分组的listview显示fastscroll,看下篇. 1,在listview中打开fastscroll 2,自 ...
- FastScroll(1)ListView打开FastScroll及自定义它的样式
打开 FastScroll 方式 android:fastScrollEnabled="true" 它是AbsListView的属性. <?xml version=" ...
- 打开程序总是会提示“Enter password to unlock your login keyring” ,如何成功关掉?
p { margin-bottom: 0.1in; line-height: 120% } 一.一开始我是按照网友所说的 : rm -f ~/.gnome2/keyrings/login.keyrin ...
- 微信扫描打开APP下载链接提示代码优化
上一次我发了一篇文章叫“微信打开网址添加在浏览器中打开提示”,里面我发出来了三个代码,分别是纯JS.js+html.jQuery+HTML代码.今天来一个简化版带可以关闭的按钮操作.使用的是纯JS+H ...
- 微信扫描打开APP下载链接提示代码优化(转)
上一次我发了一篇文章叫“微信打开网址添加在浏览器中打开提示”,里面我发出来了三个代码,分别是纯JS.js+html.jQuery+HTML代码.今天来一个简化版带可以关闭的按钮操作.使用的是纯JS+H ...
- 打开mysql时,提示 1040,Too many connections
打开mysql时,提示 1040,Too many connections,这样就无法打开数据库,看不了表里边的内容了. 出现这个问题的原因是,同时对数据库的连接数过大,mysql默认的最大连接数是1 ...
- [WPF]ListView点击列头排序功能实现
[转] [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...
- Geany 编辑器打开 高亮所选单词 功能
Geany 编辑器打开 高亮所选单词 功能 在Ubuntu 系统的Software Center 工具中,搜索到geany, 下方有个 Miscellanous Plugins for Geany, ...
- 如何打开Intellij IDEA的代码提示功能/联想/自动联想
1.默认情况下,我们输入代码时IDEA会自动弹出代码提示框,这时候是不用按快捷键的,直接使用方向键选择我们需要的条目就可以了. 2:如果我们想主动 使用代码提示时,默认的快捷键是CTRL+空格,但是中 ...
随机推荐
- javascript 文本框中,判断回车键触发事件 兼容IE&FireFox
1.onkeypress&onkeydown区别 onkeypress 事件在用户按下并放开任何字母数字键时发生.但是系统按钮(例如:箭头键.功能键)无法得到识别. onkeydown 事件在 ...
- 【SQLite】使用replace替换字段中的字符
使用replace替换字段中的字符 如:替换production表中的specification字段中的两个空格为一个空格: update production set specification = ...
- spring-cloud-hystrix熔断
依赖pom <dependencyManagement> <dependencies> <dependency> <groupId>org.spring ...
- V9任何页面GET调用内容分页的说明
如标题,很多人想要在网站首页或其他的页面实现分页效果,说明如下: 一般特殊页面实现分页是通过GET语句的(论坛很多牛人用修改PHPCMS系统函数来实现,个人不推荐,因为你改了系统文件,不利于官方下一步 ...
- 普通用户开启AUTOTRACE 功能
AUTOTRACE是一个SQL*Plus工具,用于跟踪SQL的执行计划,收集执行时所耗用资源的统计信息.系统账户本身具有AUTOTRACE,其他账户需要通过手动赋予 一. 用系统账户登录(DBA) S ...
- CodeForces 18C
Description Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In eac ...
- shell echo打印换行的方法
echo要支持同C语言一样的\转义功能,只需要加上参数-e,如下所示: [~]#echo "Hello world.\nHello sea" Hello world.\nHello ...
- unity3d引擎程序员养成
标准流程:1. c++ Primer 英文版(第四或第五版)全部看完习题做完是必须的.渲染程序设计比较复杂,后期会用到c++的全部特性.c++学的越好后面越轻松.要看英文版,计算机翻来覆去就那么几个单 ...
- 简单的SQL Server在线查看和执行系统
在大的公司里面,往往数据库和表都非常的多,一张表的字段也是会有几十个.如果这么多的表和字段没有说明注释的话,查看起来会一头雾水,新来的或者别的部门的同事看到表和字段会完全不知道是干嘛的,只能靠名称来猜 ...
- python字符串内容替换的方法(转载)
python字符串内容替换的方法 时间:2016-03-10 06:30:46来源:网络 导读:python字符串内容替换的方法,包括单个字符替换,使用re正则匹配进行字符串模式查找与替换的方法. ...