ListView分組實現方案(一),listview分組
分組的效果如下表格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 對應的itemItem item = getItem(position);//擷取groupIndexInt 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); }}