SideBar 选择城市时右侧边上的 选择bar
需要定义一个SideBar的视图类 在布局文件中引用 同时在布局中设置一个textView默认不可见
当触摸时才显示 在调用的Activity中
sideBar.setOnTouchingLetterChangedListener方法
离开时不可见 同时联动listView listView.setSelection(i)设置联动;
package com.archie.lazyhousewifery.cityandauntdetail.makeview; import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView; import com.archie.lazyhousewifery.cityandauntdetail.Constants; public class SideBar extends View { public String[] characters = new String[] { "#", "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 Paint paint;
private int textSize = 16;
private int defaultTextColor = Color.parseColor("#D2D2D2");
private int selectedTextColor = Color.parseColor("#2DB7E1");
private int touchedBgColor = Color.parseColor("#F5F5F5");
private TextView text_dialog; private OnTouchingLetterChangedListener onTouchingLetterChangedListener; private int position = -1; public SideBar(Context context) {
super(context);
} public SideBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public SideBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} public void setTextDialog(TextView textView){
this.text_dialog = textView;
} private void init() {
paint = new Paint();
paint.setAntiAlias(true);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); int height = getHeight();
int width = getWidth();
int singleHeight = height / characters.length; for (int i = 0; i < characters.length; i++) {
if (i == position) {
paint.setColor(selectedTextColor);
} else {
paint.setColor(defaultTextColor);
}
paint.setTextSize(textSize); float xPos = width / 2 - paint.measureText(characters[i]) / 2;
float yPos = singleHeight * i + singleHeight;
canvas.drawText(characters[i], xPos, yPos, paint);
}
} @Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
float y = event.getY();
position = (int) (y / (getHeight() / characters.length));
if (position >= 0 && position < Constants.CITY_TYPE.length) {
onTouchingLetterChangedListener.onTouchingLetterChanged(position);
switch (action) {
case MotionEvent.ACTION_UP:
setBackgroundColor(Color.TRANSPARENT);
position = -1;
invalidate();
if (text_dialog != null) {
text_dialog.setVisibility(View.INVISIBLE);
}
break;
default:
setBackgroundColor(touchedBgColor);
invalidate();
text_dialog.setText(characters[position]);
break;
}
}else{ setBackgroundColor(Color.TRANSPARENT);
if (text_dialog != null) {
text_dialog.setVisibility(View.INVISIBLE);
} }
return true;
} public void setOnTouchingLetterChangedListener(
OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
} public interface OnTouchingLetterChangedListener {
public void onTouchingLetterChanged(int position);
} }
SideBar
private void initSideBar() { sideBar.setTextDialog(dialog_text);
sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() { @Override
public void onTouchingLetterChanged(int position) {
String city_label = Constants.CITY_TYPE[position];
for (int i = 0; i < cityList.size(); i++) {
if (cityList.get(i).getCityName().equals(city_label)) { cityListView.setSelection(i);
dialog_text.setVisibility(View.VISIBLE);
break;
}
if(i == cityList.size() -1){ dialog_text.setVisibility(View.INVISIBLE); }
}
}
}); }
上边的代码是在activity中引用sideBar 回调的方法 其他的稍作处理就可以运行了
SideBar 选择城市时右侧边上的 选择bar的更多相关文章
- 美团HD(5)-选择城市
DJSelectCityViewController.m #import "DJSelectCityViewController.h" #import "DJConsta ...
- h5手机端下拉选择城市
<!doctype html><html> <head> <meta http-equiv="Content-Type& ...
- XML:使用DOM技术解析xML文件中的城市,实现select级联选择
中国的城市xml格式:cities.xml <?xml version="1.0" encoding="utf-8"?> <china> ...
- jquery实现输入框聚焦,键盘上下键选择城市
在最近有个项目中 需要实现当文本框聚焦的时候,可以键盘上下键选择内容,按enter键的时候,把内容传到输入框中,如图所示: 实现代码如下: /** *输入框聚焦,键盘上下键选择城市 */ ;(func ...
- [WP8] Binding时,依照DataType选择DataTemplate
[WP8] Binding时,依照DataType选择DataTemplate 范例下载 范例程序代码:点此下载 问题情景 在开发WPF.WP8...这类应用程序的时候,透过Binding机制搭配Da ...
- ecshop添加商品选择品牌时如何按拼音排序
ECSHOP后台添加新商品时,有一个选择品牌的下拉框,如果品牌太多,在下拉框里查找起来很不方便. 我想给“下拉框里的品牌列表”按品牌名的拼音排序,比如有“中国水利出版社” “中国人民出版社” 这两个品 ...
- 每天一个JavaScript实例-动态省份选择城市
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Swift基础之实现选择图片时,出现类似于ActionSheet的样式
之前看到过有APP在选择图片时,调用手机相册时,将手机相册做成了左右滑动选择的效果,这次展示的就是这种样式,用OC语言已经有人实现过类似的代码,在这里写的仅仅是效果展示的代码调用,具体代码,可以自己研 ...
- jQuery 选择城市,显示对应的即时时区时间
因客户需要,我们CRM系统中,jQuery 弄个时区插件 如图: HTML: <div id="cityDate"> <i class="P_arrow ...
随机推荐
- JAVA_Package
Javaの名前空間の仕組みの1つにパッケージがあります.大規模開発では必須の概念です.また.他人の作ったコードの再利用という観点でも.パッケージを正しく活用する必要があります. ・完全修飾名:パッケー ...
- How to Pronounce Word vs. World
How to Pronounce Word vs. World Share Tweet Share Tagged With: Comparison, Dark L What is the differ ...
- Angular2学习笔记
Angular2 这里 Angular2 是指采用 TypeScript 语言的 Angular 2.0及以上版本.与采用 JavaScript 语言的 AngularJS 相比,Angular2 不 ...
- Python操作远程服务器paramiko模块介绍
paramiko模块是基于Python实现的SSH远程安全连接,可以提供在远程服务器上执行命令.上传文件到服务器或者从指定服务器下载文件的功能. paramiko模块安装方法 paramiko模块不是 ...
- ckeditor使用说明
2015-08-17 15:42热心网友最快回答 一.使用方法:1.在页面<head>中引入ckeditor核心文件ckeditor.js<script type="t ...
- Webservice之axis
根据wsdl的url,使用axis1.4生成客户端,并且对webservice进行调用 1.到www.apache.org上去下载axis-bin-1_4.zip,如要关联源代码就把axis-sr ...
- Java文件上传:Restful接口接收上传文件,缓存在本地
接口代码 import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.PostMapping; i ...
- yii 缓存的使用 以及使用需要开启php的apc扩展
public function behaviors() { return [ [ 'class' => 'yii\filters\PageCache', 'only' => ['index ...
- apache启动报错(98)Address already in use: make_sock: could not bind to address [::]:80
说明80端口被用 终端: ps -ef|grep httpd察看占用的进程或者用netstat -lnp|grep 80 找到后kill掉,如果都不行那么再试试以下方法(试过可以) 终端输入: fi ...
- The APK failed to install. Error:Could not parse error string.
问题一: The APK failed to install. Error:Could not parse error string. 今天拖拽自己的apk到模拟器上运行,报上述错误. 搜索解决方案. ...