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

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條評論
主站蜘蛛池模板: 精品久久洲久久久久护士免费 | 欧美午夜精品久久久久免费视 | 国产亚洲精品一区久久 | 第九色激情 | 玖玖在线免费视频 | 久久久久在线观看 | 99视频99| 久久精品国产精品国产精品污 | 国产一区二区不卡免费观在线 | 在线观看精品91老司机 | 国内视频自拍 | 99热热| 久久精品国产免费看久久精品 | 神马老子不卡视频在线 | 快射视频在线观看 | 香蕉亚洲欧洲在线一区 | 免费成人一级片 | 九九热re| 天天操夜夜艹 | 爱爱一级视频 | 国产精品二区三区 | 一区二区三区精品国产欧美 | 久久久久久久亚洲精品一区 | 久久综合性 | 香蕉视频网站在线播放 | 久久久久久夜精品精品免费 | 91国内精品线免费播放 | 国产短视频精品区第一页 | 亚洲欧洲日产国码二区首页 | 亚洲精品在线观看视频 | 国产精品第一页爽爽影院 | 久久r视频| 特级全黄一级毛片免费 | 日日夜夜嗷嗷叫 | 啊用力嗯快国产在线观看 | 日本亚洲欧美国产日韩ay高清 | 久久www免费人成精品香蕉 | 国产欧美日韩中文字幕 | 99视频精品在线 | 亚洲综合图片区 | 97se色综合一区二区二区 |