Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可自定义“热门城市”显示文字 #78

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ CityPicker.from(activity) //activity或者fragment
.setAnimationStyle(anim) //自定义动画
.setLocatedCity(new LocatedCity("杭州", "浙江", "101210101"))) //APP自身已定位的城市,传null会自动定位(默认)
.setHotCities(hotCities) //指定热门城市
.setHotCityText("热门城市") //指定“热门城市”位置显示文字
.setOnPickListener(new OnPickListener() {
@Override
public void onPick(int position, City data) {
Expand Down Expand Up @@ -206,4 +207,4 @@ OK,enjoy it~

# QQ群

有兴趣可以加入QQ群一起交流:601783167
有兴趣可以加入QQ群一起交流:601783167
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @Date: 2018/2/6 17:52
*/
public class CityPicker {

private static final String TAG = "CityPicker";

private WeakReference<FragmentActivity> mContext;
Expand All @@ -30,6 +31,7 @@ public class CityPicker {
private LocatedCity mLocation;
private List<HotCity> mHotCities;
private OnPickListener mOnPickListener;
public static String mHotCityText = "热门城市";

private CityPicker(){}

Expand Down Expand Up @@ -76,6 +78,11 @@ public CityPicker setLocatedCity(LocatedCity location) {
return this;
}

public CityPicker setHotCityText(String text) {
this.mHotCityText = text;
return this;
}

public CityPicker setHotCities(List<HotCity> data){
this.mHotCities = data;
return this;
Expand Down Expand Up @@ -113,6 +120,7 @@ public void show(){
CityPickerDialogFragment.newInstance(enableAnim);
cityPickerFragment.setLocatedCity(mLocation);
cityPickerFragment.setHotCities(mHotCities);
cityPickerFragment.setHotCityText(mHotCityText);
cityPickerFragment.setAnimationStyle(mAnimStyle);
cityPickerFragment.setOnPickListener(mOnPickListener);
cityPickerFragment.show(ft, TAG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class CityPickerDialogFragment extends DialogFragment implements TextWatc
private List<City> mAllCities;
private List<HotCity> mHotCities;
private List<City> mResults;
private String mHotCityText;

private DBManager dbManager;

Expand All @@ -76,10 +77,11 @@ public class CityPickerDialogFragment extends DialogFragment implements TextWatc

/**
* 获取实例
*
* @param enable 是否启用动画效果
* @return
*/
public static CityPickerDialogFragment newInstance(boolean enable){
public static CityPickerDialogFragment newInstance(boolean enable) {
final CityPickerDialogFragment fragment = new CityPickerDialogFragment();
Bundle args = new Bundle();
args.putBoolean("cp_enable_anim", enable);
Expand All @@ -93,18 +95,24 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
setStyle(STYLE_NORMAL, R.style.CityPickerStyle);
}

public void setLocatedCity(LocatedCity location){
public void setLocatedCity(LocatedCity location) {
mLocatedCity = location;
}

public void setHotCities(List<HotCity> data){
if (data != null && !data.isEmpty()){
public void setHotCities(List<HotCity> data) {
if (data != null && !data.isEmpty()) {
this.mHotCities = data;
}
}

public void setHotCityText(String text) {
if (!TextUtils.isEmpty(text)) {
this.mHotCityText = text;
}
}

@SuppressLint("ResourceType")
public void setAnimationStyle(@StyleRes int resId){
public void setAnimationStyle(@StyleRes int resId) {
this.mAnimStyle = resId <= 0 ? mAnimStyle : resId;
}

Expand Down Expand Up @@ -138,7 +146,7 @@ private void initViews() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//确保定位城市能正常刷新
if (newState == RecyclerView.SCROLL_STATE_IDLE){
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
mAdapter.refreshLocationItem();
}
}
Expand Down Expand Up @@ -184,17 +192,17 @@ private void initData() {
mHotCities.add(new HotCity("武汉", "湖北", "101200101"));
}
//初始化定位城市,默认为空时会自动回调定位
if (mLocatedCity == null){
if (mLocatedCity == null) {
mLocatedCity = new LocatedCity(getString(R.string.cp_locating), "未知", "0");
locateState = LocateState.LOCATING;
}else{
} else {
locateState = LocateState.SUCCESS;
}

dbManager = new DBManager(getActivity());
mAllCities = dbManager.getAllCities();
mAllCities.add(0, mLocatedCity);
mAllCities.add(1, new HotCity("热门城市", "未知", "0"));
mAllCities.add(1, new HotCity(mHotCityText, "未知", "0"));
mResults = mAllCities;
}

Expand All @@ -205,8 +213,8 @@ public void onStart() {
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK){
if (mOnPickListener != null){
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mOnPickListener != null) {
mOnPickListener.onCancel();
}
}
Expand All @@ -228,42 +236,46 @@ public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

//测量宽高
private void measure() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(dm);
height = dm.heightPixels;
width = dm.widthPixels;
}else{
} else {
DisplayMetrics dm = getResources().getDisplayMetrics();
height = dm.heightPixels;
width = dm.widthPixels;
}
}

/** 搜索框监听 */
/**
* 搜索框监听
*/
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
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)){
if (TextUtils.isEmpty(keyword)) {
mClearAllBtn.setVisibility(View.GONE);
mEmptyView.setVisibility(View.GONE);
mResults = mAllCities;
((SectionItemDecoration)(mRecyclerView.getItemDecorationAt(0))).setData(mResults);
((SectionItemDecoration) (mRecyclerView.getItemDecorationAt(0))).setData(mResults);
mAdapter.updateData(mResults);
}else {
} else {
mClearAllBtn.setVisibility(View.VISIBLE);
//开始数据库查找
mResults = dbManager.searchCity(keyword);
((SectionItemDecoration)(mRecyclerView.getItemDecorationAt(0))).setData(mResults);
if (mResults == null || mResults.isEmpty()){
((SectionItemDecoration) (mRecyclerView.getItemDecorationAt(0))).setData(mResults);
if (mResults == null || mResults.isEmpty()) {
mEmptyView.setVisibility(View.VISIBLE);
}else {
} else {
mEmptyView.setVisibility(View.GONE);
mAdapter.updateData(mResults);
}
Expand All @@ -276,10 +288,10 @@ public void onClick(View v) {
int id = v.getId();
if (id == R.id.cp_cancel) {
dismiss();
if (mOnPickListener != null){
if (mOnPickListener != null) {
mOnPickListener.onCancel();
}
}else if(id == R.id.cp_clear_all){
} else if (id == R.id.cp_clear_all) {
mSearchBox.setText("");
}
}
Expand All @@ -291,26 +303,26 @@ public void onIndexChanged(String index, int position) {
mAdapter.scrollToSection(index);
}

public void locationChanged(LocatedCity location, int state){
public void locationChanged(LocatedCity location, int state) {
mAdapter.updateLocateState(location, state);
}

@Override
public void dismiss(int position, City data) {
dismiss();
if (mOnPickListener != null){
if (mOnPickListener != null) {
mOnPickListener.onPick(position, data);
}
}

@Override
public void locate(){
if (mOnPickListener != null){
public void locate() {
if (mOnPickListener != null) {
mOnPickListener.onLocate();
}
}

public void setOnPickListener(OnPickListener listener){
public void setOnPickListener(OnPickListener listener) {
this.mOnPickListener = listener;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.widget.FrameLayout;
import android.widget.TextView;

import com.zaaach.citypicker.CityPicker;
import com.zaaach.citypicker.R;
import com.zaaach.citypicker.adapter.decoration.GridItemDecoration;
import com.zaaach.citypicker.model.City;
Expand Down Expand Up @@ -209,7 +210,7 @@ public int getItemCount() {
public int getItemViewType(int position) {
if (position == 0 && TextUtils.equals("定", mData.get(position).getSection().substring(0, 1)))
return VIEW_TYPE_LOCATION;
if (position == 1 && TextUtils.equals("热", mData.get(position).getSection().substring(0, 1)))
if (position == 1 && TextUtils.equals(CityPicker.mHotCityText.substring(0, 1), mData.get(position).getSection().substring(0, 1)))
return VIEW_TYPE_HOT;
return super.getItemViewType(position);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.text.TextUtils;

import com.zaaach.citypicker.CityPicker;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -36,7 +38,7 @@ public String getSection(){
return c.toUpperCase();
}
//在添加定位和热门数据时设置的section就是‘定’、’热‘开头
else if (TextUtils.equals(c, "定") || TextUtils.equals(c, "热"))
else if (TextUtils.equals(c, "定") || TextUtils.equals(c, CityPicker.mHotCityText.substring(0, 1)))
return pinyin;
else
return "#";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.zaaach.citypicker.model;

import com.zaaach.citypicker.CityPicker;

public class HotCity extends City {

public HotCity(String name, String province, String code) {
super(name, province, "热门城市", code);
super(name, province, CityPicker.mHotCityText, code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.view.View;
import android.widget.TextView;

import com.zaaach.citypicker.CityPicker;
import com.zaaach.citypicker.R;

import java.util.ArrayList;
Expand All @@ -21,7 +22,7 @@
* @Date: 2018/2/8 10:56
*/
public class SideIndexBar extends View{
private static final String[] DEFAULT_INDEX_ITEMS = {"定位", "热门", "A", "B", "C", "D", "E", "F", "G", "H",
private static final String[] DEFAULT_INDEX_ITEMS = {"定位", CityPicker.mHotCityText.substring(0, 2), "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"};

private List<String> mIndexItems;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void onClick(View v) {
.setAnimationStyle(anim)
.setLocatedCity(null)
.setHotCities(hotCities)
.setHotCityText("当前开通城市")
.setOnPickListener(new OnPickListener() {
@Override
public void onPick(int position, City data) {
Expand Down