您可以使用以下库轻松创建分段列表。您可以在六行中创建它
所以你可以在列表视图中为每个类别设置标题
分段列表库
分段列表示例
如何实现这一点,
// 1. Your data source
String[] books = new String[] {
"A Item", "F Item", "D Item",
"H Item", "T Item",
"A Item 1", "T Item 2"
};
// 2. Sort it
Arrays.sort(books, 0, books.length, Collator.getInstance());
// 3. Create your adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, books);
// 4. Create a Sectionizer
Sectionizer<String> alphabetSectionizer = new Sectionizer<String>() {
@Override
public String getSectionTitleForItem(String categoryName) {
return bookName;
}
};
// 5. Wrap your adapter within the SimpleSectionAdapter
SimpleSectionAdapter<String> sectionAdapter = new SimpleSectionAdapter<String>(this,
adapter, R.layout.section_header, R.id.title, alphabetSectionizer);
// 6. Set the adapter to your ListView
setListAdapter(sectionAdapter);