全部产品

列表组件

更新时间:2020-06-19 14:55:36

AUPinnedSectionListView 提供分组的 ListView,在滑动中固定每个分组的标题。

注意:若要使用此控件,数据模型需区分 type,若不区分,则是普通的 ListView。

效果图

依赖

参见 快速开始

代码示例

  1. public class PinnedSectionActivity extends Activity{
  2. private AUPullRefreshView pullRefreshView;
  3. AUPullLoadingView mAUPullLoadingView;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.pinned_layout);
  8. pullRefreshView = (AUPullRefreshView) findViewById(R.id.pull_refresh);
  9. final AUPinnedSectionListView pinnedSectionListView = (AUPinnedSectionListView) findViewById(R.id.list_view);
  10. TextView tv = new TextView(this);
  11. tv.setText("nihao");
  12. pinnedSectionListView.addHeaderView(tv);
  13. pullRefreshView.setRefreshListener(new AUPullRefreshView.RefreshListener() {
  14. @Override
  15. public void onRefresh() {
  16. pullRefreshView.autoRefresh();
  17. pullRefreshView.postDelayed(new Runnable() {
  18. @Override
  19. public void run() {
  20. pullRefreshView.refreshFinished();
  21. }
  22. }, 1000);
  23. }
  24. @Override
  25. public AUPullLoadingView getOverView() {
  26. mAUPullLoadingView = (AUPullLoadingView) LayoutInflater.from(getBaseContext())
  27. .inflate(com.alipay.mobile.antui.R.layout.au_framework_pullrefresh_overview, null);
  28. Date date = new Date(1466577757265L);
  29. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  30. String dateString = formatter.format(date);
  31. mAUPullLoadingView.setIndicatorText(dateString);
  32. mAUPullLoadingView.setLoadingText(dateString);
  33. return mAUPullLoadingView;
  34. }
  35. @Override
  36. public boolean canRefresh() {
  37. return true;
  38. }
  39. });
  40. final SimpleAdapter adapter = new SimpleAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1);
  41. pinnedSectionListView.setAdapter(adapter);
  42. pinnedSectionListView.onFinishLoading(true);
  43. pinnedSectionListView.setOnLoadMoreListener(new AUPinnedSectionListView.OnLoadMoreListener() {
  44. @Override
  45. public void onLoadMoreItems() {
  46. pinnedSectionListView.postDelayed(new Runnable() {
  47. @Override
  48. public void run() {
  49. pinnedSectionListView.onFinishLoading(false);
  50. }
  51. }, 3000);
  52. }
  53. });
  54. }
  55. static class SimpleAdapter extends ArrayAdapter<Item> implements AUPinnedSectionListView.PinnedSectionListAdapter {
  56. public SimpleAdapter(Context context, int resource, int textViewResourceId) {
  57. super(context, resource, textViewResourceId);
  58. generateDataset('A', 'Z', false);
  59. }
  60. public void generateDataset(char from, char to, boolean clear) {
  61. if (clear) clear();
  62. final int sectionsNumber = to - from + 1;
  63. prepareSections(sectionsNumber);
  64. int sectionPosition = 0, listPosition = 0;
  65. for (char i=0; i<sectionsNumber; i++) {
  66. Item section = new Item(Item.SECTION, String.valueOf((char)('A' + i)));
  67. section.sectionPosition = sectionPosition;
  68. section.listPosition = listPosition++;
  69. onSectionAdded(section, sectionPosition);
  70. add(section);
  71. final int itemsNumber = (int) Math.abs((Math.cos(2f*Math.PI/3f * sectionsNumber / (i+1f)) * 25f));
  72. for (int j=0;j<itemsNumber;j++) {
  73. Item item = new Item(Item.ITEM, section.text.toUpperCase(Locale.ENGLISH) + " - " + j);
  74. item.sectionPosition = sectionPosition;
  75. item.listPosition = listPosition++;
  76. add(item);
  77. }
  78. sectionPosition++;
  79. }
  80. }
  81. protected void prepareSections(int sectionsNumber) { }
  82. protected void onSectionAdded(Item section, int sectionPosition) { }
  83. @Override public View getView(int position, View convertView, ViewGroup parent) {
  84. TextView view = (TextView) super.getView(position, convertView, parent);
  85. view.setTextColor(Color.DKGRAY);
  86. view.setTag("" + position);
  87. Item item = getItem(position);
  88. if (item.type == Item.SECTION) {
  89. //view.setOnClickListener(PinnedSectionListActivity.this);
  90. view.setBackgroundColor(Color.parseColor("#ff0000"));
  91. }
  92. return view;
  93. }
  94. @Override public int getViewTypeCount() {
  95. return 2;
  96. }
  97. @Override public int getItemViewType(int position) {
  98. return getItem(position).type;
  99. }
  100. @Override
  101. public boolean isItemViewTypePinned(int viewType) {
  102. return viewType == Item.SECTION;
  103. }
  104. }
  105. static class Item {
  106. public static final int ITEM = 0;
  107. public static final int SECTION = 1;
  108. public final int type;
  109. public final String text;
  110. public int sectionPosition;
  111. public int listPosition;
  112. public Item(int type, String text) {
  113. this.type = type;
  114. this.text = text;
  115. }
  116. @Override public String toString() {
  117. return text;
  118. }
  119. }
  120. }