首页 > 代码库 > ListView 实现分组
ListView 实现分组
1:FragmentHack4.java
/** * Created by y on 15-1-2. */public class FragmentHack4 extends Fragment{ View view; List<String> names; ListView lvNames; NameSectionAdapter adapter; TextView tvHeader; int topVisiblePos; @Override public void onAttach(Activity activity) { super.onAttach(activity); names = new ArrayList<String>(); //测试数据 String []t={"A","B","C","D","E","F"}; for(int i=0;i<t.length;i++){ for(int j=0;j<10;j++){ names.add(t[i]+"00"+j); } } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_hack4, container,false); tvHeader = (TextView)view.findViewById(R.id.tvHeader); lvNames = (ListView)view.findViewById(R.id.lvNames); adapter = new NameSectionAdapter(getActivity(),R.layout.list_name_item,names); lvNames.setAdapter(adapter); //设置第一个分组栏数据 setTopHeader(0); lvNames.setOnScrollListener( lvOnScrollListener()); return view; } AbsListView.OnScrollListener lvOnScrollListener(){ return new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView absListView, int i) { } @Override public void onScroll(AbsListView absListView, int i, int i2, int i3) { if(i!=topVisiblePos){ topVisiblePos = i; setTopHeader(i); } } }; } private void setTopHeader(int pos){ tvHeader.setText(names.get(pos).substring(0,1)); }}
2:NameSectionAdapter.java
/** * Created by y on 15-1-2. */public class NameSectionAdapter extends ArrayAdapter<String>{ private int resourceId; public NameSectionAdapter(Context context, int resourceId, List<String> objects){ super(context,resourceId,objects); this.resourceId = resourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { String name = getItem(position); ViewHolder holder; if(convertView==null){ convertView = LayoutInflater.from(getContext()).inflate(resourceId,null); holder = new ViewHolder(); holder.tvHeader = (TextView)convertView.findViewById(R.id.tvHeader); holder.tvName = (TextView)convertView.findViewById(R.id.tvName); convertView.setTag(holder); }else{ holder = (ViewHolder)convertView.getTag(); } holder.tvName.setText(name); //如果是第一项或者首字母发生了变化,则显示分组栏,否则隐藏 if(position==0 || getItem(position-1).charAt(0)!=name.charAt(0)){ holder.tvHeader.setVisibility(View.VISIBLE); holder.tvHeader.setText(name.substring(0,1)); }else{ holder.tvHeader.setVisibility(View.GONE); } return convertView; } static class ViewHolder{ public TextView tvHeader; public TextView tvName; }}
3:布局文件:
(1)fragment_hack4.xml
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lvNames" android:layout_width="match_parent" android:layout_height="match_parent"/> <include layout="@layout/list_header"/></FrameLayout>
(2)list_header.xml
<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tvHeader" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#0000ff" android:textColor="#ffffff" style="@android:style/TextAppearance.Large" android:textStyle="bold"/>
(3)list_name_item.xml
<?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="wrap_content" android:orientation="vertical"> <include layout="@layout/list_header"/> <TextView android:id="@+id/tvName" style="@android:style/TextAppearance.Small" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
4:运行结果
ListView 实现分组
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。