黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

listView 添加多個不同的adapter

系統 1986 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條評論