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...******************

posted on 2016-10-12 09:57  西门吃雪  阅读(4393)  评论(0编辑  收藏  举报

导航