Android 仿通讯录A-Z侧边索引查询 LetterListView androidstudio
2016-05-25 16:06
676 查看
Android 仿通讯录A-Z侧边索引查询 LetterListView androidstudio
实现步骤:1.自定义一个名叫LetterListView 的View。
2.在布局文件中加入这个自定义的View。
3. 重写listview Adapter
4. 在Activity中处理监听事件。
一、自定义 LetterListView 显示A-Z索引
package qqt.com.letterlist; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Typeface; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * Created by joy on 2016/5/25. */ public class LetterListView extends View { OnTouchingLetterChangedListener onTouchingLetterChangedListener; 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" }; int choose = -1; Paint paint = new Paint(); boolean showBkg = false; public LetterListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public LetterListView(Context context, AttributeSet attrs) { super(context, attrs); } public LetterListView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int height = getHeight(); int width = getWidth(); int singleHeight = height / b.length; //取消下面注释,点击背景变色 // if (showBkg) { // // 画出背景 // canvas.drawColor(Color.parseColor("#55000000")); // } for (int i = 0; i < b.length; i++) { paint.setColor(Color.WHITE); paint.setTypeface(Typeface.DEFAULT_BOLD); paint.setTextSize(28); paint.setAntiAlias(true); if (i == choose) { paint.setColor(getResources().getColor(R.color.colorAccent)); paint.setTextSize(30); paint.setFakeBoldText(true); } float xPos = width / 2 - paint.measureText(b[i]) / 2; float yPos = singleHeight * i + singleHeight; 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(); final int oldChoose = choose; final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener; final int c = (int) (y / getHeight() * b.length); switch (action) { case MotionEvent.ACTION_DOWN: showBkg = true; if (oldChoose != c && listener != null) { if (c > 0 && c < b.length) { listener.onTouchingLetterChanged(b[c]); choose = c; invalidate(); } } break; case MotionEvent.ACTION_MOVE: if (oldChoose != c && listener != null) { if (c > 0 && c < b.length) { listener.onTouchingLetterChanged(b[c]); choose = c; invalidate(); } } break; case MotionEvent.ACTION_UP: showBkg = false; choose = -1; invalidate(); break; } return true; } @Override public boolean onTouchEvent(MotionEvent event) { return super.onTouchEvent(event); } public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener onTouchingLetterChangedListener) { this.onTouchingLetterChangedListener = onTouchingLetterChangedListener; } public interface OnTouchingLetterChangedListener { public void onTouchingLetterChanged(String s); } }
二、布局文件引用LetterListView
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="40dp" android:background="@color/colorBlue" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height e892 ="match_parent" android:layout_weight="1" android:gravity="center" android:text="LetterListView" android:textColor="@color/colorWhite" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/editcity" android:layout_width="fill_parent" android:layout_height="50dp" android:layout_marginTop="5dp" android:paddingLeft="10dp" android:textSize="20sp"></EditText> </LinearLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:orientation="vertical"> <ListView android:id="@+id/lvlist" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:scrollbars="none" /> <qqt.com.letterlist.LetterListView android:id="@+id/ltlvlist" android:layout_width="30dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:background="@color/colorGrayDeep"/> </RelativeLayout> </LinearLayout> </LinearLayout>
三、重写Adapter
package qqt.com.letterlist; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.HashMap; import java.util.List; /** * Created by joy on 2016/5/25. */ public class MyAdapter extends BaseAdapter { public static HashMap hmAlpha; public static String[] sections; private LayoutInflater inflater; private List<City> listCitys; public MyAdapter(Context context, List<City> mCitys) { this.inflater = LayoutInflater.from(context); this.listCitys = mCitys; hmAlpha=new HashMap<String,Integer>(); sections=new String[mCitys.size()]; for (int i = 0; i < mCitys.size(); i++) { // 当前汉语拼音首字母 String currentStr = mCitys.get(i).getCitySzm(); // 上一个汉语拼音首字母,如果不存在为“ ” String previewStr = i>= 1? mCitys.get(i - 1) .getCitySzm() : " "; if (!previewStr.equals(currentStr)) { String name = mCitys.get(i).getCitySzm(); hmAlpha.put(name, i); sections[i] = name; } } } @Override public int getCount() { return listCitys.size(); } @Override public Object getItem(int position) { return listCitys.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ComViewHolder holder; if (convertView == null) { convertView = inflater.inflate(R.layout.item_letterlist, null); holder = new ComViewHolder(); holder.alpha = (TextView) convertView.findViewById(R.id.alpha); holder.name = (TextView) convertView.findViewById(R.id.name); convertView.setTag(holder); } else { holder = (ComViewHolder) convertView.getTag(); } holder.name.setText(listCitys.get(position).getCityname()); String currentStr = listCitys.get(position).getCitySzm(); String previewStr = position >=1 ? listCitys.get(position - 1) .getCitySzm() : " "; if (!previewStr.equals(currentStr)) { holder.alpha.setVisibility(View.VISIBLE); holder.alpha.setText(currentStr); } else { holder.alpha.setVisibility(View.GONE); } return convertView; } private class ComViewHolder { TextView alpha; TextView name; } }
四、Activity代码处理
package qqt.com.letterlist; import android.app.ActionBar; import android.content.Context; import android.graphics.PixelFormat; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import org.json.JSONArray; import org.json.JSONObject; import java.io.InputStream; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { ListView lvlist; EditText editcity; TextView overlay; LetterListView ltlvlist; Handler handler; OverlayRunnable overlayRunnable; List<City> citys; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); handler = new Handler(); overlayRunnable = new OverlayRunnable(); lvlist = (ListView) findViewById(R.id.lvlist); editcity = (EditText) findViewById(R.id.editcity); ltlvlist = (LetterListView) findViewById(R.id.ltlvlist); editcity.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) { //查询 重新绑定数据。。。 } }); //右侧索引 ltlvlist.setOnTouchingLetterChangedListener(new LetterListView.OnTouchingLetterChangedListener() { @Override public void onTouchingLetterChanged(String s) { try { if (MyAdapter.hmAlpha.get(s) != null) { int position = (int) MyAdapter.hmAlpha.get(s); lvlist.setSelection(position); overlay.setText(MyAdapter.sections[position]); overlay.setVisibility(View.VISIBLE); handler.postDelayed(overlayRunnable, 1500); } } catch (Exception e) { // TODO: handle exception } } }); initOverlay(); citys = new ArrayList<City>(); //获取数据 try { String jsonStr = readLocalJson(this, "city.json"); JSONObject jsonCity = new JSONObject(jsonStr); JSONArray jsonCitys = jsonCity.getJSONArray("citys"); for (int i = 0; i < jsonCitys.length(); i++) { JSONObject temp = jsonCitys.getJSONObject(i); City city = new City(); city.setCityname(temp.getString("name")); city.setCitypinyin(temp.getString("pinyin")); city.setCitySzm(temp.getString("pinyin").substring(0, 1)); citys.add(city); } MyAdapter adapter = new MyAdapter(this, citys); lvlist.setAdapter(adapter); } catch (Exception e) { } } private class OverlayRunnable implements Runnable { @Override public void run() { overlay.setVisibility(View.GONE); } } // 初始化汉语拼音首字母弹出提示框 private void initOverlay() { LayoutInflater inflater = LayoutInflater.from(this); overlay = (TextView) inflater.inflate(R.layout.overlay, null); overlay.setVisibility(View.INVISIBLE); WindowManager.LayoutParams lp = new WindowManager.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT); WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); windowManager.addView(overlay, lp); } //读取json数据 public static String readLocalJson(Context context, String fileName) { String jsonString = ""; String resultString = ""; try { InputStream inputStream = context.getResources().getAssets().open(fileName); byte[] buffer = new byte[inputStream.available()]; inputStream.read(buffer); resultString = new String(buffer, "UTF-8"); } catch (Exception e) { // TODO: handle exception } return resultString; } }
源码地址:http://download.csdn.net/detail/yangfan106/9530984
相关文章推荐
- android列表收缩与展开仿QQ好友列表(非常详细,附源码)
- Android中github上的开源工程
- Android基础知识之JVM && Dalvik && ART
- Android学习笔记四大组件之Service
- Android项目实战之仿网易顶部导航栏效果
- Android多状态组合之位运算(BitMask)
- Android Volley框架的几种post提交请求方式
- Android学习笔记四大组件之Activity
- AndroidStudio新版配置手册
- android eclipese 和源码SDk上集成成第三方jar包及.so动态库
- Android中事件处理机制---完
- Android中Bitmap和Drawable的转换
- Android 反编译工具使用
- [Android基础系列]设计模式(二)
- Android 动态修改TextView 颜色Color
- 一个android事件分发的简单Demo
- RecyclerView学习笔记
- android 上传图片过大处理
- 【Android开源项目分析】自定义圆形头像CircleImageView的使用和源码分析
- android 6.0 权限