说明:最近碰到一个需求,弄一个类似国家或省份列表,样式参照微信联系人
文件列表:
step1:主界面 加载列表数据~\app\src\main\java\com\example\iosdialogdemo\MainActivity.java
step2:右侧列表数据排序~\app\src\com\example\iosdialogdemo\CountryPinyinComparator.java
step3:适配器~\app\src\main\java\com\example\iosdialogdemo\CountryAdapter.java
step4:地区bean类~\app\src\main\java\com\example\iosdialogdemo\Country.java
step5:自定义控件~\app\src\main\java\com\example\iosdialogdemo\SideBar.java
step6:item布局~\app\src\main\res\layout\item_country.xml
step7:主界面布局ui~\app\src\main\res\layout\activity_main.xml
效果图:
step1:~\app\src\main\java\com\example\iosdialogdemo\MainActivity.java
package com.example.iosdialogdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
/**
* 国家代码
* Created by donkor
*/
public class MainActivity extends Activity {
private SideBar sideBar;
private TextView dialog;
private ListView sortListView;
private CountryAdapter countryAdapter;
private Button btnBack;
private ArrayList<Country> countryList;
/**
* 根据拼音来排列ListView里面的数据类
*/
private CountryPinyinComparator pinyinComparator;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
pinyinComparator = new CountryPinyinComparator();
sideBar = (SideBar) findViewById(R.id.sidrbar);
dialog = (TextView) findViewById(R.id.dialog);
sideBar.setTextView(dialog);
sortListView = (ListView) findViewById(R.id.country_lvcountry);
btnBack = (Button) findViewById(R.id.btnBack);
countryList = new ArrayList<>();
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.finish();
}
});
/*
public Country(String country, String countryCode, String sortLetters) {
*
* */
countryList.add(new Country("美国","1001","A"));
countryList.add(new Country("宋国","1001","S"));
countryList.add(new Country("赵国","1001","Z"));
countryList.add(new Country("扶余国","1001","F"));
countryList.add(new Country("夜郎国","1001","Y"));
countryList.add(new Country("天启国","1001","T"));
countryList.add(new Country("启明国","1001","Q"));
countryList.add(new Country("俄国","1001","E"));
countryList.add(new Country("英吉利国","1001","Y"));
countryList.add(new Country("法兰西国","1001","F"));
countryList.add(new Country("西班牙国","1001","X"));
countryList.add(new Country("葡萄牙国","1001","P"));
countryList.add(new Country("匈牙利国","1001","X"));
countryList.add(new Country("塞尔维亚国","1001","S"));
countryList.add(new Country("索马里国","1001","S"));
countryList.add(new Country("埃及国","1001","A"));
countryList.add(new Country("苏丹国","1001","S"));
countryList.add(new Country("哈萨克国","1001","H"));
countryList.add(new Country("伊朗国","1001","Y"));
// Log.e("asd", "zone.size(): " + zone.size());
// 根据a-z进行排序源数据
Collections.sort(countryList, pinyinComparator);
// adapter = new SortAdapter(getActivity(), SourceDateList);
countryAdapter = new CountryAdapter(MainActivity.this, countryList);
sortListView.setAdapter(countryAdapter);
// 设置右侧触摸监听
sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
@Override
public void onTouchingLetterChanged(String s) {
//该字母首次出现的位置
int position = countryAdapter.getPositionForSection(s.charAt(0));
if (position != -1) {
sortListView.setSelection(position);
}
}
});
sortListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent mIntent = new Intent(MainActivity.this, MainActivity.class);
Bundle b = new Bundle();
String country = countryList.get(position).getCountry();
String countryCode = countryList.get(position).getCountryCode().replace("+","");
b.putString("country", country);
b.putString("countryCode", countryCode);
mIntent.putExtras(b);
MainActivity.this.setResult(1, mIntent);
MainActivity.this.finish();
}
});
}
}
step2:D:~\app\src\main\java\com\example\iosdialogdemo\CountryPinyinComparator.java
package com.example.iosdialogdemo;
import java.util.Comparator;
public class CountryPinyinComparator implements Comparator<Country> {
public int compare(Country o1, Country o2) {
if (o1.getSortLetters().equals("@")
|| o2.getSortLetters().equals("#")) {
return -1;
} else if (o1.getSortLetters().equals("#")
|| o2.getSortLetters().equals("@")) {
return 1;
} else {
return o1.getSortLetters().compareTo(o2.getSortLetters());
}
}
}
step3:~\app\src\main\java\com\example\iosdialogdemo\CountryAdapter.java
package com.example.iosdialogdemo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;
import java.util.List;
public class CountryAdapter extends BaseAdapter implements SectionIndexer {
private List<Country> list = null;
private Context mContext;
public CountryAdapter(Context mContext, List<Country> list) {
this.mContext = mContext;
this.list = list;
}
/**
* 当ListView数据发生变化时,调用此方法来更新ListView
*
* @param list
*/
public void updateListView(List<Country> list) {
this.list = list;
notifyDataSetChanged();
}
public int getCount() {
return this.list.size();
}
public Object getItem(int position) {
return list.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup arg2) {
/**得到手机通讯录联系人信息**/
ViewHolder viewHolder;
Country mContent=list.get(position);
if (view == null) {
viewHolder = new ViewHolder();
view = LayoutInflater.from(mContext).inflate(R.layout.item_country, null);
viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog);
viewHolder.number = (TextView) view.findViewById(R.id.number);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
//根据position获取分类的首字母的Char ascii值
int section = getSectionForPosition(position);
//如果当前位置等于该分类首字母的Char的位置 ,则认为是第一次出现
if (position == getPositionForSection(section)) {
viewHolder.tvLetter.setVisibility(View.VISIBLE);
viewHolder.tvLetter.setText(mContent.getSortLetters());
} else {
viewHolder.tvLetter.setVisibility(View.GONE);
}
viewHolder.tvTitle.setText(this.list.get(position).getCountry());
viewHolder.number.setText(this.list.get(position).getCountryCode());
return view;
}
final static class ViewHolder {
TextView tvLetter;
TextView tvTitle;
TextView number;
}
/**
* 根据ListView的当前位置获取分类的首字母的Char ascii值
*/
public int getSectionForPosition(int position) {
return list.get(position).getSortLetters().charAt(0);
}
/**
* 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
*/
public int getPositionForSection(int section) {
for (int i = 0; i < getCount(); i++) {
String sortStr = list.get(i).getSortLetters();
char firstChar = sortStr.toUpperCase().charAt(0);
if (firstChar == section) {
return i;
}
}
return -1;
}
@Override
public Object[] getSections() {
return null;
}
}
step4:D:~\app\src\main\java\com\example\iosdialogdemo\Country.java
package com.example.iosdialogdemo;
/**
* 城市 与城市代码
*/
public class Country {
private String country;
private String countryCode;
private String sortLetters; //显示数据拼音的首字母
public Country(String country, String countryCode, String sortLetters) {
this.country = country;
this.countryCode = countryCode;
this.sortLetters = sortLetters;
}
public Country() {
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getSortLetters() {
return sortLetters;
}
public void setSortLetters(String sortLetters) {
this.sortLetters = sortLetters;
}
}
step5:~\app\src\main\java\com\example\iosdialogdemo\SideBar.java
package com.example.iosdialogdemo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class SideBar extends View {
// 触摸事件
private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
// 26个字母
public static String[] b = {"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 int choose = -1;// 选中
private Paint paint = new Paint();
private TextView mTextDialog;
public SideBar(Context context) {
super(context);
}
public SideBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SideBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setTextView(TextView mTextDialog) {
this.mTextDialog = mTextDialog;
}
/**
* 重写这个方法
*/
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 获取焦点改变背景颜色.
int height = getHeight();// 获取对应高度
int width = getWidth(); // 获取对应宽度
int singleHeight = height / b.length;// 获取每一个字母的高度
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels; // 屏幕宽(像素,如:480px)
int screenHeight = dm.heightPixels; // 屏幕高(像素,如:800px)
for (int i = 0; i < b.length; i++) {
paint.setColor(Color.rgb(11,181,94));
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setAntiAlias(true);
if (screenWidth == 720 && screenHeight == 1280) {
paint.setTextSize(22);
} else if (screenWidth == 1536 && screenHeight == 2560) {
paint.setTextSize(50);
} else {
paint.setTextSize(35);
}
float xPos = width / 2 - paint.measureText(b[i]) / 2;
float yPos = singleHeight * i + singleHeight;
// 选中的状态
if (i == choose) {
paint.setColor(Color.parseColor("#ffffff"));
paint.setFakeBoldText(true);
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
R.mipmap.ic_launcher);
canvas.drawBitmap(bitmap, width / 6, singleHeight * i + singleHeight / 5, paint);
}
// x坐标等于中间-字符串宽度的一半.
canvas.drawText(b[i], xPos, yPos, paint);
paint.reset();// 重置画笔
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final int action = event.getAction();
final float y = event.getY();// 点击y坐标
final int oldChoose = choose;
final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
final int c = (int) (y / getHeight() * b.length);// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.
switch (action) {
case MotionEvent.ACTION_UP:
setBackgroundDrawable(new ColorDrawable(0x00000000));
choose = -1;//
invalidate();
if (mTextDialog != null) {
mTextDialog.setVisibility(View.INVISIBLE);
}
break;
default:
// setBackgroundResource(R.mipmap.show_toast_bg);
if (oldChoose != c) {
if (c >= 0 && c < b.length) {
if (listener != null) {
listener.onTouchingLetterChanged(b[c]);
}
if (mTextDialog != null) {
mTextDialog.setText(b[c]);
mTextDialog.setVisibility(View.VISIBLE);
}
choose = c;
invalidate();
}
}
break;
}
return true;
}
/**
* 向外公开的方法
*
* @param onTouchingLetterChangedListener
*/
public void setOnTouchingLetterChangedListener(
OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
}
/**
* 接口
*
* @author coder
*/
public interface OnTouchingLetterChangedListener {
public void onTouchingLetterChanged(String s);
}
}
step6:~\app\src\main\res\layout\item_country.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/catalog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/titleBackground"
android:paddingBottom="5dp"
android:paddingLeft="15dp"
android:paddingTop="5dp"
android:text="A"
android:textColor="@android:color/black"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="horizontal"
android:paddingLeft="23dp"
android:paddingBottom="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="hhhh"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="hhhh"
android:layout_marginLeft="10dp"
android:textColor="@android:color/black"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
step7:~\app\src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/titleBackground"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:drawablePadding="10dp"
android:gravity="center|left"
android:paddingLeft="10dp"
android:text="返回"
android:textAllCaps="false"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_weight="1"
android:gravity="center"
android:text="国家"
android:textColor="@android:color/black"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.3dp"
android:background="@android:color/darker_gray" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/country_lvcountry"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scrollbars="none"
android:divider="@null"
android:fastScrollEnabled="true" />
<TextView
android:id="@+id/dialog"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="@mipmap/ic_launcher"
android:gravity="center"
android:textColor="#ffffffff"
android:textSize="30sp"
android:visibility="invisible" />
<com.example.iosdialogdemo.SideBar
android:id="@+id/sidrbar"
android:layout_width="33dp"
android:layout_height="match_parent"
android:layout_gravity="right|center" />
</FrameLayout>
</LinearLayout>
end