分组的效果如下表格item列所示:
listView分组思路:在item.xml配置文件里面定义一个显示组名的TextView,该TextView只在当前分组的第一个时显示,其它的默认不显示(即View.GONE);
上面其余列的说明:
position列:对应getView方法里面的position参数;
groupIndex:当然position对应的item属于第几个分组,或者说当前分组的索引号。
group_position:当前分组组名所在的item在listView中所处的位置position,比如分组A在listView中的position位置为0,分组B在listView中对应的position为4
item.xml类似如下效果:
//上下布局的linearLayout <LinearLayout> //用来显示组名比如A <TextView id="group_name"/ > //用来显示item的具体信息比如a1 <TextView id="item_info"/> </LinearLayout>
具体实现思路以及伪代码(getView里面进行处理判断:
1.判断当前position对应的item属于哪一个或者第几个分组;并获取该分组的索引groupIndex
2.根据groupIndex来获取group_position的值,如果group_position==position那么就让group_name对应的textView 显示否则设置为View.GONE;
综上所述整体上getView的伪代码如下:
假设用一个String[] group_names = {"A","B","C",...}来保存分组的名称
int[] group_positions = {0,4}用来保存当前分组在listView中的位置
Int groupIndex = getGroupIndex(position); Int group_position = getGroup_Position(groupIndex); If(group_position==position){ group_name.setVisiable(true); group_name.setText(group_names(groupIndex)); }else{ group_name.setVisiable(false); }
获取当前item属于哪一个分组getGroupIndex(position)的伪代码如下
//获取当前position 对应的item Item item = getItem(position); //获取groupIndex Int groupIndex = Arrays.binarySearch(group_names,item.getXXX()) return groupIndex>=0? index:-index-2; item.getXXX()是获取item中显示字符串第一个首字母的大写字母 //那么获取当前分组在ListView中对应位置的getGroupPosition(groupIndex)的代码就如下所 return group_positions.get(groupIndex)
关于ListView分组,Android提供了SectionIndexer接口,该接口提供了三个方法,实现分组的思路大体上跟上面讲的差不多,就不在赘述(吐槽,个人感觉SectionIndexer的名字不如GroupIndexer好)
下面贴上自己的测试代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/myListView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
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" > <!-- 分组名 --> <TextView android:id="@+id/group_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:color/darker_gray"/> <!-- 显示信息 --> <TextView android:id="@+id/item_info" android:layout_width="wrap_content" android:layout_height="80sp" android:textSize="45sp" /> </LinearLayout>
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView list = (ListView) findViewById(R.id.myListView); ArrayList<String> stringList = InitListData(); MyAdapter adapter = new MyAdapter(this, stringList); list.setAdapter(adapter); } private ArrayList<String> InitListData() { ArrayList<String> stringList = new ArrayList<String>(); stringList.add("aenegli"); stringList.add("abddaf"); stringList.add("are"); stringList.add("care"); stringList.add("ahow"); stringList.add("abide"); stringList.add("bandidate"); stringList.add("bapture"); stringList.add("catch"); stringList.add("call"); stringList.add("abuse"); stringList.add("cow"); //排个序 Collections.sort(stringList); return stringList; } }
public class MyAdapter extends BaseAdapter { //分组名 private char groupNames[] = { 'A','B','C' }; private List<Integer> groupPositions; private List<String> stringArray; //初始化groupPositions private void initGroupPositions(){ groupPositions = new ArrayList<Integer>(); for (int i = 0; i < stringArray.size(); i++) { if(i==0){//第一条数据 groupPositions.add(0); continue; } if(i != groupPositions.size()){ String currentStr = stringArray.get(i); String preStr = stringArray.get(i-1); char firstCharOfCurrentStr = currentStr.charAt(0); char firstCharOfPreStr = preStr.charAt(0); if(firstCharOfCurrentStr!=firstCharOfPreStr){ groupPositions.add(i); } } } } private Context context; public MyAdapter(Context context, ArrayList<String> arr) { stringArray = arr; this.context = context; initGroupPositions(); Log.e("groupPositions== ",groupPositions.size()+""); } public int getCount() { return stringArray.size(); } public String getItem(int position) { return stringArray.get(position); } public long getItemId(int position) { return position; } public View getView(int position, View view, ViewGroup parent) { LayoutInflater inflate = ((Activity) context).getLayoutInflater(); view = (View) inflate.inflate(R.layout.item, null); TextView groupNameView = (TextView) view.findViewById(R.id.group_name); String itemStr = stringArray.get(position); //分组处理 int groupIndex = getGroupIndex(position); int groupPosition = getGroupPosition(groupIndex); if(position==groupPosition){ groupNameView.setText(groupNames[groupIndex]+""); groupNameView.setVisibility(View.VISIBLE); }else{ groupNameView.setVisibility(View.GONE); } TextView textView = (TextView) view.findViewById(R.id.item_info); textView.setText(itemStr); return view; } //根据position获取当前item属于第几个group,也就是获取group的索引 public int getGroupIndex(int position) { String itemStr = getItem(position); char firstChar = itemStr.toUpperCase().charAt(0); int groupIndex = Arrays.binarySearch(groupNames, firstChar); Log.e("firstChar--groupIndex",firstChar+"---"+groupIndex); return groupIndex>=0?groupIndex:-groupIndex-2; } //根据groupIndex来获取当前分组对应于ListView中的position public int getGroupPosition(int groupIndex){ return groupPositions.get(groupIndex); } }