package com.example.picscrollview;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.example.picscrollview.PickerScrollView.onSelectListener;
import com.example.test.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Boolean state_pressed = true;
private String cities[] ={"北京版", "天津版", "上海版", "重庆版", "河北版", "山西版", "辽宁版", "吉林版", "黑龙江版", "江苏版", "浙江版", "安徽版",
"福建版", "江西版", "山东版", "河南版", "湖北版", "湖南版", "广东版", "甘肃版", "四川版", "贵州版", "海南版", "云南版",
"青海版", "陕西版", "广西版", "西藏版", "宁夏版", "新疆版", "内蒙古版", "澳门版", "香港版", "台湾版","全国版"};
private String[] id = { "1", "2", "3", "4", "5", "6" ,"7","8","9","10","11","12","13","14","15","16",
"17", "18", "19", "20", "21", "22" ,"23","24","25","26","27","28","29","30","31","32","33","34","35"};
private PickerScrollView pickerscrlllview; // 滚动选择器
private List<Pickers> list; // 滚动选择器数据
private CharacterParser mCharacterParser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
public void initView() {
LetterSideBar letterSideBar = (LetterSideBar) findViewById(R.id.cs_letter_sb);
letterSideBar
.setOnTouchingLetterChangedListener(new LetterSideBar.OnTouchingLetterChangedListener() {
@Override
public void onTouchingLetterChanged(String letter) {
int jumpPos = getPositionForSection(letter.charAt(0));
pickerscrlllview.setSelected(jumpPos);
pickerscrlllview = (PickerScrollView) findViewById(R.id.pickerscrlllview);
pickerscrlllview.setOnSelectListener(pickerListener);
findViewById(R.id.location_confirm_btn).setOnClickListener(this);
private void initData() {
mCharacterParser = CharacterParser.getInstance();
list = filledData();
PinyinComparator pinyinComparator = new PinyinComparator();
Collections.sort(list, pinyinComparator);
pickerscrlllview.setData(list);
pickerscrlllview.setSelected(0);
@Override
protected void onResume() {
super.onResume();
public void onPause() {
super.onPause();
private List<Pickers> filledData() {
List<Pickers> sortList = new ArrayList<Pickers>();
for (int i = 0, n = cities.length; i < n; i++) {
Pickers picker = new Pickers();
picker.setShowConetnt(cities[i]);
picker.setShowId(id[i]);
String pinyin = mCharacterParser.getSelling(cities[i]);
picker.setPinyin(pinyin);
String sortString = pinyin.substring(0, 1).toUpperCase();
if (sortString.matches("[A-Z]")) {
picker.setSortLetter(sortString.toUpperCase());
} else {
picker.setSortLetter("#");
sortList.add(picker);
return sortList;
public int getPositionForSection(int section) {
for (int i = 0; i < list.size(); i++) {
String sortStr = list.get(i).getSortLetter();
char firstChar = sortStr.toUpperCase().charAt(0);
if (firstChar == section) {
return i;
return -1;
onSelectListener pickerListener = new onSelectListener() {
@Override
public void onSelect(Pickers pickers) {
Toast.makeText(MainActivity.this, pickers.getShowConetnt(), Toast.LENGTH_LONG).show();
@Override
public void onBackPressed() {
super.onBackPressed();
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
}