仿美团城市选择界面

移动开发
MeiTuanLocateCity仿美团城市选择界面,定位城市界面设计。

源码简介:MeiTuanLocateCity仿美团城市选择界面,定位城市界面设计。

源码效果:

源码片段:

  1. package com.droid; 
  2.   
  3. import java.io.IOException; 
  4. import java.util.ArrayList; 
  5. import java.util.Collections; 
  6. import java.util.Comparator; 
  7. import java.util.HashMap; 
  8. import java.util.List; 
  9. import java.util.regex.Pattern; 
  10.   
  11. import android.app.Activity; 
  12. import android.content.Context; 
  13. import android.database.Cursor; 
  14. import android.database.sqlite.SQLiteDatabase; 
  15. import android.graphics.PixelFormat; 
  16. import android.os.Bundle; 
  17. import android.os.Handler; 
  18. import android.text.Editable; 
  19. import android.text.TextWatcher; 
  20. import android.util.Log; 
  21. import android.view.LayoutInflater; 
  22. import android.view.View; 
  23. import android.view.View.OnClickListener; 
  24. import android.view.ViewGroup; 
  25. import android.view.ViewGroup.LayoutParams; 
  26. import android.view.WindowManager; 
  27. import android.widget.AbsListView; 
  28. import android.widget.AbsListView.OnScrollListener; 
  29. import android.widget.AdapterView; 
  30. import android.widget.AdapterView.OnItemClickListener; 
  31. import android.widget.BaseAdapter; 
  32. import android.widget.EditText; 
  33. import android.widget.GridView; 
  34. import android.widget.ListView; 
  35. import android.widget.ProgressBar; 
  36. import android.widget.TextView; 
  37. import android.widget.Toast; 
  38.   
  39. import com.baidu.location.BDLocation; 
  40. import com.baidu.location.BDLocationListener; 
  41. import com.baidu.location.LocationClient; 
  42. import com.baidu.location.LocationClientOption; 
  43. import com.droid.MyLetterListView.OnTouchingLetterChangedListener; 
  44.   
  45. public class Activity01 extends Activity implements OnScrollListener { 
  46.     private BaseAdapter adapter; 
  47.     private ResultListAdapter resultListAdapter; 
  48.     private ListView personList; 
  49.     private ListView resultList; 
  50.     private TextView overlay; // 对话框首字母textview 
  51.     private MyLetterListView letterListView; // A-Z listview 
  52.     private HashMap<string, integer=""> alphaIndexer;// 存放存在的汉语拼音首字母和与之对应的列表位置 
  53.     private String[] sections;// 存放存在的汉语拼音首字母 
  54.     private Handler handler; 
  55.     private OverlayThread overlayThread; // 显示首字母对话框 
  56.     private ArrayList<city> allCity_lists; // 所有城市列表 
  57.     private ArrayList<city> city_lists;// 城市列表 
  58.     private ArrayList<city> city_hot; 
  59.     private ArrayList<city> city_result; 
  60.     private ArrayList<string> city_history; 
  61.     private EditText sh; 
  62.     private TextView tv_noresult; 
  63.   
  64.     private LocationClient mLocationClient; 
  65.     private MyLocationListener mMyLocationListener; 
  66.   
  67.     private String currentCity; // 用于保存定位到的城市 
  68.     private int locateProcess = 1// 记录当前定位的状态 正在定位-定位成功-定位失败 
  69.     private boolean isNeedFresh; 
  70.   
  71.     private DatabaseHelper helper; 
  72.   
  73.     @Override 
  74.     public void onCreate(Bundle savedInstanceState) { 
  75.         super.onCreate(savedInstanceState); 
  76.         setContentView(R.layout.main); 
  77.         personList = (ListView) findViewById(R.id.list_view); 
  78.         allCity_lists = new ArrayList<city>(); 
  79.         city_hot = new ArrayList<city>(); 
  80.         city_result = new ArrayList<city>(); 
  81.         city_history = new ArrayList<string>(); 
  82.         resultList = (ListView) findViewById(R.id.search_result); 
  83.         sh = (EditText) findViewById(R.id.sh); 
  84.         tv_noresult = (TextView) findViewById(R.id.tv_noresult); 
  85.         helper = new DatabaseHelper(this); 
  86.         sh.addTextChangedListener(new TextWatcher() { 
  87.   
  88.             @Override 
  89.             public void onTextChanged(CharSequence s, int start, int before, 
  90.                     int count) { 
  91.                 if (s.toString() == null || "".equals(s.toString())) { 
  92.                     letterListView.setVisibility(View.VISIBLE); 
  93.                     personList.setVisibility(View.VISIBLE); 
  94.                     resultList.setVisibility(View.GONE); 
  95.                     tv_noresult.setVisibility(View.GONE); 
  96.                 } else { 
  97.                     city_result.clear(); 
  98.                     letterListView.setVisibility(View.GONE); 
  99.                     personList.setVisibility(View.GONE); 
  100.                     getResultCityList(s.toString()); 
  101.                     if (city_result.size() <= 0) { 
  102.                         tv_noresult.setVisibility(View.VISIBLE); 
  103.                         resultList.setVisibility(View.GONE); 
  104.                     } else { 
  105.                         tv_noresult.setVisibility(View.GONE); 
  106.                         resultList.setVisibility(View.VISIBLE); 
  107.                         resultListAdapter.notifyDataSetChanged(); 
  108.                     } 
  109.                 } 
  110.             } 
  111.   
  112.             @Override 
  113.             public void beforeTextChanged(CharSequence s, int start, int count, 
  114.                     int after) { 
  115.             } 
  116.   
  117.             @Override 
  118.             public void afterTextChanged(Editable s) { 
  119.   
  120.             } 
  121.         }); 
  122.         letterListView = (MyLetterListView) findViewById(R.id.MyLetterListView01); 
  123.         letterListView 
  124.                 .setOnTouchingLetterChangedListener(new LetterListViewListener()); 
  125.         alphaIndexer = new HashMap<string, integer="">(); 
  126.         handler = new Handler(); 
  127.         overlayThread = new OverlayThread(); 
  128.         isNeedFresh = true
  129.         personList.setOnItemClickListener(new OnItemClickListener() { 
  130.   
  131.             @Override 
  132.             public void onItemClick(AdapterView<!--?--> parent, View view, 
  133.                     int position, long id) { 
  134.                 if (position >= 4) { 
  135.   
  136.                     Toast.makeText(getApplicationContext(), 
  137.                             allCity_lists.get(position).getName(), 
  138.                             Toast.LENGTH_SHORT).show(); 
  139.                 } 
  140.             } 
  141.         }); 
  142.         locateProcess = 1
  143.         personList.setAdapter(adapter); 
  144.         personList.setOnScrollListener(this); 
  145.         resultListAdapter = new ResultListAdapter(this, city_result); 
  146.         resultList.setAdapter(resultListAdapter); 
  147.         resultList.setOnItemClickListener(new OnItemClickListener() { 
  148.   
  149.             @Override 
  150.             public void onItemClick(AdapterView<!--?--> parent, View view, 
  151.                     int position, long id) { 
  152.                 Toast.makeText(getApplicationContext(), 
  153.                         city_result.get(position).getName(), Toast.LENGTH_SHORT) 
  154.                         .show(); 
  155.             } 
  156.         }); 
  157.         initOverlay(); 
  158.         cityInit(); 
  159.         hotCityInit(); 
  160.         hisCityInit(); 
  161.         setAdapter(allCity_lists, city_hot, city_history); 
  162.   
  163.         mLocationClient = new LocationClient(this.getApplicationContext()); 
  164.         mMyLocationListener = new MyLocationListener(); 
  165.         mLocationClient.registerLocationListener(mMyLocationListener); 
  166.         InitLocation(); 
  167.         mLocationClient.start(); 
  168.     } 
  169.   
  170.     public void InsertCity(String name) { 
  171.         SQLiteDatabase db = helper.getReadableDatabase(); 
  172.         Cursor cursor = db.rawQuery("select * from recentcity where name = '" 
  173.                 + name + "'"null); 
  174.         if (cursor.getCount() > 0) { // 
  175.             db.delete("recentcity""name = ?"new String[] { name }); 
  176.         } 
  177.         db.execSQL("insert into recentcity(name, date) values('" + name + "', " 
  178.                 + System.currentTimeMillis() + ")"); 
  179.         db.close(); 
  180.     } 
  181.   
  182.     private void InitLocation() { 
  183.         // 设置定位参数 
  184.         LocationClientOption option = new LocationClientOption(); 
  185.         option.setCoorType("bd09ll"); // 设置坐标类型 
  186.         option.setScanSpan(10000); // 10分钟扫描1次 
  187.         // 需要地址信息,设置为其他任何值(string类型,且不能为null)时,都表示无地址信息。 
  188.         option.setAddrType("all"); 
  189.         // 设置是否返回POI的电话和地址等详细信息。默认值为false,即不返回POI的电话和地址信息。 
  190.         option.setPoiExtraInfo(true); 
  191.         // 设置产品线名称。强烈建议您使用自定义的产品线名称,方便我们以后为您提供更高效准确的定位服务。 
  192.         option.setProdName("通过GPS定位我当前的位置"); 
  193.         // 禁用启用缓存定位数据 
  194.         option.disableCache(true); 
  195.         // 设置最多可返回的POI个数,默认值为3。由于POI查询比较耗费流量,设置最多返回的POI个数,以便节省流量。 
  196.         option.setPoiNumber(3); 
  197.         // 设置定位方式的优先级。 
  198.         // 当gps可用,而且获取了定位结果时,不再发起网络请求,直接返回给用户坐标。这个选项适合希望得到准确坐标位置的用户。如果gps不可用,再发起网络请求,进行定位。 
  199.         option.setPriority(LocationClientOption.GpsFirst); 
  200.         mLocationClient.setLocOption(option); 
  201.     } 
  202.   
  203.     private void cityInit() { 
  204.         City city = new City("定位""0"); // 当前定位城市 
  205.         allCity_lists.add(city); 
  206.         city = new City("最近""1"); // 最近访问的城市 
  207.         allCity_lists.add(city); 
  208.         city = new City("热门""2"); // 热门城市 
  209.         allCity_lists.add(city); 
  210.         city = new City("全部""3"); // 全部城市 
  211.         allCity_lists.add(city); 
  212.         city_lists = getCityList(); 
  213.         allCity_lists.addAll(city_lists); 
  214.     } 
  215.   
  216.     /** 
  217.      * 热门城市 
  218.      */ 
  219.     public void hotCityInit() { 
  220.         City city = new City("上海""2"); 
  221.         city_hot.add(city); 
  222.         city = new City("北京""2"); 
  223.         city_hot.add(city); 
  224.         city = new City("广州""2"); 
  225.         city_hot.add(city); 
  226.         city = new City("深圳""2"); 
  227.         city_hot.add(city); 
  228.         city = new City("武汉""2"); 
  229.         city_hot.add(city); 
  230.         city = new City("天津""2"); 
  231.         city_hot.add(city); 
  232.         city = new City("西安""2"); 
  233.         city_hot.add(city); 
  234.         city = new City("南京""2"); 
  235.         city_hot.add(city); 
  236.         city = new City("杭州""2"); 
  237.         city_hot.add(city); 
  238.         city = new City("成都""2"); 
  239.         city_hot.add(city); 
  240.         city = new City("重庆""2"); 
  241.         city_hot.add(city); 
  242.     } 
  243.   
  244.     private void hisCityInit() { 
  245.         SQLiteDatabase db = helper.getReadableDatabase(); 
  246.         Cursor cursor = db.rawQuery( 
  247.                 "select * from recentcity order by date desc limit 0, 3"null); 
  248.         while (cursor.moveToNext()) { 
  249.             city_history.add(cursor.getString(1)); 
  250.         } 
  251.         cursor.close(); 
  252.         db.close(); 
  253.     } 
  254.   
  255.     @SuppressWarnings("unchecked"
  256.     private ArrayList<city> getCityList() { 
  257.         DBHelper dbHelper = new DBHelper(this); 
  258.         ArrayList<city> list = new ArrayList<city>(); 
  259.         try { 
  260.             dbHelper.createDataBase(); 
  261.             SQLiteDatabase db = dbHelper.getWritableDatabase(); 
  262.             Cursor cursor = db.rawQuery("select * from city"null); 
  263.             City city; 
  264.             while (cursor.moveToNext()) { 
  265.                 city = new City(cursor.getString(1), cursor.getString(2)); 
  266.                 list.add(city); 
  267.             } 
  268.             cursor.close(); 
  269.             db.close(); 
  270.         } catch (IOException e) { 
  271.             e.printStackTrace(); 
  272.         } 
  273.         Collections.sort(list, comparator); 
  274.         return list; 
  275.     } 
  276.   
  277.     @SuppressWarnings("unchecked"
  278.     private void getResultCityList(String keyword) { 
  279.         DBHelper dbHelper = new DBHelper(this); 
  280.         try { 
  281.             dbHelper.createDataBase(); 
  282.             SQLiteDatabase db = dbHelper.getWritableDatabase(); 
  283.             Cursor cursor = db.rawQuery( 
  284.                     "select * from city where name like \"%" + keyword 
  285.                             + "%\" or pinyin like \"%" + keyword + "%\""null); 
  286.             City city; 
  287.             Log.e("info""length = " + cursor.getCount()); 
  288.             while (cursor.moveToNext()) { 
  289.                 city = new City(cursor.getString(1), cursor.getString(2)); 
  290.                 city_result.add(city); 
  291.             } 
  292.             cursor.close(); 
  293.             db.close(); 
  294.         } catch (IOException e) { 
  295.             e.printStackTrace(); 
  296.         } 
  297.         Collections.sort(city_result, comparator); 
  298.     } 
  299.   
  300.     /** 
  301.      * a-z排序 
  302.      */ 
  303.     @SuppressWarnings("rawtypes"
  304.     Comparator comparator = new Comparator<city>() { 
  305.         @Override 
  306.         public int compare(City lhs, City rhs) { 
  307.             String a = lhs.getPinyi().substring(01); 
  308.             String b = rhs.getPinyi().substring(01); 
  309.             int flag = a.compareTo(b); 
  310.             if (flag == 0) { 
  311.                 return a.compareTo(b); 
  312.             } else { 
  313.                 return flag; 
  314.             } 
  315.         } 
  316.     }; 
  317.   
  318.     private void setAdapter(List<city> list, List<city> hotList, 
  319.             List<string> hisCity) { 
  320.         adapter = new ListAdapter(this, list, hotList, hisCity); 
  321.         personList.setAdapter(adapter); 
  322.     } 
  323.   
  324.     /** 
  325.      * 实现实位回调监听 
  326.      */ 
  327.     public class MyLocationListener implements BDLocationListener { 
  328.   
  329.         @Override 
  330.         public void onReceiveLocation(BDLocation arg0) { 
  331.             Log.e("info""city = " + arg0.getCity()); 
  332.             if (!isNeedFresh) { 
  333.                 return
  334.             } 
  335.             isNeedFresh = false
  336.             if (arg0.getCity() == null) { 
  337.                 locateProcess = 3// 定位失败 
  338.                 personList.setAdapter(adapter); 
  339.                 adapter.notifyDataSetChanged(); 
  340.                 return
  341.             } 
  342.             currentCity = arg0.getCity().substring(0
  343.                     arg0.getCity().length() - 1); 
  344.             locateProcess = 2// 定位成功 
  345.             personList.setAdapter(adapter); 
  346.             adapter.notifyDataSetChanged(); 
  347.         } 
  348.   
  349.         @Override 
  350.         public void onReceivePoi(BDLocation arg0) { 
  351.   
  352.         } 
  353.     } 
  354.   
  355.     private class ResultListAdapter extends BaseAdapter { 
  356.         private LayoutInflater inflater; 
  357.         private ArrayList<city> results = new ArrayList<city>(); 
  358.   
  359.         public ResultListAdapter(Context context, ArrayList<city> results) { 
  360.             inflater = LayoutInflater.from(context); 
  361.             this.results = results; 
  362.         } 
  363.   
  364.         @Override 
  365.         public int getCount() { 
  366.             return results.size(); 
  367.         } 
  368.   
  369.         @Override 
  370.         public Object getItem(int position) { 
  371.             return position; 
  372.         } 
  373.   
  374.         @Override 
  375.         public long getItemId(int position) { 
  376.             return position; 
  377.         } 
  378.   
  379.         @Override 
  380.         public View getView(int position, View convertView, ViewGroup parent) { 
  381.             ViewHolder viewHolder = null
  382.             if (convertView == null) { 
  383.                 convertView = inflater.inflate(R.layout.list_item, null); 
  384.                 viewHolder = new ViewHolder(); 
  385.                 viewHolder.name = (TextView) convertView 
  386.                         .findViewById(R.id.name); 
  387.                 convertView.setTag(viewHolder); 
  388.             } else { 
  389.                 viewHolder = (ViewHolder) convertView.getTag(); 
  390.             } 
  391.             viewHolder.name.setText(results.get(position).getName()); 
  392.             return convertView; 
  393.         } 
  394.   
  395.         class ViewHolder { 
  396.             TextView name; 
  397.         } 
  398.     } 
  399.   
  400.     public class ListAdapter extends BaseAdapter { 
  401.         private Context context; 
  402.         private LayoutInflater inflater; 
  403.         private List<city> list; 
  404.         private List<city> hotList; 
  405.         private List<string> hisCity; 
  406.         final int VIEW_TYPE = 5
  407.   
  408.         public ListAdapter(Context context, List<city> list, 
  409.                 List<city> hotList, List<string> hisCity) { 
  410.             this.inflater = LayoutInflater.from(context); 
  411.             this.list = list; 
  412.             this.context = context; 
  413.             this.hotList = hotList; 
  414.             this.hisCity = hisCity; 
  415.             alphaIndexer = new HashMap<string, integer="">(); 
  416.             sections = new String[list.size()]; 
  417.             for (int i = 0; i < list.size(); i++) { 
  418.                 // 当前汉语拼音首字母 
  419.                 String currentStr = getAlpha(list.get(i).getPinyi()); 
  420.                 // 上一个汉语拼音首字母,如果不存在为" " 
  421.                 String previewStr = (i - 1) >= 0 ? getAlpha(list.get(i - 1
  422.                         .getPinyi()) : " "
  423.                 if (!previewStr.equals(currentStr)) { 
  424.                     String name = getAlpha(list.get(i).getPinyi()); 
  425.                     alphaIndexer.put(name, i); 
  426.                     sections[i] = name; 
  427.                 } 
  428.             } 
  429.         } 
  430.   
  431.         @Override 
  432.         public int getViewTypeCount() { 
  433.             return VIEW_TYPE; 
  434.         } 
  435.   
  436.         @Override 
  437.         public int getItemViewType(int position) { 
  438.             return position < 4 ? position : 4
  439.         } 
  440.   
  441.         @Override 
  442.         public int getCount() { 
  443.             return list.size(); 
  444.         } 
  445.   
  446.         @Override 
  447.         public Object getItem(int position) { 
  448.             return list.get(position); 
  449.         } 
  450.   
  451.         @Override 
  452.         public long getItemId(int position) { 
  453.             return position; 
  454.         } 
  455.   
  456.         ViewHolder holder; 
  457.   
  458.         @Override 
  459.         public View getView(int position, View convertView, ViewGroup parent) { 
  460.             final TextView city; 
  461.             int viewType = getItemViewType(position); 
  462.             if (viewType == 0) { // 定位 
  463.                 convertView = inflater.inflate(R.layout.frist_list_item, null); 
  464.                 TextView locateHint = (TextView) convertView 
  465.                         .findViewById(R.id.locateHint); 
  466.                 city = (TextView) convertView.findViewById(R.id.lng_city); 
  467.                 city.setOnClickListener(new OnClickListener() { 
  468.                     @Override 
  469.                     public void onClick(View v) { 
  470.                         if (locateProcess == 2) { 
  471.   
  472.                             Toast.makeText(getApplicationContext(), 
  473.                                     city.getText().toString(), 
  474.                                     Toast.LENGTH_SHORT).show(); 
  475.                         } else if (locateProcess == 3) { 
  476.                             locateProcess = 1
  477.                             personList.setAdapter(adapter); 
  478.                             adapter.notifyDataSetChanged(); 
  479.                             mLocationClient.stop(); 
  480.                             isNeedFresh = true
  481.                             InitLocation(); 
  482.                             currentCity = ""
  483.                             mLocationClient.start(); 
  484.                         } 
  485.                     } 
  486.                 }); 
  487.                 ProgressBar pbLocate = (ProgressBar) convertView 
  488.                         .findViewById(R.id.pbLocate); 
  489.                 if (locateProcess == 1) { // 正在定位 
  490.                     locateHint.setText("正在定位"); 
  491.                     city.setVisibility(View.GONE); 
  492.                     pbLocate.setVisibility(View.VISIBLE); 
  493.                 } else if (locateProcess == 2) { // 定位成功 
  494.                     locateHint.setText("当前定位城市"); 
  495.                     city.setVisibility(View.VISIBLE); 
  496.                     city.setText(currentCity); 
  497.                     mLocationClient.stop(); 
  498.                     pbLocate.setVisibility(View.GONE); 
  499.                 } else if (locateProcess == 3) { 
  500.                     locateHint.setText("未定位到城市,请选择"); 
  501.                     city.setVisibility(View.VISIBLE); 
  502.                     city.setText("重新选择"); 
  503.                     pbLocate.setVisibility(View.GONE); 
  504.                 } 
  505.             } else if (viewType == 1) { // 最近访问城市 
  506.                 convertView = inflater.inflate(R.layout.recent_city, null); 
  507.                 GridView rencentCity = (GridView) convertView 
  508.                         .findViewById(R.id.recent_city); 
  509.                 rencentCity 
  510.                         .setAdapter(new HitCityAdapter(context, this.hisCity)); 
  511.                 rencentCity.setOnItemClickListener(new OnItemClickListener() { 
  512.   
  513.                     @Override 
  514.                     public void onItemClick(AdapterView<!--?--> parent, View view, 
  515.                             int position, long id) { 
  516.   
  517.                         Toast.makeText(getApplicationContext(), 
  518.                                 city_history.get(position), Toast.LENGTH_SHORT) 
  519.                                 .show(); 
  520.   
  521.                     } 
  522.                 }); 
  523.                 TextView recentHint = (TextView) convertView 
  524.                         .findViewById(R.id.recentHint); 
  525.                 recentHint.setText("最近访问的城市"); 
  526.             } else if (viewType == 2) { 
  527.                 convertView = inflater.inflate(R.layout.recent_city, null); 
  528.                 GridView hotCity = (GridView) convertView 
  529.                         .findViewById(R.id.recent_city); 
  530.                 hotCity.setOnItemClickListener(new OnItemClickListener() { 
  531.   
  532.                     @Override 
  533.                     public void onItemClick(AdapterView<!--?--> parent, View view, 
  534.                             int position, long id) { 
  535.   
  536.                         Toast.makeText(getApplicationContext(), 
  537.                                 city_hot.get(position).getName(), 
  538.                                 Toast.LENGTH_SHORT).show(); 
  539.   
  540.                     } 
  541.                 }); 
  542.                 hotCity.setAdapter(new HotCityAdapter(context, this.hotList)); 
  543.                 TextView hotHint = (TextView) convertView 
  544.                         .findViewById(R.id.recentHint); 
  545.                 hotHint.setText("热门城市"); 
  546.             } else if (viewType == 3) { 
  547.                 convertView = inflater.inflate(R.layout.total_item, null); 
  548.             } else { 
  549.                 if (convertView == null) { 
  550.                     convertView = inflater.inflate(R.layout.list_item, null); 
  551.                     holder = new ViewHolder(); 
  552.                     holder.alpha = (TextView) convertView 
  553.                             .findViewById(R.id.alpha); 
  554.                     holder.name = (TextView) convertView 
  555.                             .findViewById(R.id.name); 
  556.                     convertView.setTag(holder); 
  557.                 } else { 
  558.                     holder = (ViewHolder) convertView.getTag(); 
  559.                 } 
  560.                 if (position >= 1) { 
  561.                     holder.name.setText(list.get(position).getName()); 
  562.                     String currentStr = getAlpha(list.get(position).getPinyi()); 
  563.                     String previewStr = (position - 1) >= 0 ? getAlpha(list 
  564.                             .get(position - 1).getPinyi()) : " "
  565.                     if (!previewStr.equals(currentStr)) { 
  566.                         holder.alpha.setVisibility(View.VISIBLE); 
  567.                         holder.alpha.setText(currentStr); 
  568.                     } else { 
  569.                         holder.alpha.setVisibility(View.GONE); 
  570.                     } 
  571.                 } 
  572.             } 
  573.             return convertView; 
  574.         } 
  575.   
  576.         private class ViewHolder { 
  577.             TextView alpha; // 首字母标题 
  578.             TextView name; // 城市名字 
  579.         } 
  580.     } 
  581.   
  582.     @Override 
  583.     protected void onStop() { 
  584.         mLocationClient.stop(); 
  585.         super.onStop(); 
  586.     } 
  587.   
  588.     class HotCityAdapter extends BaseAdapter { 
  589.         private Context context; 
  590.         private LayoutInflater inflater; 
  591.         private List<city> hotCitys; 
  592.   
  593.         public HotCityAdapter(Context context, List<city> hotCitys) { 
  594.             this.context = context; 
  595.             inflater = LayoutInflater.from(this.context); 
  596.             this.hotCitys = hotCitys; 
  597.         } 
  598.   
  599.         @Override 
  600.         public int getCount() { 
  601.             return hotCitys.size(); 
  602.         } 
  603.   
  604.         @Override 
  605.         public Object getItem(int position) { 
  606.             return position; 
  607.         } 
  608.   
  609.         @Override 
  610.         public long getItemId(int position) { 
  611.             return position; 
  612.         } 
  613.   
  614.         @Override 
  615.         public View getView(int position, View convertView, ViewGroup parent) { 
  616.             convertView = inflater.inflate(R.layout.item_city, null); 
  617.             TextView city = (TextView) convertView.findViewById(R.id.city); 
  618.             city.setText(hotCitys.get(position).getName()); 
  619.             return convertView; 
  620.         } 
  621.     } 
  622.   
  623.     class HitCityAdapter extends BaseAdapter { 
  624.         private Context context; 
  625.         private LayoutInflater inflater; 
  626.         private List<string> hotCitys; 
  627.   
  628.         public HitCityAdapter(Context context, List<string> hotCitys) { 
  629.             this.context = context; 
  630.             inflater = LayoutInflater.from(this.context); 
  631.             this.hotCitys = hotCitys; 
  632.         } 
  633.   
  634.         @Override 
  635.         public int getCount() { 
  636.             return hotCitys.size(); 
  637.         } 
  638.   
  639.         @Override 
  640.         public Object getItem(int position) { 
  641.             return position; 
  642.         } 
  643.   
  644.         @Override 
  645.         public long getItemId(int position) { 
  646.             return position; 
  647.         } 
  648.   
  649.         @Override 
  650.         public View getView(int position, View convertView, ViewGroup parent) { 
  651.             convertView = inflater.inflate(R.layout.item_city, null); 
  652.             TextView city = (TextView) convertView.findViewById(R.id.city); 
  653.             city.setText(hotCitys.get(position)); 
  654.             return convertView; 
  655.         } 
  656.     } 
  657.   
  658.     private boolean mReady; 
  659.   
  660.     // 初始化汉语拼音首字母弹出提示框 
  661.     private void initOverlay() { 
  662.         mReady = true
  663.         LayoutInflater inflater = LayoutInflater.from(this); 
  664.         overlay = (TextView) inflater.inflate(R.layout.overlay, null); 
  665.         overlay.setVisibility(View.INVISIBLE); 
  666.         WindowManager.LayoutParams lp = new WindowManager.LayoutParams( 
  667.                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 
  668.                 WindowManager.LayoutParams.TYPE_APPLICATION, 
  669.                 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 
  670.                         | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, 
  671.                 PixelFormat.TRANSLUCENT); 
  672.         WindowManager windowManager = (WindowManager) this 
  673.                 .getSystemService(Context.WINDOW_SERVICE); 
  674.         windowManager.addView(overlay, lp); 
  675.     } 
  676.   
  677.     private boolean isScroll = false
  678.   
  679.     private class LetterListViewListener implements 
  680.             OnTouchingLetterChangedListener { 
  681.   
  682.         @Override 
  683.         public void onTouchingLetterChanged(final String s) { 
  684.             isScroll = false
  685.             if (alphaIndexer.get(s) != null) { 
  686.                 int position = alphaIndexer.get(s); 
  687.                 personList.setSelection(position); 
  688.                 overlay.setText(s); 
  689.                 overlay.setVisibility(View.VISIBLE); 
  690.                 handler.removeCallbacks(overlayThread); 
  691.                 // 延迟一秒后执行,让overlay为不可见 
  692.                 handler.postDelayed(overlayThread, 1000); 
  693.             } 
  694.         } 
  695.     } 
  696.   
  697.     // 设置overlay不可见 
  698.     private class OverlayThread implements Runnable { 
  699.         @Override 
  700.         public void run() { 
  701.             overlay.setVisibility(View.GONE); 
  702.         } 
  703.     } 
  704.   
  705.     // 获得汉语拼音首字母 
  706.     private String getAlpha(String str) { 
  707.         if (str == null) { 
  708.             return "#"
  709.         } 
  710.         if (str.trim().length() == 0) { 
  711.             return "#"
  712.         } 
  713.         char c = str.trim().substring(01).charAt(0); 
  714.         // 正则表达式,判断首字母是否是英文字母 
  715.         Pattern pattern = Pattern.compile("^[A-Za-z]+$"); 
  716.         if (pattern.matcher(c + "").matches()) { 
  717.             return (c + "").toUpperCase(); 
  718.         } else if (str.equals("0")) { 
  719.             return "定位"
  720.         } else if (str.equals("1")) { 
  721.             return "最近"
  722.         } else if (str.equals("2")) { 
  723.             return "热门"
  724.         } else if (str.equals("3")) { 
  725.             return "全部"
  726.         } else { 
  727.             return "#"
  728.         } 
  729.     } 
  730.   
  731.     @Override 
  732.     public void onScrollStateChanged(AbsListView view, int scrollState) { 
  733.         if (scrollState == SCROLL_STATE_TOUCH_SCROLL 
  734.                 || scrollState == SCROLL_STATE_FLING) { 
  735.             isScroll = true
  736.         } 
  737.     } 
  738.   
  739.     @Override 
  740.     public void onScroll(AbsListView view, int firstVisibleItem, 
  741.             int visibleItemCount, int totalItemCount) { 
  742.         if (!isScroll) { 
  743.             return
  744.         } 
  745.   
  746.         if (mReady) { 
  747.             String text; 
  748.             String name = allCity_lists.get(firstVisibleItem).getName(); 
  749.             String pinyin = allCity_lists.get(firstVisibleItem).getPinyi(); 
  750.             if (firstVisibleItem < 4) { 
  751.                 text = name; 
  752.             } else { 
  753.                 text = PingYinUtil.converterToFirstSpell(pinyin) 
  754.                         .substring(01).toUpperCase(); 
  755.             } 
  756.             overlay.setText(text); 
  757.             overlay.setVisibility(View.VISIBLE); 
  758.             handler.removeCallbacks(overlayThread); 
  759.             // 延迟一秒后执行,让overlay为不可见 
  760.             handler.postDelayed(overlayThread, 1000); 
  761.         } 
  762.     } 
  763. }</string></string></city></city></string,></string></city></city></string></city></city></city></city></city></string></city></city></city></city></city></city></string,></string></city></city></city></string></city></city></city></city></string,> 

下载地址:http://down.51cto.com/data/2103327

责任编辑:倪明 来源: devstore
相关推荐

2015-08-07 15:39:26

仿微信语音界面源码

2009-10-29 09:55:43

无线接入技术

2015-08-07 15:32:19

欢迎界面仿微信仿qq空间

2012-07-16 10:04:45

JavaScript

2015-10-12 11:19:35

墨迹天气引导界面android

2015-09-01 16:55:42

微信朋友圈图片

2021-11-23 10:00:55

鸿蒙HarmonyOS应用

2012-12-25 13:16:56

AndroidQQ2012UI

2015-11-05 16:41:55

android源码仿ios

2021-06-01 09:34:07

面部识别人工智能AI

2015-08-17 10:50:51

美团多级下拉菜单简洁

2012-03-01 16:50:15

2014-12-05 10:18:21

大悦城华三

2017-03-31 21:32:05

AndroidView按钮

2020-10-23 10:58:27

华为城市峰会蓉城

2015-10-13 10:49:44

Pear OSMac OS XLinux

2011-11-10 15:48:06

飞视美视频会议

2010-02-25 09:04:50

浏览器选择界面

2012-09-11 13:19:18

2023-09-05 10:25:35

数据库性能
点赞
收藏

51CTO技术栈公众号