From 147d902853ac1e80800ba7823a26357e3f57ef5c Mon Sep 17 00:00:00 2001 From: rBrilliant <499953902@qq.com> Date: Sun, 27 Aug 2017 00:37:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86demo=E5=8C=85?= =?UTF-8?q?=E5=92=8Ccitypicker=E4=B8=AD=E5=AD=98=E5=9C=A8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/inspectionProfiles/Project_Default.xml | 37 ---- .../inspectionProfiles/profiles_settings.xml | 7 - .idea/misc.xml | 18 +- .idea/modules.xml | 2 +- .idea/vcs.xml | 6 + README.md | 59 +++--- build.gradle | 2 +- citypicker/build.gradle | 170 +++++++-------- .../zaaach/citypicker/CityPickerActivity.java | 195 ------------------ .../zaaach/citypicker/CityPickerFragment.java | 16 +- .../com/zaaach/citypicker/DemoActivity.java | 67 ------ .../citypicker/adapter/CityListAdapter.java | 1 + .../adapter/HotCityGridAdapter.java | 1 + .../citypicker/adapter/ResultListAdapter.java | 1 + .../zaaach/citypicker/view/SideLetterBar.java | 17 +- .../src/main/res/layout/cp_fragment_city_list | 57 ----- ...ity_list.xml => cp_fragment_city_list.xml} | 4 +- demo/src/main/AndroidManifest.xml | 3 +- .../citypickerdemo/CityPickerActivity.java | 75 +++++++ .../zaaach/citypickerdemo/MainActivity.java | 5 +- .../main/res/layout/activity_city_picker.xml | 12 ++ gradle/wrapper/gradle-wrapper.properties | 4 +- 22 files changed, 264 insertions(+), 495 deletions(-) delete mode 100644 .idea/inspectionProfiles/Project_Default.xml delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/vcs.xml delete mode 100644 citypicker/src/main/java/com/zaaach/citypicker/CityPickerActivity.java delete mode 100644 citypicker/src/main/java/com/zaaach/citypicker/DemoActivity.java delete mode 100644 citypicker/src/main/res/layout/cp_fragment_city_list rename citypicker/src/main/res/layout/{cp_activity_city_list.xml => cp_fragment_city_list.xml} (97%) create mode 100644 demo/src/main/java/com/zaaach/citypickerdemo/CityPickerActivity.java create mode 100644 demo/src/main/res/layout/activity_city_picker.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index 9e57984..0000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 3b31283..0000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 5d19981..589e1ad 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,10 +37,26 @@ - + + + + + + Android API 17 Platform + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index 649fa31..1e76133 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 7eca583..73fe4b9 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ # Install 目前没上传,只能 -下载library手动导入. +下载library(citypicker)手动导入. # Usage @@ -43,19 +43,33 @@ cityPickerFragment.updateLocateState(LocateState.SUCCESS, location.replaceAll("市", "")); ``` +### Step3 +添加到清单文件中,修改软键盘模式。添加高德地图的key +``` + + + +``` + + # 例子 ``` /** * 引用例子 */ -public class DemoActivity extends AppCompatActivity { - - Toolbar toolbar; - - FrameLayout flCityPickerContainer; +public class CityPickerActivity extends AppCompatActivity { + private FrameLayout flCityPickerContainer; private CityPickerFragment cityPickerFragment; + //声明AMapLocationClient类对象 + public AMapLocationClient mLocationClient = null; + //声明定位回调监听器 + public AMapLocationListener mLocationListener; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -64,45 +78,44 @@ public class DemoActivity extends AppCompatActivity { initView(); } - - protected void initView() { - -    //这里 -        cityPickerFragment = new CityPickerFragment(); + private void initView() { + cityPickerFragment = new CityPickerFragment(); getSupportFragmentManager().beginTransaction() - .add(R.id.fl_activity_city_picker_container, cityPickerFragment).commit(); + .add(R.id.fl_activity_city_container, cityPickerFragment).commit(); + initLocationListener(); + //初始化定位 + mLocationClient = new AMapLocationClient(getApplicationContext()); + //设置定位回调监听 + mLocationClient.setLocationListener(mLocationListener); + //启动定位 + mLocationClient.startLocation(); + } - //定位 - LocationManager.startSingleLocation(new IMapLocationListener() { + private void initLocationListener() { + mLocationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation aMapLocation) { - if (aMapLocation.getErrorCode() == 0) { String city = aMapLocation.getCity(); String district = aMapLocation.getDistrict(); String location = StringUtils.extractLocation(city, district); - + Toast.makeText(CityPickerActivity.this,city,Toast.LENGTH_SHORT).show(); //定位成功,更新状态 cityPickerFragment.updateLocateState(LocateState.SUCCESS, location.replaceAll("市", "")); } else { cityPickerFragment.updateLocateState(LocateState.FAILED, null); - } - - } - }); + }; } @Override protected void onDestroy() { super.onDestroy(); - //销毁定位 - LocationManager.stopSingleLocation(); + mLocationClient.stopLocation(); } } - ``` diff --git a/build.gradle b/build.gradle index 36efe65..67499a9 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.2' + classpath 'com.android.tools.build:gradle:2.3.3' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' diff --git a/citypicker/build.gradle b/citypicker/build.gradle index e9e8e16..85d9002 100644 --- a/citypicker/build.gradle +++ b/citypicker/build.gradle @@ -31,89 +31,89 @@ dependencies { compile files('libs/AMap_Location_V3.2.0_20161205.jar') } -apply plugin: 'com.github.dcendents.android-maven' -apply plugin: 'com.jfrog.bintray' +//apply plugin: 'com.github.dcendents.android-maven' +//apply plugin: 'com.jfrog.bintray' +// +//version = "1.1" +// +//def siteUrl = 'https://github.com/zaaach/CityPicker.git' +//def gitUrl = 'git@github.com:zaaach/CityPicker.git' +// +//group = "com.zaaach" +//install { +// repositories.mavenInstaller { +// pom { +// project { +// packaging 'aar' +// name 'CityPicker' +// description 'a nice city picker library for android.' +// url siteUrl +// +// licenses { +// license { +// name 'The Apache Software License, Version 2.0' +// url 'http://www.apache.org/licenses/LICENSE-2.0.txt' +// } +// } +// +// developers { +// developer { +// id 'zaaach' +// name 'zaaach' +// email 'bro0clzq@gmail.com' +// } +// } +// +// scm { +// connection gitUrl +// developerConnection gitUrl +// url siteUrl +// } +// } +// } +// } +//} -version = "1.1" - -def siteUrl = 'https://github.com/zaaach/CityPicker.git' -def gitUrl = 'git@github.com:zaaach/CityPicker.git' - -group = "com.zaaach" -install { - repositories.mavenInstaller { - pom { - project { - packaging 'aar' - name 'CityPicker' - description 'a nice city picker library for android.' - url siteUrl - - licenses { - license { - name 'The Apache Software License, Version 2.0' - url 'http://www.apache.org/licenses/LICENSE-2.0.txt' - } - } - - developers { - developer { - id 'zaaach' - name 'zaaach' - email 'bro0clzq@gmail.com' - } - } - - scm { - connection gitUrl - developerConnection gitUrl - url siteUrl - } - } - } - } -} - -// 生成jar包的task -task sourcesJar(type: Jar) { - from android.sourceSets.main.java.srcDirs - classifier = 'sources' -} -// 生成jarDoc的task -task javadoc(type: Javadoc) { - source = android.sourceSets.main.java.srcDirs - classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) - options.encoding "UTF-8" - options.charSet 'UTF-8' - options.author true - options.version true - failOnError false -} -// 生成javaDoc的jar -task javadocJar(type: Jar, dependsOn: javadoc) { - classifier = 'javadoc' - from javadoc.destinationDir -} -artifacts { - archives javadocJar - archives sourcesJar -} - -//上传传到Jcenter -Properties properties = new Properties() -properties.load(project.rootProject.file('local.properties').newDataInputStream()) -bintray { - user = properties.getProperty("bintray.user") - key = properties.getProperty("bintray.apikey") - - configurations = ['archives'] - pkg { - repo = "maven" - userOrg= "zaaach" - name = "CityPicker" - websiteUrl = siteUrl - vcsUrl = gitUrl - licenses = ["Apache-2.0"] - publish = true - } -} +//// 生成jar包的task +//task sourcesJar(type: Jar) { +// from android.sourceSets.main.java.srcDirs +// classifier = 'sources' +//} +//// 生成jarDoc的task +//task javadoc(type: Javadoc) { +// source = android.sourceSets.main.java.srcDirs +// classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) +// options.encoding "UTF-8" +// options.charSet 'UTF-8' +// options.author true +// options.version true +// failOnError false +//} +//// 生成javaDoc的jar +//task javadocJar(type: Jar, dependsOn: javadoc) { +// classifier = 'javadoc' +// from javadoc.destinationDir +//} +//artifacts { +// archives javadocJar +// archives sourcesJar +//} +// +////上传传到Jcenter +//Properties properties = new Properties() +//properties.load(project.rootProject.file('local.properties').newDataInputStream()) +//bintray { +// user = properties.getProperty("bintray.user") +// key = properties.getProperty("bintray.apikey") +// +// configurations = ['archives'] +// pkg { +// repo = "maven" +// userOrg= "zaaach" +// name = "CityPicker" +// websiteUrl = siteUrl +// vcsUrl = gitUrl +// licenses = ["Apache-2.0"] +// publish = true +// } +//} diff --git a/citypicker/src/main/java/com/zaaach/citypicker/CityPickerActivity.java b/citypicker/src/main/java/com/zaaach/citypicker/CityPickerActivity.java deleted file mode 100644 index 73a1e3f..0000000 --- a/citypicker/src/main/java/com/zaaach/citypicker/CityPickerActivity.java +++ /dev/null @@ -1,195 +0,0 @@ -package com.zaaach.citypicker; - -import android.content.Intent; -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.text.Editable; -import android.text.TextUtils; -import android.text.TextWatcher; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.EditText; -import android.widget.ImageView; -import android.widget.ListView; -import android.widget.TextView; - -import com.amap.api.location.AMapLocation; -import com.amap.api.location.AMapLocationClient; -import com.amap.api.location.AMapLocationClientOption; -import com.amap.api.location.AMapLocationListener; -import com.zaaach.citypicker.adapter.CityListAdapter; -import com.zaaach.citypicker.adapter.ResultListAdapter; -import com.zaaach.citypicker.db.DBManager; -import com.zaaach.citypicker.model.City; -import com.zaaach.citypicker.model.LocateState; -import com.zaaach.citypicker.utils.StringUtils; -import com.zaaach.citypicker.view.SideLetterBar; - -import java.util.List; - -/** - * Author Bro0cL on 2016/12/16. - */ -public class CityPickerActivity extends AppCompatActivity implements View.OnClickListener { - public static final String KEY_PICKED_CITY = "picked_city"; - - private ListView mListView; - private ListView mResultListView; - private SideLetterBar mLetterBar; - private EditText searchBox; - private ImageView clearBtn; - private ImageView backBtn; - private ViewGroup emptyView; - - private CityListAdapter mCityAdapter; - private ResultListAdapter mResultAdapter; - private List mAllCities; - private DBManager dbManager; - - private AMapLocationClient mLocationClient; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.cp_activity_city_list); - - initData(); - initView(); - initLocation(); - } - - private void initLocation() { - mLocationClient = new AMapLocationClient(this); - AMapLocationClientOption option = new AMapLocationClientOption(); - option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); - option.setOnceLocation(true); - mLocationClient.setLocationOption(option); - mLocationClient.setLocationListener(new AMapLocationListener() { - @Override - public void onLocationChanged(AMapLocation aMapLocation) { - if (aMapLocation != null) { - if (aMapLocation.getErrorCode() == 0) { - String city = aMapLocation.getCity(); - String district = aMapLocation.getDistrict(); - String location = StringUtils.extractLocation(city, district); - mCityAdapter.updateLocateState(LocateState.SUCCESS, location); - } else { - //定位失败 - mCityAdapter.updateLocateState(LocateState.FAILED, null); - } - } - } - }); - mLocationClient.startLocation(); - } - - private void initData() { - dbManager = new DBManager(this); - dbManager.copyDBFile(); - mAllCities = dbManager.getAllCities(); - mCityAdapter = new CityListAdapter(this, mAllCities); - mCityAdapter.setOnCityClickListener(new CityListAdapter.OnCityClickListener() { - @Override - public void onCityClick(String name) { - back(name); - } - - @Override - public void onLocateClick() { - mCityAdapter.updateLocateState(LocateState.LOCATING, null); - mLocationClient.startLocation(); - } - }); - - mResultAdapter = new ResultListAdapter(this, null); - } - - private void initView() { - mListView = (ListView) findViewById(R.id.listview_all_city); - mListView.setAdapter(mCityAdapter); - - TextView overlay = (TextView) findViewById(R.id.tv_letter_overlay); - mLetterBar = (SideLetterBar) findViewById(R.id.side_letter_bar); - mLetterBar.setOverlay(overlay); - mLetterBar.setOnLetterChangedListener(new SideLetterBar.OnLetterChangedListener() { - @Override - public void onLetterChanged(String letter) { - int position = mCityAdapter.getLetterPosition(letter); - mListView.setSelection(position); - } - }); - - searchBox = (EditText) findViewById(R.id.et_search); - searchBox.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) {} - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) {} - - @Override - public void afterTextChanged(Editable s) { - String keyword = s.toString(); - if (TextUtils.isEmpty(keyword)) { - clearBtn.setVisibility(View.GONE); - emptyView.setVisibility(View.GONE); - mResultListView.setVisibility(View.GONE); - } else { - clearBtn.setVisibility(View.VISIBLE); - mResultListView.setVisibility(View.VISIBLE); - List result = dbManager.searchCity(keyword); - if (result == null || result.size() == 0) { - emptyView.setVisibility(View.VISIBLE); - } else { - emptyView.setVisibility(View.GONE); - mResultAdapter.changeData(result); - } - } - } - }); - - emptyView = (ViewGroup) findViewById(R.id.empty_view); - mResultListView = (ListView) findViewById(R.id.listview_search_result); - mResultListView.setAdapter(mResultAdapter); - mResultListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - back(mResultAdapter.getItem(position).getName()); - } - }); - - clearBtn = (ImageView) findViewById(R.id.iv_search_clear); - backBtn = (ImageView) findViewById(R.id.back); - - clearBtn.setOnClickListener(this); - backBtn.setOnClickListener(this); - } - - private void back(String city){ - Intent data = new Intent(); - data.putExtra(KEY_PICKED_CITY, city); - setResult(RESULT_OK, data); - finish(); - } - - @Override - public void onClick(View v) { - int i = v.getId(); - if (i == R.id.iv_search_clear) { - searchBox.setText(""); - clearBtn.setVisibility(View.GONE); - emptyView.setVisibility(View.GONE); - mResultListView.setVisibility(View.GONE); - } else if (i == R.id.back) { - finish(); - - } - } - - @Override - protected void onDestroy() { - super.onDestroy(); - mLocationClient.stopLocation(); - } -} diff --git a/citypicker/src/main/java/com/zaaach/citypicker/CityPickerFragment.java b/citypicker/src/main/java/com/zaaach/citypicker/CityPickerFragment.java index 770d9cf..55ee1ed 100644 --- a/citypicker/src/main/java/com/zaaach/citypicker/CityPickerFragment.java +++ b/citypicker/src/main/java/com/zaaach/citypicker/CityPickerFragment.java @@ -1,4 +1,4 @@ -package com.qiwei.citypickerlibrary.citypicker; +package com.zaaach.citypicker; import android.content.Intent; import android.os.Bundle; @@ -16,13 +16,13 @@ import android.widget.ListView; import android.widget.TextView; -import com.qiwei.citypickerlibrary.R; -import com.qiwei.citypickerlibrary.citypicker.adapter.CityListAdapter; -import com.qiwei.citypickerlibrary.citypicker.adapter.ResultListAdapter; -import com.qiwei.citypickerlibrary.citypicker.db.DBManager; -import com.qiwei.citypickerlibrary.citypicker.model.City; -import com.qiwei.citypickerlibrary.citypicker.model.LocateState; -import com.qiwei.citypickerlibrary.citypicker.view.SideLetterBar; + +import com.zaaach.citypicker.adapter.CityListAdapter; +import com.zaaach.citypicker.adapter.ResultListAdapter; +import com.zaaach.citypicker.db.DBManager; +import com.zaaach.citypicker.model.City; +import com.zaaach.citypicker.model.LocateState; +import com.zaaach.citypicker.view.SideLetterBar; import java.util.List; diff --git a/citypicker/src/main/java/com/zaaach/citypicker/DemoActivity.java b/citypicker/src/main/java/com/zaaach/citypicker/DemoActivity.java deleted file mode 100644 index 88b0eef..0000000 --- a/citypicker/src/main/java/com/zaaach/citypicker/DemoActivity.java +++ /dev/null @@ -1,67 +0,0 @@ -import android.location.LocationManager; -import android.os.Bundle; -import android.support.annotation.Nullable; -import android.support.v7.app.AppCompatActivity; -import android.widget.FrameLayout; - -import com.qiwei.citypickerlibrary.R; -import com.qiwei.citypickerlibrary.citypicker.model.LocateState; -import com.qiwei.citypickerlibrary.citypicker.utils.StringUtils; - - -/** - * 引用例子 - */ -public class DemoActivity extends AppCompatActivity { - - Toolbar toolbar; - - FrameLayout flCityPickerContainer; - - private CityPickerFragment cityPickerFragment; - - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_city_picker); - initView(); - } - - - protected void initView() { - cityPickerFragment = new CityPickerFragment(); - getSupportFragmentManager().beginTransaction() - .add(R.id.fl_activity_city_picker_container, cityPickerFragment).commit(); - - - - //定位 - LocationManager.startSingleLocation(new IMapLocationListener() { - @Override - public void onLocationChanged(AMapLocation aMapLocation) { - - if (aMapLocation.getErrorCode() == 0) { - String city = aMapLocation.getCity(); - String district = aMapLocation.getDistrict(); - String location = StringUtils.extractLocation(city, district); - - //定位成功,更新状态 - cityPickerFragment.updateLocateState(LocateState.SUCCESS, location.replaceAll("市", "")); - } else { - cityPickerFragment.updateLocateState(LocateState.FAILED, null); - - } - - - } - }); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - - //销毁定位 - LocationManager.stopSingleLocation(); - } -} diff --git a/citypicker/src/main/java/com/zaaach/citypicker/adapter/CityListAdapter.java b/citypicker/src/main/java/com/zaaach/citypicker/adapter/CityListAdapter.java index 5f575e7..34bb7a9 100644 --- a/citypicker/src/main/java/com/zaaach/citypicker/adapter/CityListAdapter.java +++ b/citypicker/src/main/java/com/zaaach/citypicker/adapter/CityListAdapter.java @@ -15,6 +15,7 @@ import com.zaaach.citypicker.utils.PinyinUtils; import com.zaaach.citypicker.view.WrapHeightGridView; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.HashMap; import java.util.List; diff --git a/citypicker/src/main/java/com/zaaach/citypicker/adapter/HotCityGridAdapter.java b/citypicker/src/main/java/com/zaaach/citypicker/adapter/HotCityGridAdapter.java index e3b789c..4996ea9 100644 --- a/citypicker/src/main/java/com/zaaach/citypicker/adapter/HotCityGridAdapter.java +++ b/citypicker/src/main/java/com/zaaach/citypicker/adapter/HotCityGridAdapter.java @@ -9,6 +9,7 @@ import com.zaaach.citypicker.R; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; diff --git a/citypicker/src/main/java/com/zaaach/citypicker/adapter/ResultListAdapter.java b/citypicker/src/main/java/com/zaaach/citypicker/adapter/ResultListAdapter.java index 74120e7..0b78020 100644 --- a/citypicker/src/main/java/com/zaaach/citypicker/adapter/ResultListAdapter.java +++ b/citypicker/src/main/java/com/zaaach/citypicker/adapter/ResultListAdapter.java @@ -10,6 +10,7 @@ import com.zaaach.citypicker.R; import com.zaaach.citypicker.model.City; +import java.lang.ref.WeakReference; import java.util.List; /** diff --git a/citypicker/src/main/java/com/zaaach/citypicker/view/SideLetterBar.java b/citypicker/src/main/java/com/zaaach/citypicker/view/SideLetterBar.java index 998a0ce..2f60d0b 100644 --- a/citypicker/src/main/java/com/zaaach/citypicker/view/SideLetterBar.java +++ b/citypicker/src/main/java/com/zaaach/citypicker/view/SideLetterBar.java @@ -18,6 +18,10 @@ public class SideLetterBar extends View { private boolean showBg = false; private OnLetterChangedListener onLetterChangedListener; private TextView overlay; + //SideLetterBar的高度 +// private int mHight; + //判断是否第一次绘制 + private boolean isFirst = true; public SideLetterBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); @@ -33,9 +37,10 @@ public SideLetterBar(Context context) { /** * 设置悬浮的textview + * * @param overlay */ - public void setOverlay(TextView overlay){ + public void setOverlay(TextView overlay) { this.overlay = overlay; } @@ -48,6 +53,10 @@ protected void onDraw(Canvas canvas) { } int height = getHeight(); +// if (isFirst) { //只设置一次,SideLetterBar不会随着软键盘出现而改变布局高度 +// mHight = getHeight(); +// isFirst = false; +// } int width = getWidth(); int singleHeight = height / b.length; for (int i = 0; i < b.length; i++) { @@ -82,7 +91,7 @@ public boolean dispatchTouchEvent(MotionEvent event) { listener.onLetterChanged(b[c]); choose = c; invalidate(); - if (overlay != null){ + if (overlay != null) { overlay.setVisibility(VISIBLE); overlay.setText(b[c]); } @@ -96,7 +105,7 @@ public boolean dispatchTouchEvent(MotionEvent event) { listener.onLetterChanged(b[c]); choose = c; invalidate(); - if (overlay != null){ + if (overlay != null) { overlay.setVisibility(VISIBLE); overlay.setText(b[c]); } @@ -107,7 +116,7 @@ public boolean dispatchTouchEvent(MotionEvent event) { showBg = false; choose = -1; invalidate(); - if (overlay != null){ + if (overlay != null) { overlay.setVisibility(GONE); } break; diff --git a/citypicker/src/main/res/layout/cp_fragment_city_list b/citypicker/src/main/res/layout/cp_fragment_city_list deleted file mode 100644 index 8333b5a..0000000 --- a/citypicker/src/main/res/layout/cp_fragment_city_list +++ /dev/null @@ -1,57 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/citypicker/src/main/res/layout/cp_activity_city_list.xml b/citypicker/src/main/res/layout/cp_fragment_city_list.xml similarity index 97% rename from citypicker/src/main/res/layout/cp_activity_city_list.xml rename to citypicker/src/main/res/layout/cp_fragment_city_list.xml index b15046d..e4b9764 100644 --- a/citypicker/src/main/res/layout/cp_activity_city_list.xml +++ b/citypicker/src/main/res/layout/cp_fragment_city_list.xml @@ -3,11 +3,9 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" + android:clickable="true" android:orientation="vertical" android:background="@color/cp_content_bg"> - - - - diff --git a/demo/src/main/java/com/zaaach/citypickerdemo/CityPickerActivity.java b/demo/src/main/java/com/zaaach/citypickerdemo/CityPickerActivity.java new file mode 100644 index 0000000..632ef30 --- /dev/null +++ b/demo/src/main/java/com/zaaach/citypickerdemo/CityPickerActivity.java @@ -0,0 +1,75 @@ +package com.zaaach.citypickerdemo; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v7.app.AppCompatActivity; +import android.widget.FrameLayout; +import android.widget.Toast; + +import com.amap.api.location.AMapLocation; +import com.amap.api.location.AMapLocationClient; +import com.amap.api.location.AMapLocationListener; +import com.zaaach.citypicker.CityPickerFragment; +import com.zaaach.citypicker.model.LocateState; +import com.zaaach.citypicker.utils.StringUtils; + +/** + * Created by rhm on 2017/8/26. + */ + +public class CityPickerActivity extends AppCompatActivity { + + private FrameLayout flCityPickerContainer; + private CityPickerFragment cityPickerFragment; + //声明AMapLocationClient类对象 + public AMapLocationClient mLocationClient = null; + //声明定位回调监听器 + public AMapLocationListener mLocationListener; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_city_picker); + initView(); + } + + private void initView() { + cityPickerFragment = new CityPickerFragment(); + getSupportFragmentManager().beginTransaction() + .add(R.id.fl_activity_city_container, cityPickerFragment).commit(); + initLocationListener(); + //初始化定位 + mLocationClient = new AMapLocationClient(getApplicationContext()); + //设置定位回调监听 + mLocationClient.setLocationListener(mLocationListener); + //启动定位 + mLocationClient.startLocation(); + + } + + private void initLocationListener() { + mLocationListener = new AMapLocationListener() { + @Override + public void onLocationChanged(AMapLocation aMapLocation) { + if (aMapLocation.getErrorCode() == 0) { + String city = aMapLocation.getCity(); + String district = aMapLocation.getDistrict(); + String location = StringUtils.extractLocation(city, district); + Toast.makeText(CityPickerActivity.this,city,Toast.LENGTH_SHORT).show(); + //定位成功,更新状态 + cityPickerFragment.updateLocateState(LocateState.SUCCESS, location.replaceAll("市", "")); + } else { + cityPickerFragment.updateLocateState(LocateState.FAILED, null); + } + } + }; + } + + @Override + protected void onDestroy() { + super.onDestroy(); + //销毁定位 + mLocationClient.stopLocation(); + } +} + diff --git a/demo/src/main/java/com/zaaach/citypickerdemo/MainActivity.java b/demo/src/main/java/com/zaaach/citypickerdemo/MainActivity.java index 6ea0335..936283d 100644 --- a/demo/src/main/java/com/zaaach/citypickerdemo/MainActivity.java +++ b/demo/src/main/java/com/zaaach/citypickerdemo/MainActivity.java @@ -6,7 +6,8 @@ import android.view.View; import android.widget.TextView; -import com.zaaach.citypicker.CityPickerActivity; +import com.zaaach.citypicker.CityPickerFragment; + public class MainActivity extends AppCompatActivity { private static final int REQUEST_CODE_PICK_CITY = 233; @@ -31,7 +32,7 @@ public void onClick(View v) { protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_PICK_CITY && resultCode == RESULT_OK){ if (data != null){ - String city = data.getStringExtra(CityPickerActivity.KEY_PICKED_CITY); + String city = data.getStringExtra(CityPickerFragment.KEY_PICKED_CITY); resultTV.setText("当前选择:" + city); } } diff --git a/demo/src/main/res/layout/activity_city_picker.xml b/demo/src/main/res/layout/activity_city_picker.xml new file mode 100644 index 0000000..326d066 --- /dev/null +++ b/demo/src/main/res/layout/activity_city_picker.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 04e285f..bfe5893 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Dec 28 10:00:20 PST 2015 +#Sat Aug 26 23:46:30 CST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip From 79b3a6dc150fb465d5c01c4c1906dba041690d28 Mon Sep 17 00:00:00 2001 From: rBrilliant <499953902@qq.com> Date: Sun, 27 Aug 2017 00:39:56 +0800 Subject: [PATCH 2/2] update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 73fe4b9..b7b33f6 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ 现在使用比较多的类似美团等APP的城市选择界面. -2步即可实现,就是这么简单粗暴! +3步即可实现,就是这么简单粗暴! # Gif