Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android之自定義控件-城市選擇,android控件

Android之自定義控件-城市選擇,android控件

編輯:關於android開發

Android之自定義控件-城市選擇,android控件


實現效果:

   

 

圖片素材:          

 

--> 首先, 城市數據字節放在 Json 文件, 就不網絡獲取了.

  city.json 存放 Json 數據:

1 { 2 "result": "0", 3 "message": "接口通信成功!", 4 "responseData": { 5 "History": [], 6 "Recent": [], 7 "Hot": [ 8 { 9 "id": "226", 10 "cityName": "上海" 11 }, 12 { 13 "id": "447", 14 "cityName": "北京" 15 }, 16 { 17 "id": "221", 18 "cityName": "南京" 19 }, 20 { 21 "id": "220", 22 "cityName": "蘇州" 23 }, 24 { 25 "id": "261", 26 "cityName": "杭州" 27 }, 28 { 29 "id": "476", 30 "cityName": "天津" 31 } 32 ], 33 "Common": [ 34 { 35 "title": "A", 36 "items": [ 37 { 38 "id": 664, 39 "cityName": "安慶" 40 }, 41 { 42 "id": 3590, 43 "cityName": "安吉縣" 44 } 45 ] 46 }, 47 { 48 "title": "B", 49 "items": [ 50 { 51 "id": 3612, 52 "cityName": "巴州" 53 }, 54 { 55 "id": 3366, 56 "cityName": "白銀" 57 }, 58 { 59 "id": 910, 60 "cityName": "蚌埠" 61 }, 62 { 63 "id": 2818, 64 "cityName": "保定" 65 }, 66 { 67 "id": 1068, 68 "cityName": "北海" 69 }, 70 { 71 "id": 447, 72 "cityName": "北京" 73 }, 74 { 75 "id": 882, 76 "cityName": "濱州" 77 }, 78 { 79 "id": 2119, 80 "cityName": "亳州" 81 }, 82 { 83 "id": 3449, 84 "cityName": "博爾塔拉蒙古自治州" 85 }, 86 { 87 "id": 2202, 88 "cityName": "濱海縣" 89 }, 90 { 91 "id": 2462, 92 "cityName": "濱海新區大港" 93 }, 94 { 95 "id": 2463, 96 "cityName": "濱海新區塘沽" 97 }, 98 { 99 "id": 2464, 100 "cityName": "濱海新區漢沽" 101 }, 102 { 103 "id": 2788, 104 "cityName": "寶應縣" 105 }, 106 { 107 "id": 3452, 108 "cityName": "博樂" 109 }, 110 { 111 "id": 3487, 112 "cityName": "霸州" 113 } 114 ] 115 }, 116 { 117 "title": "C", 118 "items": [ 119 { 120 "id": 1889, 121 "cityName": "滄州" 122 }, 123 { 124 "id": 2611, 125 "cityName": "昌吉自治州" 126 }, 127 { 128 "id": 2329, 129 "cityName": "長春" 130 }, 131 { 132 "id": 651, 133 "cityName": "長沙" 134 }, 135 { 136 "id": 2926, 137 "cityName": "長治" 138 }, 139 { 140 "id": 237, 141 "cityName": "常州" 142 }, 143 { 144 "id": 1959, 145 "cityName": "朝陽" 146 }, 147 { 148 "id": 3339, 149 "cityName": "潮州" 150 }, 151 { 152 "id": 279, 153 "cityName": "成都" 154 }, 155 { 156 "id": 3527, 157 "cityName": "承德" 158 }, 159 { 160 "id": 2095, 161 "cityName": "池州" 162 }, 163 { 164 "id": 2303, 165 "cityName": "赤峰" 166 }, 167 { 168 "id": 901, 169 "cityName": "滁州" 170 }, 171 { 172 "id": 621, 173 "cityName": "重慶" 174 }, 175 { 176 "id": 920, 177 "cityName": "崇明縣" 178 }, 179 { 180 "id": 2275, 181 "cityName": "曹縣" 182 }, 183 { 184 "id": 2620, 185 "cityName": "慈溪" 186 }, 187 { 188 "id": 2709, 189 "cityName": "常熟" 190 }, 191 { 192 "id": 2779, 193 "cityName": "茌平縣" 194 }, 195 { 196 "id": 3066, 197 "cityName": "巢湖" 198 }, 199 { 200 "id": 3135, 201 "cityName": "長豐縣" 202 }, 203 { 204 "id": 3630, 205 "cityName": "昌黎縣" 206 }, 207 { 208 "id": 3640, 209 "cityName": "章丘" 210 }, 211 { 212 "id": 3657, 213 "cityName": "崇州" 214 } 215 ] 216 }, 217 { 218 "title": "D", 219 "items": [ 220 { 221 "id": 3384, 222 "cityName": "達卡(孟加拉國)" 223 }, 224 { 225 "id": 1699, 226 "cityName": "大連" 227 }, 228 { 229 "id": 2700, 230 "cityName": "大慶" 231 }, 232 { 233 "id": 2290, 234 "cityName": "大同" 235 }, 236 { 237 "id": 579, 238 "cityName": "德州" 239 }, 240 { 241 "id": 3351, 242 "cityName": "定州" 243 }, 244 { 245 "id": 254, 246 "cityName": "東莞" 247 }, 248 { 249 "id": 1748, 250 "cityName": "東營" 251 }, 252 { 253 "id": 2208, 254 "cityName": "德清縣" 255 }, 256 { 257 "id": 2286, 258 "cityName": "東海縣" 259 }, 260 { 261 "id": 2608, 262 "cityName": "定遠縣" 263 }, 264 { 265 "id": 2715, 266 "cityName": "東台" 267 }, 268 { 269 "id": 3101, 270 "cityName": "砀山縣" 271 }, 272 { 273 "id": 3150, 274 "cityName": "大廠縣" 275 }, 276 { 277 "id": 3166, 278 "cityName": "東鄉縣" 279 }, 280 { 281 "id": 3281, 282 "cityName": "丹陽" 283 }, 284 { 285 "id": 3406, 286 "cityName": "東明縣" 287 }, 288 { 289 "id": 3618, 290 "cityName": "大悟縣" 291 }, 292 { 293 "id": 3643, 294 "cityName": "敦煌" 295 }, 296 { 297 "id": 3661, 298 "cityName": "大名縣" 299 } 300 ] 301 }, 302 { 303 "title": "F", 304 "items": [ 305 { 306 "id": 2176, 307 "cityName": "佛山" 308 }, 309 { 310 "id": 801, 311 "cityName": "福州" 312 }, 313 { 314 "id": 3165, 315 "cityName": "撫州" 316 }, 317 { 318 "id": 2881, 319 "cityName": "阜陽" 320 }, 321 { 322 "id": 2262, 323 "cityName": "富錦" 324 }, 325 { 326 "id": 2399, 327 "cityName": "鳳陽縣" 328 }, 329 { 330 "id": 3157, 331 "cityName": "繁峙縣" 332 }, 333 { 334 "id": 3162, 335 "cityName": "豐縣" 336 }, 337 { 338 "id": 3297, 339 "cityName": "阜南縣" 340 }, 341 { 342 "id": 3441, 343 "cityName": "奉化" 344 }, 345 { 346 "id": 3508, 347 "cityName": "阜寧縣" 348 }, 349 { 350 "id": 3509, 351 "cityName": "肥東縣" 352 }, 353 { 354 "id": 3541, 355 "cityName": "肥西縣" 356 }, 357 { 358 "id": 3544, 359 "cityName": "鳳台縣" 360 } 361 ] 362 }, 363 { 364 "title": "G", 365 "items": [ 366 { 367 "id": 2976, 368 "cityName": "贛州" 369 }, 370 { 371 "id": 3472, 372 "cityName": "廣元" 373 }, 374 { 375 "id": 1969, 376 "cityName": "廣州" 377 }, 378 { 379 "id": 709, 380 "cityName": "貴陽" 381 }, 382 { 383 "id": 898, 384 "cityName": "桂林" 385 }, 386 { 387 "id": 2002, 388 "cityName": "高唐縣" 389 }, 390 { 391 "id": 2129, 392 "cityName": "灌南縣" 393 }, 394 { 395 "id": 2224, 396 "cityName": "固始縣" 397 }, 398 { 399 "id": 2518, 400 "cityName": "贛榆縣" 401 }, 402 { 403 "id": 2734, 404 "cityName": "高密" 405 }, 406 { 407 "id": 2746, 408 "cityName": "高郵" 409 }, 410 { 411 "id": 2752, 412 "cityName": "灌雲縣" 413 }, 414 { 415 "id": 2819, 416 "cityName": "高碑店" 417 }, 418 { 419 "id": 3060, 420 "cityName": "古交" 421 }, 422 { 423 "id": 3385, 424 "cityName": "孤山1" 425 }, 426 { 427 "id": 3455, 428 "cityName": "沽源縣" 429 }, 430 { 431 "id": 3490, 432 "cityName": "固鎮縣" 433 }, 434 { 435 "id": 3502, 436 "cityName": "貴溪" 437 }, 438 { 439 "id": 3589, 440 "cityName": "瓜州縣" 441 }, 442 { 443 "id": 3614, 444 "cityName": "廣德縣" 445 }, 446 { 447 "id": 3619, 448 "cityName": "古田縣" 449 }, 450 { 451 "id": 3634, 452 "cityName": "格爾木" 453 }, 454 { 455 "id": 3699, 456 "cityName": "高安" 457 } 458 ] 459 }, 460 { 461 "title": "H", 462 "items": [ 463 { 464 "id": 558, 465 "cityName": "哈爾濱" 466 }, 467 { 468 "id": 2104, 469 "cityName": "海口" 470 }, 471 { 472 "id": 3633, 473 "cityName": "海西蒙古族藏族自治州" 474 }, 475 { 476 "id": 2233, 477 "cityName": "邯鄲" 478 }, 479 { 480 "id": 3652, 481 "cityName": "韓國首爾" 482 }, 483 { 484 "id": 2845, 485 "cityName": "漢中" 486 }, 487 { 488 "id": 261, 489 "cityName": "杭州" 490 }, 491 { 492 "id": 639, 493 "cityName": "合肥" 494 }, 495 { 496 "id": 895, 497 "cityName": "菏澤" 498 }, 499 { 500 "id": 1074, 501 "cityName": "賀州" 502 }, 503 { 504 "id": 3372, 505 "cityName": "鶴壁" 506 }, 507 { 508 "id": 3611, 509 "cityName": "衡陽" 510 }, 511 { 512 "id": 645, 513 "cityName": "呼和浩特" 514 }, 515 { 516 "id": 2205, 517 "cityName": "湖州" 518 }, 519 { 520 "id": 3397, 521 "cityName": "葫蘆島" 522 }, 523 { 524 "id": 594, 525 "cityName": "淮安" 526 }, 527 { 528 "id": 699, 529 "cityName": "淮北" 530 }, 531 { 532 "id": 1953, 533 "cityName": "淮南" 534 }, 535 { 536 "id": 3124, 537 "cityName": "黃岡" 538 }, 539 { 540 "id": 2451, 541 "cityName": "黃山" 542 }, 543 { 544 "id": 255, 545 "cityName": "惠州" 546 }, 547 { 548 "id": 1774, 549 "cityName": "海安縣" 550 }, 551 { 552 "id": 1840, 553 "cityName": "海陽" 554 }, 555 { 556 "id": 2347, 557 "cityName": "海門" 558 }, 559 { 560 "id": 2356, 561 "cityName": "黃骅" 562 }, 563 { 564 "id": 2878, 565 "cityName": "霍山縣" 566 }, 567 { 568 "id": 3125, 569 "cityName": "紅安縣" 570 }, 571 { 572 "id": 3266, 573 "cityName": "洪澤縣" 574 }, 575 { 576 "id": 3291, 577 "cityName": "懷來縣" 578 }, 579 { 580 "id": 3494, 581 "cityName": "霍邱縣" 582 } 583 ] 584 }, 585 { 586 "title": "J", 587 "items": [ 588 { 589 "id": 771, 590 "cityName": "吉林" 591 }, 592 { 593 "id": 470, 594 "cityName": "濟南" 595 }, 596 { 597 "id": 996, 598 "cityName": "濟寧" 599 }, 600 { 601 "id": 2230, 602 "cityName": "佳木斯" 603 }, 604 { 605 "id": 1536, 606 "cityName": "嘉興" 607 }, 608 { 609 "id": 1777, 610 "cityName": "嘉峪關" 611 }, 612 { 613 "id": 2242, 614 "cityName": "焦作" 615 }, 616 { 617 "id": 2893, 618 "cityName": "揭陽" 619 }, 620 { 621 "id": 654, 622 "cityName": "金華" 623 }, 624 { 625 "id": 2543, 626 "cityName": "錦州" 627 }, 628 { 629 "id": 2759, 630 "cityName": "晉城" 631 }, 632 { 633 "id": 2316, 634 "cityName": "晉中" 635 }, 636 { 637 "id": 2076, 638 "cityName": "荊門" 639 }, 640 { 641 "id": 2070, 642 "cityName": "景德鎮" 643 }, 644 { 645 "id": 702, 646 "cityName": "九江" 647 }, 648 { 649 "id": 1781, 650 "cityName": "酒泉" 651 }, 652 { 653 "id": 1723, 654 "cityName": "薊縣" 655 }, 656 { 657 "id": 2110, 658 "cityName": "巨野縣" 659 }, 660 { 661 "id": 2163, 662 "cityName": "金鄉縣" 663 }, 664 { 665 "id": 2496, 666 "cityName": "靖江" 667 }, 668 { 669 "id": 2584, 670 "cityName": "句容" 671 }, 672 { 673 "id": 2629, 674 "cityName": "江陰" 675 }, 676 { 677 "id": 2656, 678 "cityName": "莒南縣" 679 }, 680 { 681 "id": 2791, 682 "cityName": "嘉祥縣" 683 }, 684 { 685 "id": 2923, 686 "cityName": "吉木薩爾縣" 687 }, 688 { 689 "id": 3000, 690 "cityName": "莒縣" 691 }, 692 { 693 "id": 3034, 694 "cityName": "膠州" 695 }, 696 { 697 "id": 3213, 698 "cityName": "金湖縣" 699 }, 700 { 701 "id": 3219, 702 "cityName": "建湖縣" 703 }, 704 { 705 "id": 3256, 706 "cityName": "鄄城縣" 707 }, 708 { 709 "id": 3294, 710 "cityName": "靜海縣" 711 }, 712 { 713 "id": 3403, 714 "cityName": "金壇" 715 }, 716 { 717 "id": 3428, 718 "cityName": "嘉峪關" 719 }, 720 { 721 "id": 3539, 722 "cityName": "金寨縣" 723 }, 724 { 725 "id": 3548, 726 "cityName": "泾縣" 727 }, 728 { 729 "id": 3578, 730 "cityName": "績溪縣" 731 }, 732 { 733 "id": 3600, 734 "cityName": "介休" 735 }, 736 { 737 "id": 3677, 738 "cityName": "嘉善縣" 739 }, 740 { 741 "id": 3706, 742 "cityName": "吉首" 743 } 744 ] 745 }, 746 { 747 "title": "K", 748 "items": [ 749 { 750 "id": 907, 751 "cityName": "開封" 752 }, 753 { 754 "id": 1833, 755 "cityName": "昆明" 756 }, 757 { 758 "id": 2824, 759 "cityName": "昆山" 760 }, 761 { 762 "id": 3540, 763 "cityName": "康保縣" 764 }, 765 { 766 "id": 3613, 767 "cityName": "庫爾勒" 768 } 769 ] 770 }, 771 { 772 "title": "L", 773 "items": [ 774 { 775 "id": 3515, 776 "cityName": "來賓" 777 }, 778 { 779 "id": 618, 780 "cityName": "萊蕪" 781 }, 782 { 783 "id": 1545, 784 "cityName": "蘭州" 785 }, 786 { 787 "id": 993, 788 "cityName": "廊坊" 789 }, 790 { 791 "id": 2623, 792 "cityName": "麗江" 793 }, 794 { 795 "id": 3038, 796 "cityName": "麗水" 797 }, 798 { 799 "id": 762, 800 "cityName": "連雲港" 801 }, 802 { 803 "id": 3597, 804 "cityName": "遼源" 805 }, 806 { 807 "id": 1527, 808 "cityName": "聊城" 809 }, 810 { 811 "id": 1008, 812 "cityName": "臨沂" 813 }, 814 { 815 "id": 1067, 816 "cityName": "柳州" 817 }, 818 { 819 "id": 2721, 820 "cityName": "六安" 821 }, 822 { 823 "id": 2961, 824 "cityName": "六盤水" 825 }, 826 { 827 "id": 3553, 828 "cityName": "隴南" 829 }, 830 { 831 "id": 3312, 832 "cityName": "呂梁" 833 }, 834 { 835 "id": 267, 836 "cityName": "洛陽" 837 }, 838 { 839 "id": 2417, 840 "cityName": "鹿邑縣" 841 }, 842 { 843 "id": 2531, 844 "cityName": "洛川縣" 845 }, 846 { 847 "id": 2637, 848 "cityName": "廬江縣" 849 }, 850 { 851 "id": 2694, 852 "cityName": "溧陽" 853 }, 854 { 855 "id": 2712, 856 "cityName": "漣水縣" 857 }, 858 { 859 "id": 2902, 860 "cityName": "樂亭縣" 861 }, 862 { 863 "id": 3120, 864 "cityName": "臨朐縣" 865 }, 866 { 867 "id": 3202, 868 "cityName": "蘭陵縣" 869 }, 870 { 871 "id": 3324, 872 "cityName": "臨海" 873 }, 874 { 875 "id": 3409, 876 "cityName": "萊陽" 877 }, 878 { 879 "id": 3431, 880 "cityName": "龍口" 881 }, 882 { 883 "id": 3446, 884 "cityName": "林西縣" 885 }, 886 { 887 "id": 3517, 888 "cityName": "郎溪縣" 889 }, 890 { 891 "id": 3549, 892 "cityName": "利辛縣" 893 }, 894 { 895 "id": 3579, 896 "cityName": "臨桂縣" 897 }, 898 { 899 "id": 3582, 900 "cityName": "靈璧縣" 901 }, 902 { 903 "id": 3656, 904 "cityName": "灤平縣" 905 }, 906 { 907 "id": 3676, 908 "cityName": "臨清" 909 } 910 ] 911 }, 912 { 913 "title": "M", 914 "items": [ 915 { 916 "id": 271, 917 "cityName": "馬鞍山" 918 }, 919 { 920 "id": 3333, 921 "cityName": "梅州" 922 }, 923 { 924 "id": 3673, 925 "cityName": "美國鳳凰城" 926 }, 927 { 928 "id": 2402, 929 "cityName": "密雲縣" 930 }, 931 { 932 "id": 3390, 933 "cityName": "蒙城縣" 934 }, 935 { 936 "id": 3492, 937 "cityName": "明光" 938 }, 939 { 940 "id": 3675, 941 "cityName": "美國鳳凰城" 942 } 943 ] 944 }, 945 { 946 "title": "N", 947 "items": [ 948 { 949 "id": 276, 950 "cityName": "南昌" 951 }, 952 { 953 "id": 221, 954 "cityName": "南京" 955 }, 956 { 957 "id": 723, 958 "cityName": "南寧" 959 }, 960 { 961 "id": 238, 962 "cityName": "南通" 963 }, 964 { 965 "id": 678, 966 "cityName": "寧波" 967 }, 968 { 969 "id": 1135, 970 "cityName": "寧德" 971 }, 972 { 973 "id": 1826, 974 "cityName": "南昌縣" 975 }, 976 { 977 "id": 3682, 978 "cityName": "寧國" 979 } 980 ] 981 }, 982 { 983 "title": "P", 984 "items": [ 985 { 986 "id": 3510, 987 "cityName": "平潭縣" 988 }, 989 { 990 "id": 1130, 991 "cityName": "莆田" 992 }, 993 { 994 "id": 750, 995 "cityName": "濮陽" 996 }, 997 { 998 "id": 1839, 999 "cityName": "蓬萊" 1000 }, 1001 { 1002 "id": 2377, 1003 "cityName": "邳州" 1004 }, 1005 { 1006 "id": 2386, 1007 "cityName": "平陰縣" 1008 }, 1009 { 1010 "id": 2685, 1011 "cityName": "平遙縣" 1012 }, 1013 { 1014 "id": 2962, 1015 "cityName": "盤縣" 1016 }, 1017 { 1018 "id": 3284, 1019 "cityName": "普寧" 1020 }, 1021 { 1022 "id": 3542, 1023 "cityName": "沛縣" 1024 }, 1025 { 1026 "id": 3655, 1027 "cityName": "郫縣" 1028 } 1029 ] 1030 }, 1031 { 1032 "title": "Q", 1033 "items": [ 1034 { 1035 "id": 1070, 1036 "cityName": "欽州" 1037 }, 1038 { 1039 "id": 2211, 1040 "cityName": "秦皇島" 1041 }, 1042 { 1043 "id": 612, 1044 "cityName": "青島" 1045 }, 1046 { 1047 "id": 2599, 1048 "cityName": "衢州" 1049 }, 1050 { 1051 "id": 3466, 1052 "cityName": "泉州" 1053 }, 1054 { 1055 "id": 2296, 1056 "cityName": "啟東" 1057 }, 1058 { 1059 "id": 2408, 1060 "cityName": "青陽縣" 1061 }, 1062 { 1063 "id": 2612, 1064 "cityName": "奇台縣" 1065 }, 1066 { 1067 "id": 2861, 1068 "cityName": "青州" 1069 }, 1070 { 1071 "id": 3056, 1072 "cityName": "曲阜" 1073 }, 1074 { 1075 "id": 3069, 1076 "cityName": "遷安" 1077 }, 1078 { 1079 "id": 3488, 1080 "cityName": "齊河縣" 1081 }, 1082 { 1083 "id": 3501, 1084 "cityName": "全椒縣" 1085 }, 1086 { 1087 "id": 3623, 1088 "cityName": "青銅峽" 1089 }, 1090 { 1091 "id": 3662, 1092 "cityName": "沁陽" 1093 } 1094 ] 1095 }, 1096 { 1097 "title": "R", 1098 "items": [ 1099 { 1100 "id": 934, 1101 "cityName": "日照" 1102 }, 1103 { 1104 "id": 873, 1105 "cityName": "如東縣" 1106 }, 1107 { 1108 "id": 2147, 1109 "cityName": "如皋" 1110 }, 1111 { 1112 "id": 2896, 1113 "cityName": "乳山" 1114 }, 1115 { 1116 "id": 3523, 1117 "cityName": "榮成" 1118 } 1119 ] 1120 }, 1121 { 1122 "title": "S", 1123 "items": [ 1124 { 1125 "id": 1131, 1126 "cityName": "三明" 1127 }, 1128 { 1129 "id": 3093, 1130 "cityName": "三亞" 1131 }, 1132 { 1133 "id": 1569, 1134 "cityName": "汕頭" 1135 }, 1136 { 1137 "id": 2280, 1138 "cityName": "商丘" 1139 }, 1140 { 1141 "id": 226, 1142 "cityName": "上海" 1143 }, 1144 { 1145 "id": 981, 1146 "cityName": "上饒" 1147 }, 1148 { 1149 "id": 1866, 1150 "cityName": "紹興" 1151 }, 1152 { 1153 "id": 253, 1154 "cityName": "深圳" 1155 }, 1156 { 1157 "id": 273, 1158 "cityName": "沈陽" 1159 }, 1160 { 1161 "id": 3459, 1162 "cityName": "十堰" 1163 }, 1164 { 1165 "id": 746, 1166 "cityName": "石家莊" 1167 }, 1168 { 1169 "id": 220, 1170 "cityName": "蘇州" 1171 }, 1172 { 1173 "id": 732, 1174 "cityName": "宿遷" 1175 }, 1176 { 1177 "id": 1726, 1178 "cityName": "宿州" 1179 }, 1180 { 1181 "id": 2221, 1182 "cityName": "泗陽縣" 1183 }, 1184 { 1185 "id": 2335, 1186 "cityName": "濉溪縣" 1187 }, 1188 { 1189 "id": 2353, 1190 "cityName": "泗洪縣" 1191 }, 1192 { 1193 "id": 2561, 1194 "cityName": "壽光" 1195 }, 1196 { 1197 "id": 2697, 1198 "cityName": "沭陽縣" 1199 }, 1200 { 1201 "id": 2722, 1202 "cityName": "壽縣" 1203 }, 1204 { 1205 "id": 2749, 1206 "cityName": "射陽縣" 1207 }, 1208 { 1209 "id": 2932, 1210 "cityName": "睢寧縣" 1211 }, 1212 { 1213 "id": 3039, 1214 "cityName": "遂昌縣" 1215 }, 1216 { 1217 "id": 3171, 1218 "cityName": "沙河" 1219 }, 1220 { 1221 "id": 3186, 1222 "cityName": "三河" 1223 }, 1224 { 1225 "id": 3222, 1226 "cityName": "舒城縣" 1227 }, 1228 { 1229 "id": 3532, 1230 "cityName": "商城縣" 1231 }, 1232 { 1233 "id": 3642, 1234 "cityName": "雙流縣" 1235 }, 1236 { 1237 "id": 3644, 1238 "cityName": "泗縣" 1239 } 1240 ] 1241 }, 1242 { 1243 "title": "T", 1244 "items": [ 1245 { 1246 "id": 600, 1247 "cityName": "台州" 1248 }, 1249 { 1250 "id": 736, 1251 "cityName": "太原" 1252 }, 1253 { 1254 "id": 265, 1255 "cityName": "泰安" 1256 }, 1257 { 1258 "id": 606, 1259 "cityName": "泰州" 1260 }, 1261 { 1262 "id": 684, 1263 "cityName": "唐山" 1264 }, 1265 { 1266 "id": 476, 1267 "cityName": "天津" 1268 }, 1269 { 1270 "id": 1778, 1271 "cityName": "天水" 1272 }, 1273 { 1274 "id": 3318, 1275 "cityName": "通遼" 1276 }, 1277 { 1278 "id": 885, 1279 "cityName": "銅陵" 1280 }, 1281 { 1282 "id": 2982, 1283 "cityName": "吐魯番地區" 1284 }, 1285 { 1286 "id": 1312, 1287 "cityName": "桐廬縣" 1288 }, 1289 { 1290 "id": 1696, 1291 "cityName": "桐鄉" 1292 }, 1293 { 1294 "id": 2362, 1295 "cityName": "泰興" 1296 }, 1297 { 1298 "id": 2659, 1299 "cityName": "太倉" 1300 }, 1301 { 1302 "id": 2718, 1303 "cityName": "天台縣" 1304 }, 1305 { 1306 "id": 2785, 1307 "cityName": "滕州" 1308 }, 1309 { 1310 "id": 2983, 1311 "cityName": "吐魯番" 1312 }, 1313 { 1314 "id": 3132, 1315 "cityName": "太和縣" 1316 }, 1317 { 1318 "id": 3495, 1319 "cityName": "太湖縣" 1320 }, 1321 { 1322 "id": 3530, 1323 "cityName": "桐城" 1324 } 1325 ] 1326 }, 1327 { 1328 "title": "W", 1329 "items": [ 1330 { 1331 "id": 1563, 1332 "cityName": "威海" 1333 }, 1334 { 1335 "id": 756, 1336 "cityName": "濰坊" 1337 }, 1338 { 1339 "id": 768, 1340 "cityName": "溫州" 1341 }, 1342 { 1343 "id": 3300, 1344 "cityName": "烏蘭察布" 1345 }, 1346 { 1347 "id": 630, 1348 "cityName": "烏魯木齊" 1349 }, 1350 { 1351 "id": 222, 1352 "cityName": "無錫" 1353 }, 1354 { 1355 "id": 3259, 1356 "cityName": "吳忠" 1357 }, 1358 { 1359 "id": 1046, 1360 "cityName": "蕪湖" 1361 }, 1362 { 1363 "id": 421, 1364 "cityName": "武漢" 1365 }, 1366 { 1367 "id": 1779, 1368 "cityName": "武威" 1369 }, 1370 { 1371 "id": 985, 1372 "cityName": "婺源縣" 1373 }, 1374 { 1375 "id": 1051, 1376 "cityName": "蕪湖縣" 1377 }, 1378 { 1379 "id": 1965, 1380 "cityName": "汶上縣" 1381 }, 1382 { 1383 "id": 2914, 1384 "cityName": "五河縣" 1385 }, 1386 { 1387 "id": 2967, 1388 "cityName": "蔚縣" 1389 }, 1390 { 1391 "id": 3177, 1392 "cityName": "文登" 1393 }, 1394 { 1395 "id": 3313, 1396 "cityName": "文水縣" 1397 }, 1398 { 1399 "id": 3357, 1400 "cityName": "文安縣" 1401 }, 1402 { 1403 "id": 3360, 1404 "cityName": "渦陽縣" 1405 }, 1406 { 1407 "id": 3497, 1408 "cityName": "微山縣" 1409 }, 1410 { 1411 "id": 3686, 1412 "cityName": "無為縣" 1413 } 1414 ] 1415 }, 1416 { 1417 "title": "X", 1418 "items": [ 1419 { 1420 "id": 719, 1421 "cityName": "西安" 1422 }, 1423 { 1424 "id": 2137, 1425 "cityName": "西寧" 1426 }, 1427 { 1428 "id": 802, 1429 "cityName": "廈門" 1430 }, 1431 { 1432 "id": 3555, 1433 "cityName": "鹹陽" 1434 }, 1435 { 1436 "id": 3705, 1437 "cityName": "湘西州" 1438 }, 1439 { 1440 "id": 3462, 1441 "cityName": "孝感" 1442 }, 1443 { 1444 "id": 3156, 1445 "cityName": "忻州" 1446 }, 1447 { 1448 "id": 2506, 1449 "cityName": "新鄉" 1450 }, 1451 { 1452 "id": 2189, 1453 "cityName": "新余" 1454 }, 1455 { 1456 "id": 1590, 1457 "cityName": "信陽" 1458 }, 1459 { 1460 "id": 2569, 1461 "cityName": "邢台" 1462 }, 1463 { 1464 "id": 816, 1465 "cityName": "徐州" 1466 }, 1467 { 1468 "id": 3078, 1469 "cityName": "宣城" 1470 }, 1471 { 1472 "id": 1720, 1473 "cityName": "盱眙縣" 1474 }, 1475 { 1476 "id": 1869, 1477 "cityName": "新昌縣" 1478 }, 1479 { 1480 "id": 1926, 1481 "cityName": "興化" 1482 }, 1483 { 1484 "id": 2390, 1485 "cityName": "霞浦縣" 1486 }, 1487 { 1488 "id": 2580, 1489 "cityName": "響水縣" 1490 }, 1491 { 1492 "id": 2650, 1493 "cityName": "新沂" 1494 }, 1495 { 1496 "id": 3253, 1497 "cityName": "蕭縣" 1498 }, 1499 { 1500 "id": 3398, 1501 "cityName": "興城" 1502 }, 1503 { 1504 "id": 3489, 1505 "cityName": "象山縣" 1506 }, 1507 { 1508 "id": 3512, 1509 "cityName": "仙居縣" 1510 }, 1511 { 1512 "id": 3536, 1513 "cityName": "歙縣" 1514 } 1515 ] 1516 }, 1517 { 1518 "title": "Y", 1519 "items": [ 1520 { 1521 "id": 753, 1522 "cityName": "煙台" 1523 }, 1524 { 1525 "id": 2527, 1526 "cityName": "延安" 1527 }, 1528 { 1529 "id": 759, 1530 "cityName": "鹽城" 1531 }, 1532 { 1533 "id": 239, 1534 "cityName": "揚州" 1535 }, 1536 { 1537 "id": 2521, 1538 "cityName": "陽江" 1539 }, 1540 { 1541 "id": 2768, 1542 "cityName": "陽泉" 1543 }, 1544 { 1545 "id": 2341, 1546 "cityName": "宜昌" 1547 }, 1548 { 1549 "id": 3698, 1550 "cityName": "宜春" 1551 }, 1552 { 1553 "id": 1736, 1554 "cityName": "銀川" 1555 }, 1556 { 1557 "id": 2577, 1558 "cityName": "鷹潭" 1559 }, 1560 { 1561 "id": 1073, 1562 "cityName": "玉林" 1563 }, 1564 { 1565 "id": 1210, 1566 "cityName": "岳陽" 1567 }, 1568 { 1569 "id": 1766, 1570 "cityName": "運城" 1571 }, 1572 { 1573 "id": 1912, 1574 "cityName": "玉山縣" 1575 }, 1576 { 1577 "id": 2471, 1578 "cityName": "魚台縣" 1579 }, 1580 { 1581 "id": 2553, 1582 "cityName": "儀征" 1583 }, 1584 { 1585 "id": 2647, 1586 "cityName": "永城" 1587 }, 1588 { 1589 "id": 2662, 1590 "cityName": "義烏" 1591 }, 1592 { 1593 "id": 2869, 1594 "cityName": "宜興" 1595 }, 1596 { 1597 "id": 2917, 1598 "cityName": "余姚" 1599 }, 1600 { 1601 "id": 3110, 1602 "cityName": "揚中" 1603 }, 1604 { 1605 "id": 3216, 1606 "cityName": "沂南縣" 1607 }, 1608 { 1609 "id": 3309, 1610 "cityName": "延慶縣" 1611 }, 1612 { 1613 "id": 3505, 1614 "cityName": "颍上縣" 1615 }, 1616 { 1617 "id": 3519, 1618 "cityName": "陽谷縣" 1619 }, 1620 { 1621 "id": 3594, 1622 "cityName": "永康" 1623 }, 1624 { 1625 "id": 3615, 1626 "cityName": "沂水縣" 1627 }, 1628 { 1629 "id": 3629, 1630 "cityName": "弋陽縣" 1631 }, 1632 { 1633 "id": 3659, 1634 "cityName": "郓城縣" 1635 }, 1636 { 1637 "id": 3663, 1638 "cityName": "伊川縣" 1639 } 1640 ] 1641 }, 1642 { 1643 "title": "Z", 1644 "items": [ 1645 { 1646 "id": 1854, 1647 "cityName": "棗莊" 1648 }, 1649 { 1650 "id": 1207, 1651 "cityName": "張家界" 1652 }, 1653 { 1654 "id": 953, 1655 "cityName": "張家口" 1656 }, 1657 { 1658 "id": 1792, 1659 "cityName": "張掖" 1660 }, 1661 { 1662 "id": 585, 1663 "cityName": "鎮江" 1664 }, 1665 { 1666 "id": 490, 1667 "cityName": "鄭州" 1668 }, 1669 { 1670 "id": 706, 1671 "cityName": "中山" 1672 }, 1673 { 1674 "id": 3205, 1675 "cityName": "中衛" 1676 }, 1677 { 1678 "id": 2156, 1679 "cityName": "舟山" 1680 }, 1681 { 1682 "id": 2414, 1683 "cityName": "周口" 1684 }, 1685 { 1686 "id": 2058, 1687 "cityName": "珠海" 1688 }, 1689 { 1690 "id": 826, 1691 "cityName": "淄博" 1692 }, 1693 { 1694 "id": 3468, 1695 "cityName": "自貢" 1696 }, 1697 { 1698 "id": 2420, 1699 "cityName": "張北縣" 1700 }, 1701 { 1702 "id": 2691, 1703 "cityName": "諸暨" 1704 }, 1705 { 1706 "id": 2729, 1707 "cityName": "鄒城" 1708 }, 1709 { 1710 "id": 2743, 1711 "cityName": "張家港" 1712 }, 1713 { 1714 "id": 3641, 1715 "cityName": "諸城" 1716 } 1717 ] 1718 } 1719 ] 1720 } 1721 } city.json

  JsonData.java 序列化 Json 數據: (GsonFormat)

1 package com.dragon.android.cityselect.model; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 /** 7 * Created by Auser on 2016/10/10. 8 */ 9 10 public class JsonData { 11 12 13 /** 14 * result : 0 15 * message : 接口通信成功! 16 * responseData : {"History":[],"Recent":[],"Hot":[{"id":"226","cityName":"上海"},{"id":"447","cityName":"北京"},{"id":"221","cityName":"南京"},{"id":"220","cityName":"蘇州"},{"id":"261","cityName":"杭州"},{"id":"476","cityName":"天津"}],"Common":[{"title":"A","items":[{"id":664,"cityName":"安慶"},{"id":3590,"cityName":"安吉縣"}]},{"title":"B","items":[{"id":3612,"cityName":"巴州"},{"id":3366,"cityName":"白銀"},{"id":910,"cityName":"蚌埠"},{"id":2818,"cityName":"保定"},{"id":1068,"cityName":"北海"},{"id":447,"cityName":"北京"},{"id":882,"cityName":"濱州"},{"id":2119,"cityName":"亳州"},{"id":3449,"cityName":"博爾塔拉蒙古自治州"},{"id":2202,"cityName":"濱海縣"},{"id":2462,"cityName":"濱海新區大港"},{"id":2463,"cityName":"濱海新區塘沽"},{"id":2464,"cityName":"濱海新區漢沽"},{"id":2788,"cityName":"寶應縣"},{"id":3452,"cityName":"博樂"},{"id":3487,"cityName":"霸州"}]},{"title":"C","items":[{"id":1889,"cityName":"滄州"},{"id":2611,"cityName":"昌吉自治州"},{"id":2329,"cityName":"長春"},{"id":651,"cityName":"長沙"},{"id":2926,"cityName":"長治"},{"id":237,"cityName":"常州"},{"id":1959,"cityName":"朝陽"},{"id":3339,"cityName":"潮州"},{"id":279,"cityName":"成都"},{"id":3527,"cityName":"承德"},{"id":2095,"cityName":"池州"},{"id":2303,"cityName":"赤峰"},{"id":901,"cityName":"滁州"},{"id":621,"cityName":"重慶"},{"id":920,"cityName":"崇明縣"},{"id":2275,"cityName":"曹縣"},{"id":2620,"cityName":"慈溪"},{"id":2709,"cityName":"常熟"},{"id":2779,"cityName":"茌平縣"},{"id":3066,"cityName":"巢湖"},{"id":3135,"cityName":"長豐縣"},{"id":3630,"cityName":"昌黎縣"},{"id":3640,"cityName":"章丘"},{"id":3657,"cityName":"崇州"}]},{"title":"D","items":[{"id":3384,"cityName":"達卡(孟加拉國)"},{"id":1699,"cityName":"大連"},{"id":2700,"cityName":"大慶"},{"id":2290,"cityName":"大同"},{"id":579,"cityName":"德州"},{"id":3351,"cityName":"定州"},{"id":254,"cityName":"東莞"},{"id":1748,"cityName":"東營"},{"id":2208,"cityName":"德清縣"},{"id":2286,"cityName":"東海縣"},{"id":2608,"cityName":"定遠縣"},{"id":2715,"cityName":"東台"},{"id":3101,"cityName":"砀山縣"},{"id":3150,"cityName":"大廠縣"},{"id":3166,"cityName":"東鄉縣"},{"id":3281,"cityName":"丹陽"},{"id":3406,"cityName":"東明縣"},{"id":3618,"cityName":"大悟縣"},{"id":3643,"cityName":"敦煌"},{"id":3661,"cityName":"大名縣"}]},{"title":"F","items":[{"id":2176,"cityName":"佛山"},{"id":801,"cityName":"福州"},{"id":3165,"cityName":"撫州"},{"id":2881,"cityName":"阜陽"},{"id":2262,"cityName":"富錦"},{"id":2399,"cityName":"鳳陽縣"},{"id":3157,"cityName":"繁峙縣"},{"id":3162,"cityName":"豐縣"},{"id":3297,"cityName":"阜南縣"},{"id":3441,"cityName":"奉化"},{"id":3508,"cityName":"阜寧縣"},{"id":3509,"cityName":"肥東縣"},{"id":3541,"cityName":"肥西縣"},{"id":3544,"cityName":"鳳台縣"}]},{"title":"G","items":[{"id":2976,"cityName":"贛州"},{"id":3472,"cityName":"廣元"},{"id":1969,"cityName":"廣州"},{"id":709,"cityName":"貴陽"},{"id":898,"cityName":"桂林"},{"id":2002,"cityName":"高唐縣"},{"id":2129,"cityName":"灌南縣"},{"id":2224,"cityName":"固始縣"},{"id":2518,"cityName":"贛榆縣"},{"id":2734,"cityName":"高密"},{"id":2746,"cityName":"高郵"},{"id":2752,"cityName":"灌雲縣"},{"id":2819,"cityName":"高碑店"},{"id":3060,"cityName":"古交"},{"id":3385,"cityName":"孤山1"},{"id":3455,"cityName":"沽源縣"},{"id":3490,"cityName":"固鎮縣"},{"id":3502,"cityName":"貴溪"},{"id":3589,"cityName":"瓜州縣"},{"id":3614,"cityName":"廣德縣"},{"id":3619,"cityName":"古田縣"},{"id":3634,"cityName":"格爾木"},{"id":3699,"cityName":"高安"}]},{"title":"H","items":[{"id":558,"cityName":"哈爾濱"},{"id":2104,"cityName":"海口"},{"id":3633,"cityName":"海西蒙古族藏族自治州"},{"id":2233,"cityName":"邯鄲"},{"id":3652,"cityName":"韓國首爾"},{"id":2845,"cityName":"漢中"},{"id":261,"cityName":"杭州"},{"id":639,"cityName":"合肥"},{"id":895,"cityName":"菏澤"},{"id":1074,"cityName":"賀州"},{"id":3372,"cityName":"鶴壁"},{"id":3611,"cityName":"衡陽"},{"id":645,"cityName":"呼和浩特"},{"id":2205,"cityName":"湖州"},{"id":3397,"cityName":"葫蘆島"},{"id":594,"cityName":"淮安"},{"id":699,"cityName":"淮北"},{"id":1953,"cityName":"淮南"},{"id":3124,"cityName":"黃岡"},{"id":2451,"cityName":"黃山"},{"id":255,"cityName":"惠州"},{"id":1774,"cityName":"海安縣"},{"id":1840,"cityName":"海陽"},{"id":2347,"cityName":"海門"},{"id":2356,"cityName":"黃骅"},{"id":2878,"cityName":"霍山縣"},{"id":3125,"cityName":"紅安縣"},{"id":3266,"cityName":"洪澤縣"},{"id":3291,"cityName":"懷來縣"},{"id":3494,"cityName":"霍邱縣"}]},{"title":"J","items":[{"id":771,"cityName":"吉林"},{"id":470,"cityName":"濟南"},{"id":996,"cityName":"濟寧"},{"id":2230,"cityName":"佳木斯"},{"id":1536,"cityName":"嘉興"},{"id":1777,"cityName":"嘉峪關"},{"id":2242,"cityName":"焦作"},{"id":2893,"cityName":"揭陽"},{"id":654,"cityName":"金華"},{"id":2543,"cityName":"錦州"},{"id":2759,"cityName":"晉城"},{"id":2316,"cityName":"晉中"},{"id":2076,"cityName":"荊門"},{"id":2070,"cityName":"景德鎮"},{"id":702,"cityName":"九江"},{"id":1781,"cityName":"酒泉"},{"id":1723,"cityName":"薊縣"},{"id":2110,"cityName":"巨野縣"},{"id":2163,"cityName":"金鄉縣"},{"id":2496,"cityName":"靖江"},{"id":2584,"cityName":"句容"},{"id":2629,"cityName":"江陰"},{"id":2656,"cityName":"莒南縣"},{"id":2791,"cityName":"嘉祥縣"},{"id":2923,"cityName":"吉木薩爾縣"},{"id":3000,"cityName":"莒縣"},{"id":3034,"cityName":"膠州"},{"id":3213,"cityName":"金湖縣"},{"id":3219,"cityName":"建湖縣"},{"id":3256,"cityName":"鄄城縣"},{"id":3294,"cityName":"靜海縣"},{"id":3403,"cityName":"金壇"},{"id":3428,"cityName":"嘉峪關"},{"id":3539,"cityName":"金寨縣"},{"id":3548,"cityName":"泾縣"},{"id":3578,"cityName":"績溪縣"},{"id":3600,"cityName":"介休"},{"id":3677,"cityName":"嘉善縣"},{"id":3706,"cityName":"吉首"}]},{"title":"K","items":[{"id":907,"cityName":"開封"},{"id":1833,"cityName":"昆明"},{"id":2824,"cityName":"昆山"},{"id":3540,"cityName":"康保縣"},{"id":3613,"cityName":"庫爾勒"}]},{"title":"L","items":[{"id":3515,"cityName":"來賓"},{"id":618,"cityName":"萊蕪"},{"id":1545,"cityName":"蘭州"},{"id":993,"cityName":"廊坊"},{"id":2623,"cityName":"麗江"},{"id":3038,"cityName":"麗水"},{"id":762,"cityName":"連雲港"},{"id":3597,"cityName":"遼源"},{"id":1527,"cityName":"聊城"},{"id":1008,"cityName":"臨沂"},{"id":1067,"cityName":"柳州"},{"id":2721,"cityName":"六安"},{"id":2961,"cityName":"六盤水"},{"id":3553,"cityName":"隴南"},{"id":3312,"cityName":"呂梁"},{"id":267,"cityName":"洛陽"},{"id":2417,"cityName":"鹿邑縣"},{"id":2531,"cityName":"洛川縣"},{"id":2637,"cityName":"廬江縣"},{"id":2694,"cityName":"溧陽"},{"id":2712,"cityName":"漣水縣"},{"id":2902,"cityName":"樂亭縣"},{"id":3120,"cityName":"臨朐縣"},{"id":3202,"cityName":"蘭陵縣"},{"id":3324,"cityName":"臨海"},{"id":3409,"cityName":"萊陽"},{"id":3431,"cityName":"龍口"},{"id":3446,"cityName":"林西縣"},{"id":3517,"cityName":"郎溪縣"},{"id":3549,"cityName":"利辛縣"},{"id":3579,"cityName":"臨桂縣"},{"id":3582,"cityName":"靈璧縣"},{"id":3656,"cityName":"灤平縣"},{"id":3676,"cityName":"臨清"}]},{"title":"M","items":[{"id":271,"cityName":"馬鞍山"},{"id":3333,"cityName":"梅州"},{"id":3673,"cityName":"美國鳳凰城"},{"id":2402,"cityName":"密雲縣"},{"id":3390,"cityName":"蒙城縣"},{"id":3492,"cityName":"明光"},{"id":3675,"cityName":"美國鳳凰城"}]},{"title":"N","items":[{"id":276,"cityName":"南昌"},{"id":221,"cityName":"南京"},{"id":723,"cityName":"南寧"},{"id":238,"cityName":"南通"},{"id":678,"cityName":"寧波"},{"id":1135,"cityName":"寧德"},{"id":1826,"cityName":"南昌縣"},{"id":3682,"cityName":"寧國"}]},{"title":"P","items":[{"id":3510,"cityName":"平潭縣"},{"id":1130,"cityName":"莆田"},{"id":750,"cityName":"濮陽"},{"id":1839,"cityName":"蓬萊"},{"id":2377,"cityName":"邳州"},{"id":2386,"cityName":"平陰縣"},{"id":2685,"cityName":"平遙縣"},{"id":2962,"cityName":"盤縣"},{"id":3284,"cityName":"普寧"},{"id":3542,"cityName":"沛縣"},{"id":3655,"cityName":"郫縣"}]},{"title":"Q","items":[{"id":1070,"cityName":"欽州"},{"id":2211,"cityName":"秦皇島"},{"id":612,"cityName":"青島"},{"id":2599,"cityName":"衢州"},{"id":3466,"cityName":"泉州"},{"id":2296,"cityName":"啟東"},{"id":2408,"cityName":"青陽縣"},{"id":2612,"cityName":"奇台縣"},{"id":2861,"cityName":"青州"},{"id":3056,"cityName":"曲阜"},{"id":3069,"cityName":"遷安"},{"id":3488,"cityName":"齊河縣"},{"id":3501,"cityName":"全椒縣"},{"id":3623,"cityName":"青銅峽"},{"id":3662,"cityName":"沁陽"}]},{"title":"R","items":[{"id":934,"cityName":"日照"},{"id":873,"cityName":"如東縣"},{"id":2147,"cityName":"如皋"},{"id":2896,"cityName":"乳山"},{"id":3523,"cityName":"榮成"}]},{"title":"S","items":[{"id":1131,"cityName":"三明"},{"id":3093,"cityName":"三亞"},{"id":1569,"cityName":"汕頭"},{"id":2280,"cityName":"商丘"},{"id":226,"cityName":"上海"},{"id":981,"cityName":"上饒"},{"id":1866,"cityName":"紹興"},{"id":253,"cityName":"深圳"},{"id":273,"cityName":"沈陽"},{"id":3459,"cityName":"十堰"},{"id":746,"cityName":"石家莊"},{"id":220,"cityName":"蘇州"},{"id":732,"cityName":"宿遷"},{"id":1726,"cityName":"宿州"},{"id":2221,"cityName":"泗陽縣"},{"id":2335,"cityName":"濉溪縣"},{"id":2353,"cityName":"泗洪縣"},{"id":2561,"cityName":"壽光"},{"id":2697,"cityName":"沭陽縣"},{"id":2722,"cityName":"壽縣"},{"id":2749,"cityName":"射陽縣"},{"id":2932,"cityName":"睢寧縣"},{"id":3039,"cityName":"遂昌縣"},{"id":3171,"cityName":"沙河"},{"id":3186,"cityName":"三河"},{"id":3222,"cityName":"舒城縣"},{"id":3532,"cityName":"商城縣"},{"id":3642,"cityName":"雙流縣"},{"id":3644,"cityName":"泗縣"}]},{"title":"T","items":[{"id":600,"cityName":"台州"},{"id":736,"cityName":"太原"},{"id":265,"cityName":"泰安"},{"id":606,"cityName":"泰州"},{"id":684,"cityName":"唐山"},{"id":476,"cityName":"天津"},{"id":1778,"cityName":"天水"},{"id":3318,"cityName":"通遼"},{"id":885,"cityName":"銅陵"},{"id":2982,"cityName":"吐魯番地區"},{"id":1312,"cityName":"桐廬縣"},{"id":1696,"cityName":"桐鄉"},{"id":2362,"cityName":"泰興"},{"id":2659,"cityName":"太倉"},{"id":2718,"cityName":"天台縣"},{"id":2785,"cityName":"滕州"},{"id":2983,"cityName":"吐魯番"},{"id":3132,"cityName":"太和縣"},{"id":3495,"cityName":"太湖縣"},{"id":3530,"cityName":"桐城"}]},{"title":"W","items":[{"id":1563,"cityName":"威海"},{"id":756,"cityName":"濰坊"},{"id":768,"cityName":"溫州"},{"id":3300,"cityName":"烏蘭察布"},{"id":630,"cityName":"烏魯木齊"},{"id":222,"cityName":"無錫"},{"id":3259,"cityName":"吳忠"},{"id":1046,"cityName":"蕪湖"},{"id":421,"cityName":"武漢"},{"id":1779,"cityName":"武威"},{"id":985,"cityName":"婺源縣"},{"id":1051,"cityName":"蕪湖縣"},{"id":1965,"cityName":"汶上縣"},{"id":2914,"cityName":"五河縣"},{"id":2967,"cityName":"蔚縣"},{"id":3177,"cityName":"文登"},{"id":3313,"cityName":"文水縣"},{"id":3357,"cityName":"文安縣"},{"id":3360,"cityName":"渦陽縣"},{"id":3497,"cityName":"微山縣"},{"id":3686,"cityName":"無為縣"}]},{"title":"X","items":[{"id":719,"cityName":"西安"},{"id":2137,"cityName":"西寧"},{"id":802,"cityName":"廈門"},{"id":3555,"cityName":"鹹陽"},{"id":3705,"cityName":"湘西州"},{"id":3462,"cityName":"孝感"},{"id":3156,"cityName":"忻州"},{"id":2506,"cityName":"新鄉"},{"id":2189,"cityName":"新余"},{"id":1590,"cityName":"信陽"},{"id":2569,"cityName":"邢台"},{"id":816,"cityName":"徐州"},{"id":3078,"cityName":"宣城"},{"id":1720,"cityName":"盱眙縣"},{"id":1869,"cityName":"新昌縣"},{"id":1926,"cityName":"興化"},{"id":2390,"cityName":"霞浦縣"},{"id":2580,"cityName":"響水縣"},{"id":2650,"cityName":"新沂"},{"id":3253,"cityName":"蕭縣"},{"id":3398,"cityName":"興城"},{"id":3489,"cityName":"象山縣"},{"id":3512,"cityName":"仙居縣"},{"id":3536,"cityName":"歙縣"}]},{"title":"Y","items":[{"id":753,"cityName":"煙台"},{"id":2527,"cityName":"延安"},{"id":759,"cityName":"鹽城"},{"id":239,"cityName":"揚州"},{"id":2521,"cityName":"陽江"},{"id":2768,"cityName":"陽泉"},{"id":2341,"cityName":"宜昌"},{"id":3698,"cityName":"宜春"},{"id":1736,"cityName":"銀川"},{"id":2577,"cityName":"鷹潭"},{"id":1073,"cityName":"玉林"},{"id":1210,"cityName":"岳陽"},{"id":1766,"cityName":"運城"},{"id":1912,"cityName":"玉山縣"},{"id":2471,"cityName":"魚台縣"},{"id":2553,"cityName":"儀征"},{"id":2647,"cityName":"永城"},{"id":2662,"cityName":"義烏"},{"id":2869,"cityName":"宜興"},{"id":2917,"cityName":"余姚"},{"id":3110,"cityName":"揚中"},{"id":3216,"cityName":"沂南縣"},{"id":3309,"cityName":"延慶縣"},{"id":3505,"cityName":"颍上縣"},{"id":3519,"cityName":"陽谷縣"},{"id":3594,"cityName":"永康"},{"id":3615,"cityName":"沂水縣"},{"id":3629,"cityName":"弋陽縣"},{"id":3659,"cityName":"郓城縣"},{"id":3663,"cityName":"伊川縣"}]},{"title":"Z","items":[{"id":1854,"cityName":"棗莊"},{"id":1207,"cityName":"張家界"},{"id":953,"cityName":"張家口"},{"id":1792,"cityName":"張掖"},{"id":585,"cityName":"鎮江"},{"id":490,"cityName":"鄭州"},{"id":706,"cityName":"中山"},{"id":3205,"cityName":"中衛"},{"id":2156,"cityName":"舟山"},{"id":2414,"cityName":"周口"},{"id":2058,"cityName":"珠海"},{"id":826,"cityName":"淄博"},{"id":3468,"cityName":"自貢"},{"id":2420,"cityName":"張北縣"},{"id":2691,"cityName":"諸暨"},{"id":2729,"cityName":"鄒城"},{"id":2743,"cityName":"張家港"},{"id":3641,"cityName":"諸城"}]}]} 17 */ 18 19 private String result; 20 private String message; 21 private ResponseDataBean responseData; 22 23 public String getResult() { 24 return result; 25 } 26 27 public void setResult(String result) { 28 this.result = result; 29 } 30 31 public String getMessage() { 32 return message; 33 } 34 35 public void setMessage(String message) { 36 this.message = message; 37 } 38 39 public ResponseDataBean getResponseData() { 40 return responseData; 41 } 42 43 public void setResponseData(ResponseDataBean responseData) { 44 this.responseData = responseData; 45 } 46 47 public static class ResponseDataBean { 48 private List<?> History = new ArrayList<>(); 49 private List<?> Recent = new ArrayList<>(); 50 /** 51 * id : 226 52 * cityName : 上海 53 */ 54 55 private List<HotBean> Hot = new ArrayList<>(); 56 /** 57 * title : A 58 * items : [{"id":664,"cityName":"安慶"},{"id":3590,"cityName":"安吉縣"}] 59 */ 60 61 private List<CommonBean> Common = new ArrayList<>(); 62 63 public List<?> getHistory() { 64 return History; 65 } 66 67 public void setHistory(List<?> History) { 68 this.History = History; 69 } 70 71 public List<?> getRecent() { 72 return Recent; 73 } 74 75 public void setRecent(List<?> Recent) { 76 this.Recent = Recent; 77 } 78 79 public List<HotBean> getHot() { 80 return Hot; 81 } 82 83 public void setHot(List<HotBean> Hot) { 84 this.Hot = Hot; 85 } 86 87 public List<CommonBean> getCommon() { 88 return Common; 89 } 90 91 public void setCommon(List<CommonBean> Common) { 92 this.Common = Common; 93 } 94 95 public static class HotBean { 96 private String id; 97 private String cityName; 98 99 public String getId() { 100 return id; 101 } 102 103 public void setId(String id) { 104 this.id = id; 105 } 106 107 public String getCityName() { 108 return cityName; 109 } 110 111 public void setCityName(String cityName) { 112 this.cityName = cityName; 113 } 114 } 115 116 public static class CommonBean { 117 private String title; 118 /** 119 * id : 664 120 * cityName : 安慶 121 */ 122 123 private List<ItemsBean> items = new ArrayList<>(); 124 125 public String getTitle() { 126 return title; 127 } 128 129 public void setTitle(String title) { 130 this.title = title; 131 } 132 133 public List<ItemsBean> getItems() { 134 return items; 135 } 136 137 public void setItems(List<ItemsBean> items) { 138 this.items = items; 139 } 140 141 public static class ItemsBean { 142 private int id; 143 private String cityName; 144 145 public int getId() { 146 return id; 147 } 148 149 public void setId(int id) { 150 this.id = id; 151 } 152 153 public String getCityName() { 154 return cityName; 155 } 156 157 public void setCityName(String cityName) { 158 this.cityName = cityName; 159 } 160 } 161 } 162 } 163 } JsonData

 

--> 接下來, 先寫布局, 可以看到第一張圖片, 垂直的四個布局樣式一樣, 這裡准備用組合控件的形式實現.

  單條布局 item_linear_layout.xml:

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 android:layout_width="match_parent" 5 android:layout_height="wrap_content" 6 android:background="?attr/actionBarItemBackground" 7 android:orientation="vertical"> 8 9 <View 10 android:id="@+id/view_shape1" 11 android:layout_width="match_parent" 12 android:layout_height="1dp" 13 android:background="#ccc" 14 android:visibility="invisible" /> 15 16 <LinearLayout 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:gravity="center_vertical" 20 android:orientation="horizontal" 21 android:padding="16dp"> 22 23 <ImageView 24 android:id="@+id/iv_icon" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 app:srcCompat="@mipmap/img_city" /> 28 29 <TextView 30 android:id="@+id/tv_title" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:layout_weight="1" 34 android:gravity="center_vertical" 35 android:paddingLeft="16dp" 36 android:text="入住城市" /> 37 38 <TextView 39 android:id="@+id/tv_title_c" 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" 42 android:text="入住城市" 43 android:textColor="#007F6E" /> 44 45 <ImageView 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content" 48 app:srcCompat="@mipmap/back_black_normal" /> 49 50 </LinearLayout> 51 52 <View 53 android:id="@+id/view_shape2" 54 android:layout_width="match_parent" 55 android:layout_height="1dp" 56 android:background="#ccc" 57 android:visibility="invisible" /> 58 59 </LinearLayout> item_linear_layout

    自定義組合控件 DefineLinearLayout.java:

1 package com.dragon.android.cityselect.view; 2 3 import android.content.Context; 4 import android.util.AttributeSet; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.widget.ImageView; 8 import android.widget.LinearLayout; 9 import android.widget.TextView; 10 11 import com.dragon.android.cityselect.R; 12 13 /** 14 * Created by Auser on 2016/10/10. 15 */ 16 17 public class DefineLinearLayout extends LinearLayout { 18 19 private View view_shape1; 20 private View view_shape2; 21 private ImageView iv_icon; 22 private TextView tv_title; 23 private TextView tv_title_c; 24 private View layout; 25 26 public DefineLinearLayout(Context context) { 27 super(context); 28 } 29 30 public DefineLinearLayout(Context context, AttributeSet attrs) { 31 super(context, attrs); 32 initView(context, attrs); 33 } 34 35 private void initView(Context context, AttributeSet attrs) { 36 37 // 加載布局 38 layout = LayoutInflater.from(context).inflate(R.layout.item_linear_layout, this); 39 40 view_shape1 = layout.findViewById(R.id.view_shape1); 41 view_shape2 = layout.findViewById(R.id.view_shape2); 42 iv_icon = (ImageView) layout.findViewById(R.id.iv_icon); 43 tv_title = (TextView) layout.findViewById(R.id.tv_title); 44 tv_title_c = (TextView) layout.findViewById(R.id.tv_title_c); 45 } 46 47 // 這裡沒有用自定義屬性的方式修改數據 48 public void changeData(Boolean isChecked, int imgRes, String title, String title_c) { 49 view_shape1.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE); 50 view_shape2.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE); 51 iv_icon.setImageResource(imgRes); 52 tv_title.setText(title); 53 tv_title_c.setText(title_c); 54 } 55 56 public void changeData(Boolean isChecked) { 57 view_shape1.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE); 58 view_shape2.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE); 59 } 60 61 public void changeData(String title_c) { 62 tv_title_c.setText(title_c); 63 } 64 } DefineLinearLayout

  然後在主布局 activity_main.xml 中寫首頁布局: (圓角)

1 <?xml version="1.0" encoding="utf-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android"> 3 4 <corners android:radius="15dp"/> 5 <solid android:color="#fff"/> 6 7 </shape> bg_shape_linear 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:id="@+id/activity_main" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:background="#ddd" 8 android:orientation="vertical" 9 android:padding="16dp" 10 tools:context="com.dragon.android.cityselect.MainActivity"> 11 12 <LinearLayout 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:background="@drawable/bg_shape_linear" 16 android:orientation="vertical"> 17 18 <com.dragon.android.cityselect.view.DefineLinearLayout 19 android:id="@+id/dl_city" 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:background="?attr/actionBarItemBackground" 23 android:clickable="true" 24 android:onClick="click" /> 25 26 <com.dragon.android.cityselect.view.DefineLinearLayout 27 android:id="@+id/dl_key" 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" 30 android:background="?attr/actionBarItemBackground" 31 android:clickable="true" 32 android:onClick="click" /> 33 34 <com.dragon.android.cityselect.view.DefineLinearLayout 35 android:id="@+id/dl_time" 36 android:layout_width="match_parent" 37 android:layout_height="wrap_content" 38 android:background="?attr/actionBarItemBackground" 39 android:clickable="true" 40 android:onClick="click" /> 41 42 <com.dragon.android.cityselect.view.DefineLinearLayout 43 android:id="@+id/dl_day" 44 android:layout_width="match_parent" 45 android:layout_height="wrap_content" 46 android:background="?attr/actionBarItemBackground" 47 android:clickable="true" 48 android:onClick="click" /> 49 50 </LinearLayout> 51 52 </LinearLayout> activity_main

 

--> 第三步, 寫出城市選擇的布局. 

  首先是右側城市首字母選擇的自定義 View (也可以使用自定義 ListView 實現) SlideBar.java:

1 package com.dragon.android.cityselect.view; 2 3 import android.content.Context; 4 import android.graphics.Canvas; 5 import android.graphics.Color; 6 import android.graphics.Paint; 7 import android.graphics.Typeface; 8 import android.util.AttributeSet; 9 import android.view.MotionEvent; 10 import android.view.View; 11 12 import com.dragon.android.cityselect.R; 13 14 import java.util.ArrayList; 15 16 /** 17 * Created by Jerry on 16/10/11. 18 */ 19 20 public class SlideBar extends View { 21 22 private ArrayList<String> titles; // 首字母的集合(不一定為26個) 23 private int position = -1; // 被選中的字符的位置 24 private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); // 畫筆 25 26 public SlideBar(Context context) { 27 super(context); 28 } 29 30 public SlideBar(Context context, AttributeSet attrs) { 31 super(context, attrs); 32 } 33 34 public void getTitles(ArrayList<String> titles){ 35 this.titles = titles; 36 } 37 38 @Override 39 protected void onDraw(Canvas canvas) { 40 super.onDraw(canvas); 41 42 // 得到右側寬高 43 int width = getWidth(); 44 int height = getHeight() - 16; 45 46 int codeHeight = height / titles.size(); // 計算出每一個字母占有的高度 47 48 for (int i = 0; i < titles.size(); i++) { 49 mPaint.setStyle(Paint.Style.FILL_AND_STROKE); 50 mPaint.setTypeface(Typeface.DEFAULT_BOLD); 51 mPaint.setColor(Color.parseColor("#666666")); 52 mPaint.setTextSize(20); 53 54 // 給選中的 字母 不同的顏色 55 if (position != -1 && position == i) { 56 mPaint.setColor(Color.parseColor("#3399ff")); 57 } 58 float xpos = (width - mPaint.measureText(titles.get(i))) / 2; 59 float ypos = (i + 1) * codeHeight; 60 // 畫出字母 61 canvas.drawText(titles.get(i), xpos, ypos, mPaint); 62 } 63 } 64 65 /** 66 * 處理觸碰事件 67 * 1. 當選擇一個字符時, 顯示中間的提示 68 * 2. 當手勢抬起後, 中間的提示取消 69 */ 70 @Override 71 public boolean onTouchEvent(MotionEvent event) { 72 int action = event.getAction(); 73 switch (action) { 74 case MotionEvent.ACTION_DOWN: 75 case MotionEvent.ACTION_MOVE: 76 // 右側選擇欄的樣式 77 setBackgroundResource(R.drawable.shape_slide_bar); 78 79 float ypos = event.getY(); // 先獲取是哪一個被按下 80 position = (int) (ypos / getHeight() * titles.size());// 選中的字符的位置 81 82 if (position >= 0 && position < titles.size()) { 83 if (listener != null) { 84 // 通知 activity 選中的位置 85 listener.onCharClick(titles.get(position)); 86 } 87 } 88 invalidate(); // 重繪 89 return true; 90 case MotionEvent.ACTION_UP: 91 position = -1; // 重置位置 92 setBackgroundResource(android.R.color.transparent); 93 if (listener != null) { 94 listener.onClickUp(); 95 } 96 break; 97 } 98 return super.onTouchEvent(event); 99 } 100 101 // 定義一個回調接口 102 public interface OnSlideBarCharSelectListener { 103 void onCharClick(String selectChar); 104 void onClickUp(); 105 } 106 107 private OnSlideBarCharSelectListener listener; 108 109 public void setOnSlideBarCharSelectListener(OnSlideBarCharSelectListener listener) { 110 this.listener = listener; 111 } 112 } SlideBar

  然後是城市選擇的自定義 ListView (重點) CitySelect.java:

1 package com.dragon.android.cityselect.view; 2 3 import android.content.Context; 4 import android.content.res.AssetManager; 5 import android.util.AttributeSet; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.BaseAdapter; 10 import android.widget.ListView; 11 import android.widget.TextView; 12 13 import com.dragon.android.cityselect.model.JsonData; 14 import com.google.gson.Gson; 15 16 import java.io.BufferedReader; 17 import java.io.IOException; 18 import java.io.InputStream; 19 import java.io.InputStreamReader; 20 import java.util.ArrayList; 21 import java.util.List; 22 23 import static android.R.id.text1; 24 25 /** 26 * Created by Auser on 2016/10/10. 27 */ 28 29 public class CitySelect extends ListView { 30 31 public CitySelect(Context context, AttributeSet attrs) { 32 super(context, attrs); 33 getJsonData(context); 34 35 MyAdapter myAdapter = new MyAdapter(context); 36 this.setAdapter(myAdapter); 37 } 38 39 private JsonData jsonData; 40 41 public JsonData getJsonData(Context context) { 42 // 加載 assets 中的文件, 並得到數據 43 AssetManager assetManager = context.getAssets(); 44 try { 45 InputStream is = assetManager.open("city.json"); 46 BufferedReader br = new BufferedReader(new InputStreamReader(is)); 47 StringBuffer stringBuffer = new StringBuffer(); 48 String str = null; 49 while ((str = br.readLine()) != null) { 50 stringBuffer.append(str); 51 } 52 // GsonFormat 53 jsonData = new Gson().fromJson(stringBuffer.toString(), JsonData.class); 54 } catch (IOException e) { 55 e.printStackTrace(); 56 } 57 return jsonData; 58 } 59 60 public class MyAdapter extends BaseAdapter { 61 62 private List<JsonData.ResponseDataBean.CommonBean> common; 63 private List<JsonData.ResponseDataBean.CommonBean.ItemsBean> items; 64 private Context context; 65 66 public MyAdapter(Context context) { 67 this.context = context; 68 } 69 70 // 一次性得到所有城市個數 (可以用下面的 getData() 方法) 71 @Override 72 public int getCount() { 73 common = jsonData.getResponseData().getCommon(); 74 int sum = 0; 75 for (int i = 0; i < common.size(); i++) { 76 items = common.get(i).getItems(); 77 sum += items.size(); 78 } 79 return jsonData == null ? 0 : sum; 80 } 81 82 @Override 83 public JsonData.ResponseDataBean getItem(int position) { 84 return jsonData.getResponseData(); 85 } 86 87 @Override 88 public long getItemId(int position) { 89 return position; 90 } 91 92 @Override 93 public View getView(int position, View convertView, ViewGroup parent) { 94 JsonData.ResponseDataBean item = getItem(position); 95 ViewHolder viewHolder; 96 if (convertView == null) { 97 viewHolder = new ViewHolder(); 98 // 直接使用 系統布局 99 convertView = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, null); 100 101 viewHolder.tv_city = (TextView) convertView.findViewById(text1); 102 convertView.setTag(viewHolder); 103 } else { 104 viewHolder = (ViewHolder) convertView.getTag(); 105 } 106 // 修改數據 (得到城市內容) 107 viewHolder.tv_city.setText(getData().get(position)); 108 109 return convertView; 110 } 111 112 // 加載所有城市的內容 113 public ArrayList<String> getData() { 114 ArrayList<String> datas = new ArrayList<>(); 115 for (int i = 0; i < common.size(); i++) { 116 for (int j = 0; j < common.get(i).getItems().size(); j++) { 117 datas.add(common.get(i).getItems().get(j).getCityName()); 118 } 119 } 120 return datas; 121 } 122 } 123 124 class ViewHolder { 125 TextView tv_city; 126 } 127 } CitySelect

  最後是城市選擇界面的布局: (EditText的樣式, 右側選擇欄的樣式, 中間的提示的樣式)

1 <?xml version="1.0" encoding="utf-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android"> 3 4 <stroke 5 android:width="1dp" 6 android:color="#A3D154" /> 7 <corners android:radius="15dp" /> 8 <solid android:color="#fff" /> 9 10 </shape> bg_edit_text 1 <?xml version="1.0" encoding="utf-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android"> 3 <corners 4 android:bottomLeftRadius="10dp" 5 android:topLeftRadius="10dp"/> 6 <solid android:color="#50ff0000"/> 7 </shape> shape_slide_bar 1 <?xml version="1.0" encoding="utf-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android"> 3 4 <corners android:radius="15dp"/> 5 <solid android:color="#A0CE51"/> 6 <stroke android:width="10px" android:color="#5aaa"/> 7 8 </shape> bg_shape_text_view 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:id="@+id/activity_city" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:focusable="true" 8 android:focusableInTouchMode="true" 9 tools:context="com.dragon.android.cityselect.CityActivity"> 10 11 <TextView 12 android:id="@+id/tv_title" 13 android:layout_width="match_parent" 14 android:layout_height="?attr/actionBarSize" 15 android:background="#ECECEC" 16 android:gravity="center" 17 android:text="全部城市" /> 18 19 <ImageView 20 android:id="@+id/iv_back" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:layout_marginLeft="14dp" 24 android:layout_marginTop="14dp" 25 android:background="?attr/actionBarItemBackground" 26 android:clickable="true" 27 android:src="@mipmap/back_black_normal1" /> 28 29 <View 30 android:id="@+id/vvv" 31 android:layout_width="match_parent" 32 android:layout_height="40dp" 33 android:layout_alignParentLeft="true" 34 android:layout_below="@+id/tv_title" 35 android:background="#E0E0E0" /> 36 37 <EditText 38 android:id="@+id/et_search" 39 android:layout_width="match_parent" 40 android:layout_height="30dp" 41 android:layout_alignParentLeft="true" 42 android:layout_below="@+id/tv_title" 43 android:layout_marginTop="10dp" 44 android:background="@drawable/bg_edit_text" 45 android:drawableLeft="@mipmap/add_qiuyou_ic_search_dark" 46 android:drawablePadding="10dp" 47 android:hint="請輸入關鍵字" 48 android:paddingLeft="10dp" /> 49 50 <TextView 51 android:id="@+id/tv_szm" 52 android:layout_width="match_parent" 53 android:layout_height="30dp" 54 android:layout_alignParentLeft="true" 55 android:layout_alignParentStart="true" 56 android:layout_below="@+id/vvv" 57 android:background="#E0E0E0" 58 android:gravity="center_vertical" 59 android:paddingLeft="15dp" 60 android:text="A" 61 android:textSize="16sp" /> 62 63 <com.dragon.android.cityselect.view.CitySelect 64 android:id="@+id/lv_show" 65 android:layout_width="match_parent" 66 android:layout_height="match_parent" 67 android:layout_alignParentLeft="true" 68 android:layout_alignParentStart="true" 69 android:layout_below="@+id/tv_szm" /> 70 71 <com.dragon.android.cityselect.view.SlideBar 72 android:id="@+id/lv_szm" 73 android:layout_width="30dp" 74 android:layout_height="match_parent" 75 android:layout_alignParentEnd="true" 76 android:layout_alignParentRight="true" 77 android:layout_below="@+id/vvv" 78 android:layout_marginTop="30dp" 79 android:divider="#0000" 80 android:dividerHeight="0dp" /> 81 82 <TextView 83 android:id="@+id/tv_show_szm" 84 android:layout_width="80dp" 85 android:layout_height="80dp" 86 android:layout_centerInParent="true" 87 android:background="@drawable/bg_shape_text_view" 88 android:gravity="center" 89 android:text="A" 90 android:textColor="#fff" 91 android:textSize="30sp" 92 android:visibility="gone" /> 93 94 </RelativeLayout> activity_city

 

--> 最後, Java 代碼的實現:

  CityActivity.java 實現城市選擇功能: (工具類 FirstLetter.java 根據漢字得到首字母並轉換大寫)

1 package com.dragon.android.cityselect.util; 2 3 import android.support.annotation.Nullable; 4 5 import java.io.UnsupportedEncodingException; 6 7 /** 8 * Created by Auser on 2016/10/11. 9 */ 10 11 public class FirstLetter { 12 13 private static final int GB_SP_DIFF = 160; 14 // 存放國標一級漢字不同讀音的起始區位碼 15 private static final int[] secPosValueList = {1601, 1637, 1833, 2078, 2274, 2302, 16 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858, 4027, 17 4086, 4390, 4558, 4684, 4925, 5249, 5600}; 18 // 存放國標一級漢字不同讀音的起始區位碼對應讀音 19 private static final char[] firstLetter = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 20 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'w', 'x', 21 'y', 'z'}; 22 23 @Nullable 24 public static String getFirstLetter(char ch) { 25 26 byte[] uniCode = null; 27 try { 28 uniCode = String.valueOf(ch).getBytes("GBK"); 29 } catch (UnsupportedEncodingException e) { 30 e.printStackTrace(); 31 return null; 32 } 33 if (uniCode[0] < 128 && uniCode[0] > 0) { 34 return null; 35 } else { 36 return convert(uniCode); 37 } 38 } 39 40 private static String convert(byte[] bytes) { 41 char result = '-'; 42 int secPosValue = 0; 43 int i; 44 for (i = 0; i < bytes.length; i++) { 45 bytes[i] -= GB_SP_DIFF; 46 } 47 secPosValue = bytes[0] * 100 + bytes[1]; 48 for (i = 0; i < 23; i++) { 49 if (secPosValue >= secPosValueList[i] 50 && secPosValue < secPosValueList[i + 1]) { 51 result = firstLetter[i]; 52 break; 53 } 54 } 55 String string = String.valueOf(result); 56 string = string.toUpperCase(); 57 return string; 58 } 59 } FirstLetter 1 package com.dragon.android.cityselect; 2 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.support.v7.app.AppCompatActivity; 6 import android.view.View; 7 import android.widget.AbsListView; 8 import android.widget.AdapterView; 9 import android.widget.TextView; 10 11 import com.dragon.android.cityselect.model.JsonData; 12 import com.dragon.android.cityselect.util.FirstLetter; 13 import com.dragon.android.cityselect.view.CitySelect; 14 import com.dragon.android.cityselect.view.SlideBar; 15 16 import java.util.ArrayList; 17 import java.util.List; 18 19 public class CityActivity extends AppCompatActivity { 20 21 private CitySelect lv_show; 22 private CitySelect.MyAdapter myAdapter; 23 private TextView tv_szm; 24 private TextView tv_show_szm; 25 private SlideBar lv_szm; 26 27 private View lastClickedView; 28 List<JsonData.ResponseDataBean.CommonBean> common; 29 private List<JsonData.ResponseDataBean.CommonBean.ItemsBean> items; 30 private String theText = "A"; 31 32 @Override 33 protected void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 setContentView(R.layout.activity_city); 36 getSupportActionBar().hide(); 37 38 lv_show = (CitySelect) findViewById(R.id.lv_show); 39 tv_szm = ((TextView) findViewById(R.id.tv_szm)); 40 tv_show_szm = ((TextView) findViewById(R.id.tv_show_szm)); 41 42 lv_show.setOnItemClickListener(new AdapterView.OnItemClickListener() { 43 @Override 44 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 45 // MainActivity 的startAcitivityForResult 46 Intent intent = getIntent(); 47 TextView text1 = (TextView) view.findViewById(android.R.id.text1); 48 intent.putExtra("city", text1.getText()); 49 setResult(RESULT_OK, intent); 50 finish(); 51 } 52 }); 53 lv_show.setOnScrollListener(new AbsListView.OnScrollListener() { 54 @Override 55 public void onScrollStateChanged(AbsListView view, int scrollState) { 56 } 57 58 @Override 59 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 60 // TextView childAt = (TextView) lv_show.getChildAt(firstVisibleItem); 61 // if (childAt != null) { 62 // 通過城市名得到 首字母並轉換大寫 63 theText = FirstLetter.getFirstLetter(((CitySelect.MyAdapter)lv_show.getAdapter()).getData().get(firstVisibleItem).toCharArray()[0]); 64 tv_szm.setText(theText); 65 // } 66 } 67 }); 68 69 common = lv_show.getJsonData(this).getResponseData().getCommon(); 70 ArrayList<String> titles = new ArrayList<>(); 71 for (int i = 0; i < common.size(); i++) { 72 titles.add(common.get(i).getTitle()); 73 } 74 lv_szm = (SlideBar) findViewById(R.id.lv_szm); 75 lv_szm.getTitles(titles); 76 // 接口回調的實現 77 lv_szm.setOnSlideBarCharSelectListener(new SlideBar.OnSlideBarCharSelectListener() { 78 79 @Override 80 public void onCharClick(String selectChar) { 81 tv_show_szm.setText(selectChar); 82 tv_show_szm.setVisibility(View.VISIBLE); 83 84 tv_szm.setText(selectChar); 85 int commonPosition = 0; 86 for (int i = 0; i < common.size(); i++) { 87 if (common.get(i).getTitle().equals(selectChar)) { 88 commonPosition = i; 89 } 90 } 91 int position = 0; 92 for (int j = 0; j < commonPosition; j++) { 93 items = common.get(j).getItems(); 94 position += items.size(); 95 } 96 // lv_show.smoothScrollToPosition(position); // 帶動畫 顯示 某項 - notifyDataSetChanged() 對其沒影響 97 lv_show.setSelection(position); // 跳轉並使某項為 第一項 - notifyDataSetChanged() 則無效 98 } 99 100 @Override 101 public void onClickUp() { 102 tv_show_szm.setVisibility(View.GONE); 103 } 104 }); 105 // ArrayAdapter arrayAdapter = new ArrayAdapter<>(this, R.layout.item_szm, titles); 106 // lv_szm.setAdapter(arrayAdapter); 107 // lv_szm.setOnItemClickListener(new AdapterView.OnItemClickListener() { 108 // @Override 109 // public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 110 // if (lastClickedView == null) { 111 // lastClickedView = view; 112 // } 113 // if (lastClickedView == view) { 114 // } else { 115 // ((TextView) lastClickedView).setTextColor(Color.parseColor("#666666")); 116 // } 117 // ((TextView) view).setTextColor(Color.WHITE); 118 // lastClickedView = view; 119 // } 120 // }); 121 122 findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() { 123 @Override 124 public void onClick(View v) { 125 finish(); 126 } 127 }); 128 } 129 } CityActivity

  MainAcitivity.java 實現其余功能: (包含一個DataPicker - 外部包:

    compile 'com.github.flavienlaurent.datetimepicker:library:0.0.2'

  )

  1 package com.dragon.android.cityselect;
  2 
  3 import android.content.Intent;
  4 import android.os.Bundle;
  5 import android.os.Handler;
  6 import android.os.Message;
  7 import android.support.v7.app.AppCompatActivity;
  8 import android.view.View;
  9 import android.widget.TextView;
 10 
 11 import com.dragon.android.cityselect.view.DefineLinearLayout;
 12 import com.fourmob.datetimepicker.date.DatePickerDialog;
 13 import com.sleepbot.datetimepicker.time.RadialPickerLayout;
 14 import com.sleepbot.datetimepicker.time.TimePickerDialog;
 15 
 16 import java.util.Calendar;
 17 
 18 public class MainActivity extends AppCompatActivity {
 19 
 20     private DefineLinearLayout dl_city;
 21     private DefineLinearLayout dl_key;
 22     private DefineLinearLayout dl_time;
 23     private DefineLinearLayout dl_day;
 24     public static final int REQUEST_CODE = 0x000;
 25 
 26     private int[] imgs = {R.mipmap.img_city, R.mipmap.img_key_words, R.mipmap.imag_time, R.mipmap.img_days};
 27     private String[] titles = {"入住城市", "關鍵字", "入住時間", "入住天數"};
 28     private DefineLinearLayout[] dlls;
 29 
 30     private Bundle mSaveInstanceState;
 31     private Handler handler;
 32 
 33     @Override
 34     protected void onCreate(Bundle savedInstanceState) {
 35         super.onCreate(savedInstanceState);
 36         setContentView(R.layout.activity_main);
 37 
 38         initView();
 39         mSaveInstanceState = savedInstanceState;
 40     }
 41 
 42     private void initView() {
 43         dl_city = (DefineLinearLayout) findViewById(R.id.dl_city);
 44         dl_key = (DefineLinearLayout) findViewById(R.id.dl_key);
 45         dl_time = (DefineLinearLayout) findViewById(R.id.dl_time);
 46         dl_day = (DefineLinearLayout) findViewById(R.id.dl_day);
 47 
 48         dlls = new DefineLinearLayout[]{dl_city, dl_key, dl_time, dl_day};
 49         for (int i = 0; i < dlls.length; i++) {
 50             changeDatas(dlls[i], false, imgs[i], titles[i], titles[i]);
 51         }
 52         getDataPicker();
 53     }
 54 
 55     // 修改初始數據
 56     private void changeDatas(DefineLinearLayout layout, Boolean isChecked, int imgRes, String title, String title_c) {
 57         layout.changeData(isChecked, imgRes, title, title_c);
 58     }
 59 
 60     private void changeDatas(DefineLinearLayout layout, Boolean isChecked) {
 61         layout.changeData(isChecked);
 62     }
 63 
 64     private void changeData(DefineLinearLayout layout, String title_c) {
 65         layout.changeData(title_c);
 66     }
 67 
 68     private DefineLinearLayout lastLayout;
 69 
 70     // 點擊事件
 71     public void click(View v) {
 72         if (lastLayout == null) {
 73             lastLayout = (DefineLinearLayout) v;
 74         }
 75         if (v == lastLayout) {
 76         } else {
 77             changeDatas(lastLayout, false);
 78         }
 79         changeDatas((DefineLinearLayout) v, true);
 80         lastLayout = (DefineLinearLayout) v;
 81         switch (v.getId()) {
 82             case R.id.dl_city:
 83                 // 城市選擇
 84                 startActivityForResult(new Intent(this, CityActivity.class), REQUEST_CODE);
 85                 break;
 86             case R.id.dl_key:
 87                 break;
 88             case R.id.dl_time:
 89                 new DataPicker().showDataPicker(mSaveInstanceState);
 90                 isFirstTime = true; // 點擊入住時間
 91                 break;
 92             case R.id.dl_day:
 93                 new DataPicker().showDataPicker(mSaveInstanceState);
 94                 isFirstTime = false; // 點擊入住天數
 95                 break;
 96         }
 97     }
 98 
 99     @Override
100     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
101         super.onActivityResult(requestCode, resultCode, data);
102         if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
103             String city = data.getStringExtra("city");
104             changeData(dl_city, city);
105         }
106     }
107 
108     private void getDataPicker() {
109         handler = new Handler(new Handler.Callback() {
110             @Override
111             public boolean handleMessage(Message msg) {
112                 switch (msg.what) {
113                     case MSG_WHAT:
114                         if (isFirstTime) {
115                             ((TextView) dl_time.findViewById(R.id.tv_title_c)).setText(msg.obj.toString());
116                         } else {
117                             ((TextView) dl_day.findViewById(R.id.tv_title_c)).setText(msg.obj.toString());
118                         }
119                         break;
120                 }
121                 return false;
122             }
123         });
124     }
125 
126     private boolean isFirstTime = false; // 是否點擊入住時間 (否則為 入住天數)
127     private static final int MSG_WHAT = 0x00;
128 
129     // DataPicker
130     class DataPicker implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {
131 
132         public static final String DATEPICKER_TAG = "datepicker";
133         public static final String TIMEPICKER_TAG = "timepicker";
134         private Calendar calendar;
135         private String mData;
136 
137         private void showDataPicker(Bundle savedInstanceState) {
138             calendar = Calendar.getInstance();
139 
140             DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), false);
141 
142             datePickerDialog.setVibrate(false);
143             datePickerDialog.setYearRange(1985, 2028);
144             datePickerDialog.setCloseOnSingleTapDay(false);
145             datePickerDialog.show(getSupportFragmentManager(), DATEPICKER_TAG);
146 
147             if (savedInstanceState != null) {
148                 DatePickerDialog dpd = (DatePickerDialog) getSupportFragmentManager().findFragmentByTag(DATEPICKER_TAG);
149                 if (dpd != null) {
150                     dpd.setOnDateSetListener(this);
151                 }
152 
153                 TimePickerDialog tpd = (TimePickerDialog) getSupportFragmentManager().findFragmentByTag(TIMEPICKER_TAG);
154                 if (tpd != null) {
155                     tpd.setOnTimeSetListener(this);
156                 }
157             }
158         }
159 
160         @Override
161         public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
162 //            Toast.makeText(MainActivity.this, "new date:" + year + "-" + month + "-" + day, Toast.LENGTH_LONG).show();
163 //            TimePickerDialog timePickerDialog = TimePickerDialog.newInstance(this, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), false, false);
164 //            timePickerDialog.setVibrate(false);
165 //            timePickerDialog.setCloseOnSingleTapMinute(false);
166 //            timePickerDialog.show(getSupportFragmentManager(), TIMEPICKER_TAG);
167             Message msg = Message.obtain();
168             msg.obj = year + "-" + month + "-" + day;
169             msg.what = MSG_WHAT;
170             handler.sendMessage(msg);
171         }
172 
173         @Override
174         public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
175 //            Toast.makeText(MainActivity.this, "new time:" + hourOfDay + "-" + minute, Toast.LENGTH_LONG).show();
176         }
177     }
178 }

 

注意?> 要在 Manifest 中定義鍵盤覆蓋, 否則點擊 EditText 會把布局往上推.

1 <activity
2     android:name=".CityActivity"
3     android:windowSoftInputMode="adjustPan|stateHidden"
4     android:configChanges="keyboardHidden|orientation|screenSize" />

 

程序 Demo: 鏈接:http://pan.baidu.com/s/1geW38GF 密碼:k505

***************很久沒發了,學習實在太累T T...******************

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved