android 开发技巧(15)--为ListView添加分段标头
2016-03-03 11:41
513 查看
效果图:
![](http://img.blog.csdn.net/20160303112051958)
我记得github上有一个类似的效果github类似效果
说一下实现这个效果的思路:在列表项中嵌入分段标头,然后根据需要显示或者隐藏分段标头,创建一个特殊的TextView,让其叠加在列表的顶部,当列表滚动到一个新的分段时,就更新其内容
创建列表布局
创建一个xml,随列表滚动的分段标头和列表顶部的固定分段标头复用这个布局文件
header.xml
主布局list.xml
创建列表项布局文件list_item.xml,包含数据项和分段标头
SectionAdapter.java
主界面
我记得github上有一个类似的效果github类似效果
说一下实现这个效果的思路:在列表项中嵌入分段标头,然后根据需要显示或者隐藏分段标头,创建一个特殊的TextView,让其叠加在列表的顶部,当列表滚动到一个新的分段时,就更新其内容
创建列表布局
创建一个xml,随列表滚动的分段标头和列表顶部的固定分段标头复用这个布局文件
header.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/header" style="@android:style/TextAppearance.Small" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#0000ff" />
主布局list.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <include layout="@layout/header" /> </FrameLayout>
创建列表项布局文件list_item.xml,包含数据项和分段标头
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <include layout="@layout/header" /> <TextView android:id="@+id/label" style="@android:style/TextAppearance.Large" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
SectionAdapter.java
public class SectionAdapter extends ArrayAdapter<String> { private Activity activity; public SectionAdapter(Activity activity, String[] objects) { super(activity, R.layout.list_item, R.id.label, objects);//为自定义视图指定XML布局文件 this.activity = activity; } @Override public View getView(int position, View view, ViewGroup parent) { if (view == null) { view = activity.getLayoutInflater().inflate(R.layout.list_item, parent, false); } TextView header = (TextView) view.findViewById(R.id.header); String label = getItem(position); if (position == 0//检查列表项起始字母是否发生了改变,如果发生改变,该列表项就是分段中的第一项,修改分段标头的内容并显示该分段标头,否则隐藏 || getItem(position - 1).charAt(0) != label.charAt(0)) { header.setVisibility(View.VISIBLE); header.setText(label.substring(0, 1)); } else { header.setVisibility(View.GONE);//隐藏分段标头 } return super.getView(position, view, parent); } }
主界面
public class Hack26Activity extends ListActivity { private TextView topHeader;//用于访问分段标头 private int topVisiblePosition = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list); topHeader = (TextView) findViewById(R.id.header); setListAdapter(new SectionAdapter(this, Countries.COUNTRIES)); //设置滚动监听器,当用户滚动列表时,检查位置是否发生了变化,如果改变,调用setTopHeader更新悬浮的分段标头,当列表第一次显示时,确保根据第一个列表项初始化分段标头 getListView().setOnScrollListener( new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // Empty. } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (firstVisibleItem != topVisiblePosition) { topVisiblePosition = firstVisibleItem; setTopHeader(firstVisibleItem); } } }); setTopHeader(0); } private void setTopHeader(int pos) { final String text = Countries.COUNTRIES[pos].substring(0, 1); topHeader.setText(text);//更新文本内容 } }
相关文章推荐
- Android Camera HAL V3 Vendor Tag及V1,V3参数转换
- Android 隐藏ViewPager之后,控件点击事件无法响应的问题
- [Android 性能优化系列]内存之提升篇--应用应该如何管理内存
- Android Studio :Error:(1, 0) Plugin is too old, please update to a more recent version, or set AND……
- Android 动画实现原理
- Error:(54, 61) 错误: -source 1.6 中不支持 diamond 运算符 (请使用 -source 7 或更高版本以启用 diamond 运算符)
- [Android 性能优化系列]内存之基础篇--Android如何管理内存
- Adroid Studio 提示Error:(2) Error retrieving parent for item: No resource found that matches
- Android SQLite (五 ) 全面详解(三)
- Android SQLite (四 ) 全面详解(二)
- Android SQLite (三 ) 全面详解(一)
- umeng分享到QQ,分享成功,回调失败。
- Android开发Can't create handler inside thread that has not called Looper.prepare()
- Android <shape />的使用
- Android程序开发中单选按钮(RadioGroup)的使用详解
- 如何使用Android Studio开发Gradle插件
- 查看Android系统的源码 在线和离线方式 及阅读工具
- android 开发技巧(14)--通过ViewHolder优化适配器
- Java(Android)线程池妙用
- Android开发退出程序的方法汇总