歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:擴展的ListView實例

Android開發教程:擴展的ListView實例

日期:2017/3/1 10:33:07   编辑:Linux編程
從網上看了個擴展的Android listView的例子,感覺還蠻炫,自己也試著做了下。

由於比較簡單,所有就直接上代碼:

[html]
  1. public class ExtendedListView extends ExpandableListActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. // TODO Auto-generated method stub
  5. super.onCreate(savedInstanceState);
  6. MyExpandableListAdapter adapter=new MyExpandableListAdapter();
  7. setListAdapter(adapter);
  8. }
  9. public class MyExpandableListAdapter extends BaseExpandableListAdapter{
  10. public String[] groups={"我的好友","大學同學","高中同學"};
  11. public String[][] childrens={{"小張","小李","小麗","向明"},{"向明","向明","向明","向明"},{"向明","向明","向明","向明"}};
  12. public Object getChild(int groupPosition, int childPosition) {
  13. // TODO Auto-generated method stub
  14. return childrens[groupPosition][childPosition];
  15. }
  16. public long getChildId(int groupPosition, int childPosition) {
  17. // TODO Auto-generated method stub
  18. return childPosition;
  19. }
  20. public View getChildView(int groupPosition, int childPosition,
  21. boolean isLastChild, View convertView, ViewGroup parent) {
  22. // TODO Auto-generated method stub
  23. TextView textView=getGenericView();
  24. textView.setText(getChild(groupPosition, childPosition).toString());
  25. return textView;
  26. }
  27. //新建一個TextView
  28. public TextView getGenericView() {
  29. // Layout parameters for the ExpandableListView
  30. AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
  31. ViewGroup.LayoutParams.MATCH_PARENT, 64);
  32. TextView textView = new TextView(ExtendedListView.this);
  33. textView.setLayoutParams(lp);
  34. // Center the text vertically
  35. textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
  36. // Set the text starting position
  37. textView.setPadding(36, 0, 0, 0);
  38. return textView;
  39. }
  40. public int getChildrenCount(int groupPosition) {
  41. // TODO Auto-generated method stub
  42. return childrens[groupPosition].length;
  43. }
  44. public Object getGroup(int groupPosition) {
  45. // TODO Auto-generated method stub
  46. return groups[groupPosition];
  47. }
  48. public int getGroupCount() {
  49. // TODO Auto-generated method stub
  50. return groups.length;
  51. }
  52. public long getGroupId(int groupPosition) {
  53. // TODO Auto-generated method stub
  54. return groupPosition;
  55. }
  56. public View getGroupView(int groupPosition, boolean isExpanded,
  57. View convertView, ViewGroup parent) {
  58. // TODO Auto-generated method stub
  59. TextView textView = getGenericView();
  60. textView.setText(getGroup(groupPosition).toString());
  61. return textView;
  62. }
  63. public boolean hasStableIds() {
  64. // TODO Auto-generated method stub
  65. return true;
  66. }
  67. public boolean isChildSelectable(int groupPosition, int childPosition) {
  68. // TODO Auto-generated method stub
  69. return true;
  70. }
  71. }
  72. }
看一下運行在模擬器上的效果:

Copyright © Linux教程網 All Rights Reserved