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

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條評論
主站蜘蛛池模板: jiz中国zz| 四虎在线免费播放 | 床上毛片 | 国产福利一区二区在线精品 | 久久伊人成人 | 亚洲视频在线一区二区 | 第一福利视频网 | 天天爽夜夜爽天天做夜夜做 | 国产波波社区精品视频 | 久久精品麻豆 | 日本一级毛片免费 | 草草影院第一页yycccom | 综合图片区 | 一区二区三区四区 | 色狗综合| 91视频精品 | 国产精品国产三级国产专区5o | 成人一级片在线观看 | 一级片短视频 | 精品视频在线观看一区二区 | 国产男女猛视频在线观看网站 | 亚洲成人网在线 | 成人a免费α片在线视频网站 | 青青国产成人久久91 | 黄色的视频免费看 | 日韩久久精品视频 | 久久综合99re88久久爱 | 天海翼一区二区三区高清视频 | 国产精品国产精品国产专区不卡 | 久热草在线 | 欧美xxx精品 | 成人区精品一区二区不卡亚洲 | 久久国产精品范冰啊 | 国产高清不卡 | 天天弄天天干 | 国产在线成人一区二区 | 成人欧美在线观看免费视频 | 韩国 欧美 日产 国产精品 | 人成午夜免费大片在线观看 | 天天操天天干天天插 | 欧美日韩亚洲视频 |