StickyListHeaders:在listview中可以轻松的把headers添加到列表视图中组件

StickyListHeaders:在listview中可以轻松的把headers添加到列表视图中组件

StickyListHeaders 是一个在listview中可以轻松的把headers添加到列表视图中,类似于Android4.0 的手机通讯录的效果。很多IOS 也用这种效果,这个框架也可以用在不动的列表之中.

安装

Maven

1
2
3
4
5
     se.emilsjolander
     stickylistheaders
     x.x.x

gradle

1
2
3
dependencies {
     compile  'se.emilsjolander:stickylistheaders:x.x.x'
}

例子

让我们以一个活动或者片段开始,它看起来像这样

1
2
3
4
     android:id= "@+id/list"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent" />

现在在活动的Oncreate或者片段的Oncreateview 中你要加上以下代码

1
2
3
StickyListHeadersListView stickyList = (StickyListHeadersListView) findViewById(R.id.list);
MyAdapter adapter =  new MyAdapter( this );
stickyList.setAdapter(adapter);

如果你的list是一个国家列表,每个header是字母表中的一个字母

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
public class MyAdapter  extends BaseAdapter  implements StickyListHeadersAdapter {
 
     private String [] countries;
     private LayoutInflater inflater;
 
     public TestBaseAdapter(Context context) {
         inflater = LayoutInflater.from(context);
         countries = context.getResources().getStringArray(R.array.countries);
     }
 
     @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) {
         ViewHolder holder;
 
         if (convertView ==  null ) {
             holder =  new ViewHolder();
             convertView = inflater.inflate(R.layout.test_list_item_layout, parent,  false );
             holder.text = (TextView) convertView.findViewById(R.id.text);
             convertView.setTag(holder);
         else {
             holder = (ViewHolder) convertView.getTag();
         }
 
         holder.text.setText(countries[position]);
 
         return convertView;
     }
 
     @Override
     public View getHeaderView( int position, View convertView, ViewGroup parent) {
         HeaderViewHolder holder;
         if (convertView ==  null ) {
             holder =  new HeaderViewHolder();
             convertView = inflater.inflate(R.layout.header, parent,  false );
             holder.text = (TextView) convertView.findViewById(R.id.text);
             convertView.setTag(holder);
         else {
             holder = (HeaderViewHolder) convertView.getTag();
         }
         //set header text as first char in name
         String headerText =  "" + countries[position].subSequence( 0 1 ).charAt( 0 );
         holder.text.setText(headerText);
         return convertView;
     }
 
     @Override
     public long getHeaderId( int position) {
         //return the first character of the country as ID because this is what headers are based upon
         return countries[position].subSequence( 0 1 ).charAt( 0 );
     }
 
     class HeaderViewHolder {
         TextView text;
     }
 
     class ViewHolder {
         TextView text;
     }
 
}

开源地址:https://github.com/emilsjolander/StickyListHeaders

你可能感兴趣的:(Android开发)