本文只让fastscroll具有提示分组功能,但listview并不显示分组,如果想让分组的listview显示fastscroll,看下篇。

1,在listview中打开fastscroll

2,自定义fastscroll的样式,其中包括分组相关的样式

3,代码如下:

主要是实现了sectionIndexer接口

 import java.util.ArrayList;

 import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView; import com.example.fastscroll.R; public class CustomFastScrollAdapter extends BaseAdapter implements SectionIndexer{ private LayoutInflater mInflater;
private Context mContext; /*
* 保存当前section的第一条记录的Position
* Character是section,Integer是section对应的第一条数据的position
*/
/*
* 保存每个section的第一条数据的position
* firstPositions的下标 firstPositions的值
* 0 0
* 1 2
* 2 4
* 3 6
*/ private Integer firstPositions[]; /* 用空间代时间,把数据中的每个position对应的section index存下来
* 如:
* sectionIndexes的下标 sectionIndexes数组中的值(section的下标)
* 0 0
* 1 0
* 2 1
* 3 1
* 4 2
* 5 2
* 6 3
* ... ...
*/
private int sectionIndexes[];//保存每个position对应的section index private Character sections [] ;
private String countries[]= { "Afghanistan", "Albania", "Algeria", "American Samoa",
"Andorra", "Angola", "Anguilla", "Antarctica",
"Antigua and Barbuda", "Argentina", "Armenia", "Aruba",
"Australia", "Austria", "Azerbaijan",
"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", "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",
"Democratic Republic of the Congo", "Denmark", "Djibouti",
"Dominica", "Dominican Republic",
"East Timor", "Ecuador", "Egypt",
"El Salvador", "Equatorial Guinea", "Eritrea", "Estonia","Ethiopia",
"Faeroe Islands", "Falkland Islands", "Fiji",
"Finland", "Former Yugoslav Republic of Macedonia", "France",
"French Guiana", "French Polynesia", "French Southern Territories",
"Gabon", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece",
"Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala",
"Guinea", "Guinea-Bissau", "Guyana",
"Haiti","Heard Island and McDonald Islands", "Honduras", "Hong Kong","Hungary",
"Iceland", "India", "Indonesia", "Iran", "Iraq","Ireland", "Israel","Italy",
"Jamaica", "Japan", "Jordan",
"Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",
"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya",
"Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Madagascar",
"Malawi", "Malaysia", "Maldives", "Mali", "Malta",
"Marshall Islands", "Martinique", "Mauritania", "Mauritius",
"Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia",
"Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
"Nauru", "Nepal", "Netherlands", "Netherlands Antilles",
"New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria",
"Niue", "Norfolk Island", "North Korea", "Northern Marianas",
"Norway", "Oman", "Pakistan", "Palau", "Panama",
"Papua New Guinea", "Paraguay", "Peru", "Philippines",
"Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",
"Reunion", "Romania", "Russia", "Rwanda", "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", "Taiwan", "Tajikistan", "Tanzania", "Thailand",
"The Bahamas", "The Gambia", "Togo", "Tokelau", "Tonga",
"Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan",
"Turks and Caicos Islands", "Tuvalu", "Uganda",
"Ukraine", "United Arab Emirates", "United Kingdom",
"United States", "United States Minor Outlying Islands", "Uruguay",
"Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Vietnam","Virgin Islands",
"Wallis and Futuna", "Western Sahara", "Yemen", "Yugoslavia",
"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"
};*/ @Override
public int getCount() {
return countries.length;
} @Override
public Object getItem(int position) {
return countries[position];
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_custom_fs, parent,false);
}
ImageView icon = (ImageView) convertView.findViewById(R.id.iv_icon);
icon.setImageResource(R.drawable.list_item_custom_fs_icon);
TextView content = (TextView) convertView.findViewById(R.id.tv_content); content.setText(countries[position]); return convertView;
} public CustomFastScrollAdapter(Context ctxt){
mContext = ctxt;
mInflater = LayoutInflater.from(mContext); //假设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); }
//返回分组数组,分组是用来显示预览的(它是内部分的)。如果分组中数据是string 那么直接显示它,如果是object,将调用其中的toString函数
@Override
public Object[] getSections() {
return sections;
}
//根据section的index返回在该section在整个数据中的首个index
@Override
public int getPositionForSection(int sectionIndex) {
sectionIndex %= sections.length;
return firstPositions[sectionIndex];
}
//根据数据的index返回所属section的index
@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 CustomFastScrollFrgmt extends Fragment {
private ListView mListView;
private CustomFastScrollAdapter mAdapter; void init(){
mAdapter = new CustomFastScrollAdapter(getActivity());
mListView.setAdapter(mAdapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frgmt_custom_fastscroll, container,false);
mListView = (ListView) v.findViewById(R.id.list_custom_fs);
init(); return v;
}
}
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" > <ListView
android:id="@+id/list_custom_fs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fastScrollEnabled="true" /> <!-- android:fastScrollAlwaysVisible="true" --> </LinearLayout>
   <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(2)不分组的listview 打开fastscroll的分组提示功能的更多相关文章

  1. FastScroll(3)分组的listview 打开fastscroll的分组提示功能

    1,让ListView显示分组(用两个layout) 2,让ListView实现sectionIndexer接口 3,代码如下: import java.util.ArrayList; import ...

  2. FastScroll(1)ListView打开FastScroll及自定义它的样式

    打开 FastScroll 方式 android:fastScrollEnabled="true" 它是AbsListView的属性. <?xml version=" ...

  3. 打开程序总是会提示“Enter password to unlock your login keyring” ,如何成功关掉?

    p { margin-bottom: 0.1in; line-height: 120% } 一.一开始我是按照网友所说的 : rm -f ~/.gnome2/keyrings/login.keyrin ...

  4. 微信扫描打开APP下载链接提示代码优化

    上一次我发了一篇文章叫“微信打开网址添加在浏览器中打开提示”,里面我发出来了三个代码,分别是纯JS.js+html.jQuery+HTML代码.今天来一个简化版带可以关闭的按钮操作.使用的是纯JS+H ...

  5. 微信扫描打开APP下载链接提示代码优化(转)

    上一次我发了一篇文章叫“微信打开网址添加在浏览器中打开提示”,里面我发出来了三个代码,分别是纯JS.js+html.jQuery+HTML代码.今天来一个简化版带可以关闭的按钮操作.使用的是纯JS+H ...

  6. 打开mysql时,提示 1040,Too many connections

    打开mysql时,提示 1040,Too many connections,这样就无法打开数据库,看不了表里边的内容了. 出现这个问题的原因是,同时对数据库的连接数过大,mysql默认的最大连接数是1 ...

  7. [WPF]ListView点击列头排序功能实现

    [转]   [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...

  8. Geany 编辑器打开 高亮所选单词 功能

    Geany 编辑器打开 高亮所选单词 功能 在Ubuntu 系统的Software Center 工具中,搜索到geany, 下方有个 Miscellanous Plugins for Geany, ...

  9. 如何打开Intellij IDEA的代码提示功能/联想/自动联想

    1.默认情况下,我们输入代码时IDEA会自动弹出代码提示框,这时候是不用按快捷键的,直接使用方向键选择我们需要的条目就可以了. 2:如果我们想主动 使用代码提示时,默认的快捷键是CTRL+空格,但是中 ...

随机推荐

  1. sbt的安装设置

    文章转载自http://my.oschina.net/u/915967/blog/146746 本文主要是windows平台的安装,linux环境操作类似. 首先到http://www.scala-s ...

  2. C#委托的异步调用1

    本文将主要通过“同步调用”.“异步调用”.“异步回调”三个示例来讲解在用委托执行同一个“加法类”的时候的的区别和利弊. 首先,通过代码定义一个委托和下面三个示例将要调用的方法: /*添加的命名空间 u ...

  3. 布局—column(属性)

    column-width:||用来定义多列中每列的宽度,用像素表示column-count:||用来定义多列中的列数,用数字来表示1-10columns: 200px 2||列宽和列数column-g ...

  4. jackson 解析结合类(需要传入Class, 和 Class.Class, 回调方法是List<Class>)

    import java.util.HashMap; import java.util.List; import org.codehaus.jackson.map.ObjectMapper; impor ...

  5. C#实现网络传输数据加密

    1. 分组密码 分组密码是将明文消息编码表示后数字序列划分成长为n的分组,各组分别在密钥的作用下进行变换输出等长的数字序列,即密文.一次加密一个数据组,加解密所使用的是同一密钥,故其通常也称为对称加密 ...

  6. ligerGrid 取得选中行的数据

    var SelectUserIdArr = []; function GetData() { var returnValue = ""; var grid = $("#m ...

  7. 小课堂Week8 例外处理设计的逆袭Part1

    小课堂Week8 例外处理设计的逆袭Part1 今天和大家讲一本书,书名是<例外处理设计的逆袭>. 为什么想讲这本书,是因为,例外处理在程序代码中到处存在,但是这些到底该如何写好,总觉得有 ...

  8. xshell连接linux服务器切换至oracle的sqlplus控制台时,无法使用回车键的解决方案!

    当使用xshell连接linux服务器后,切换至sqlplus控制台,当使用回车键时,出现^H的符号,貌似回车键不能用!解决方案: 1)$ stty erase ^H : 与退格键相关的设置是eras ...

  9. 转几篇WPF文章

    How to view word document in WPF application (CSVSTOViewWordInWPF) WPF 浏览PDF 文件 如何保存RichTextBox的文本到数 ...

  10. 1009. Product of Polynomials (25)

    #include <stdio.h> struct MyStruct { int exp; double coe; }; int main() { int k1,k2,i,j; MyStr ...