亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

listView 添加多個不同的adapter

系統 1642 0

有時候我們想在listView上分類,或者呢 有時候一行顯示兩列內容,有時候需要三列內容 ,那怎么實現呢,這里呢就要使用

Java代碼
  1. class Section{
  2. Stringcaption;
  3. Adapteradapter;
  4. Section(Stringcaption,Adapteradapter){
  5. this .caption=caption;
  6. this .adapter=adapter;
  7. }
  8. }

自定義一個類,這個類呢包含多個adapter就可以了,想用那種就用那種。

Java代碼
  1. abstract public class SectionedAdapter extends BaseAdapter{
  2. abstract protected ViewgetHeaderView(Stringcaption, int index,ViewconvertView,ViewGroupparent);
  3. private List<Section>sections= new ArrayList<Section>();
  4. private static int TYPE_SECTION_HEADER= 0 ;
  5. public SectionedAdapter(){
  6. super ();
  7. }
  8. public void addSection(Stringcaption,Adapteradapter){
  9. sections.add( new Section(caption,adapter));
  10. }
  11. public ObjectgetItem( int position){
  12. for (Sectionsection: this .sections){
  13. if (position== 0 ){
  14. return (section);
  15. }
  16. int size=section.adapter.getCount()+ 1 ;
  17. if (position<size){
  18. return (section.adapter.getItem(position- 1 ));
  19. }
  20. position-=size;
  21. }
  22. return ( null );
  23. }
  24. public int getCount(){
  25. int total= 0 ;
  26. for (Sectionsection: this .sections){
  27. total+=section.adapter.getCount()+ 1 ; //addoneforheader
  28. }
  29. return (total);
  30. }
  31. public int getViewTypeCount(){
  32. int total= 1 ; //onefortheheader,plusthosefromsections
  33. for (Sectionsection: this .sections){
  34. total+=section.adapter.getViewTypeCount();
  35. }
  36. return (total);
  37. }
  38. public int getItemViewType( int position){
  39. int typeOffset=TYPE_SECTION_HEADER+ 1 ; //startcountingfromhere
  40. for (Sectionsection: this .sections){
  41. if (position== 0 ){
  42. return (TYPE_SECTION_HEADER);
  43. }
  44. int size=section.adapter.getCount()+ 1 ;
  45. if (position<size){
  46. return (typeOffset+section.adapter.getItemViewType(position- 1 ));
  47. }
  48. position-=size;
  49. typeOffset+=section.adapter.getViewTypeCount();
  50. }
  51. return (- 1 );
  52. }
  53. public boolean areAllItemsSelectable(){
  54. return ( false );
  55. }
  56. public boolean isEnabled( int position){
  57. return (getItemViewType(position)!=TYPE_SECTION_HEADER);
  58. }
  59. @Override
  60. public ViewgetView( int position,ViewconvertView,
  61. ViewGroupparent){
  62. int sectionIndex= 0 ;
  63. for (Sectionsection: this .sections){
  64. if (position== 0 ){
  65. return (getHeaderView(section.caption,sectionIndex,convertView,parent));
  66. }
  67. int size=section.adapter.getCount()+ 1 ;
  68. if (position<size){
  69. return (section.adapter.getView(position- 1 ,convertView,parent));
  70. }
  71. position-=size;
  72. sectionIndex++;
  73. }
  74. return ( null );
  75. }
  76. @Override
  77. public long getItemId( int position){
  78. return (position);
  79. }
  80. class Section{
  81. Stringcaption;
  82. Adapteradapter;
  83. Section(Stringcaption,Adapteradapter){
  84. this .caption=caption;
  85. this .adapter=adapter;
  86. }
  87. }
  88. }

然后主類就是

Java代碼
  1. public class SectionedDemo extends ListActivity{
  2. private static String[]items={ "lorem" , "ipsum" , "dolor" , "purus" };
  3. @Override
  4. public void onCreate(Bundleicicle){
  5. super .onCreate(icicle);
  6. setContentView(R.layout.main);
  7. adapter.addSection( "Original" , new ArrayAdapter<String>( this ,
  8. android.R.layout.simple_list_item_1,
  9. items));
  10. List<String>list=Arrays.asList(items);
  11. Collections.shuffle(list);
  12. adapter.addSection( "Shuffled" , new ArrayAdapter<String>( this ,
  13. android.R.layout.simple_list_item_1,
  14. list));
  15. list=Arrays.asList(items);
  16. Collections.shuffle(list);
  17. adapter.addSection( "Re-shuffled" , new ArrayAdapter<String>( this ,
  18. android.R.layout.simple_list_item_1,
  19. list));
  20. setListAdapter(adapter);
  21. }
  22. SectionedAdapteradapter= new SectionedAdapter(){
  23. protected ViewgetHeaderView(Stringcaption, int index,ViewconvertView,ViewGroupparent){
  24. TextViewresult=(TextView)convertView;
  25. if (convertView== null ){
  26. result=(TextView)getLayoutInflater().inflate(R.layout.header, null );
  27. }
  28. result.setText(caption);
  29. return (result);
  30. }
  31. };
  32. }

其他的就需要你自己變化了,我這里只是吧內容打亂。
listView 添加多個不同的adapter
有些東西我只是簡單調解沒有源碼或者我認為很簡單的就不提供了。

listView 添加多個不同的adapter


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: sihu国产精品永久免费 | 亚洲最大网 | 国产亚洲精品一区二区在线播放 | 6080一级毛片 | 夜夜做日日做夜夜爽 | 亚洲欧美日韩高清一区二区三区 | 日韩精品成人免费观看 | 噜噜噜天天躁狠狠躁夜夜精品 | 日韩女人做爰大片 | www.四虎在线观看 | 亚洲一区二区三区免费观看 | 国产成人女人视频在线观看 | 在线观看人成网站深夜免费 | 黄色成人免费网站 | 国产视频一区在线观看 | 麻豆精品在线 | 中文久久| 精品一区二区三区在线观看l | 欧美亚洲另类色国产综合 | 亚洲精品国产成人一区二区 | 成人欧美一区二区三区 | 九九综合九九 | 成人免费视频一区 | 欧美三级纯黄版 | 亚洲成人91| 国产成人亚综合91精品首页 | 天天干夜操 | 日本三级强在线观看 | 欧美理论在线 | 亚洲免费观看 | 免费一级毛片在线播放放视频 | 97理论片 | 亚洲乱码中文字幕 | 国产成人小视频在线观看 | 夜色资源在线观看免费 | 欧美性猛交xxxx免费看久久 | 中文字幕在线免费看 | 久久久久久人精品免费费看 | 国产精品欧美日韩精品 | 国产精品播放 | 九月丁香婷婷亚洲综合色 |