android通讯录导航栏源码(一)

时间:2022-03-10 17:32:02

通讯录导航栏源码:

android通讯录导航栏源码(一)

1.activity

 package com.anna.contact.activity;

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import com.anna.contact.R;
import com.anna.contact.adapter.MyFriendsAdapter;
import com.anna.contact.entity.Friend;
import com.anna.contact.view.LetterlistViewForFriend;
import com.anna.contact.view.LetterlistViewForFriend.OnTouchingLetterChangedListener;
import com.iflytek.voiceads.AdError;
import com.iflytek.voiceads.AdKeys;
import com.iflytek.voiceads.IFLYAdListener;
import com.iflytek.voiceads.IFLYAdSize;
import com.iflytek.voiceads.IFLYFullScreenAd; import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; public class MyFriendsActivity extends Activity {
private IFLYFullScreenAd ad; ListView lv_friend;
String[] strings;// 存放存在的汉语拼音首字母
List<Friend> friends;
//右边字母导航
LetterlistViewForFriend letterlistViewForFriend;
HashMap<String, Integer> alphaIndex;// 存放存在的汉语拼音首字母和与之对应的列表位置
TextView overlay;
Handler handler;
OverlayThread overlayThread; // 隐藏字母提示框 @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
createAd();
setContentView(R.layout.vip_myactivity_myfriend);
overlayThread = new OverlayThread();
handler = new Handler();
/* 初始化右边导航 */
initOverlay();
/* 初始化通讯录 */
initData();
lv_friend = (ListView) this.findViewById(R.id.vip_myfriend_lv_1); lv_friend.setAdapter(new MyFriendsAdapter(MyFriendsActivity.this,
friends));
lv_friend.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub } }); letterlistViewForFriend=(LetterlistViewForFriend)this.findViewById(R.id.LetterlistViewForFriend);
letterlistViewForFriend.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() { @Override
public void onTouchingLetterChanged(String s) {
// TODO Auto-generated method stub
System.out.println(s);
int position = alphaIndex.get(s);
if (alphaIndex.get(s) != null) {
lv_friend.setSelection(position);
}
overlay.setText(s);
overlay.setVisibility(View.VISIBLE);
handler.removeCallbacks(overlayThread);
// 延迟一秒后执行,让overlay为不可见
handler.postDelayed(overlayThread, 1500);
}
}); }
// 初始化汉语拼音首字母弹出提示框
private void initOverlay() {
LayoutInflater inflater = LayoutInflater.from(this);
overlay = (TextView) inflater.inflate(R.layout.overlay_layout, null);
overlay.setVisibility(View.INVISIBLE);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT, 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);
} public class OverlayThread implements Runnable { @Override
public void run() {
// TODO Auto-generated method stub
overlay.setVisibility(View.INVISIBLE);
} } private void initData() {
List<String> list_data = new ArrayList<String>();
list_data.add("Alex");
list_data.add("Anna");
list_data.add("Blic");
list_data.add("Blpha");
list_data.add("Clex");
list_data.add("Clic");
list_data.add("Clpha");
list_data.add("Clex");
list_data.add("Dlic");
list_data.add("Dlpha");
list_data.add("Dace");
list_data.add("Dlpha");
list_data.add("Elic");
list_data.add("Elpha");
list_data.add("Eace");
list_data.add("Elpha");
list_data.add("Elea");
list_data.add("Flpha");
list_data.add("Flea");
list_data.add("Glpha");
list_data.add("Gace");
list_data.add("Glpha");
list_data.add("Hlea");
list_data.add("Ilpha");
list_data.add("Jace");
list_data.add("Jlpha");
list_data.add("Jenney");
list_data.add("Kitty");
list_data.add("Klpha");
list_data.add("Kath");
list_data.add("Lace");
list_data.add("Llpha");
list_data.add("Moon");
list_data.add("Merry");
list_data.add("Nice");
list_data.add("Object");
list_data.add("Purplegrape");
list_data.add("Queen");
list_data.add("Qearl");
list_data.add("Rose");
list_data.add("Rain");
list_data.add("Sunny");
list_data.add("Sweet");
list_data.add("Tiger");
list_data.add("Ternence");
list_data.add("Uea");
list_data.add("Uea");
list_data.add("Wendy");
list_data.add("Winnie");
list_data.add("Vivian");
list_data.add("Vicky");
list_data.add("Vanessa");
list_data.add("Yvette");
list_data.add("Yolanda");
list_data.add("Yvonne");
list_data.add("Zoey"); friends = new ArrayList<Friend>();
alphaIndex = new HashMap<String, Integer>(); for (int i = 0; i < list_data.size(); i++) {
Friend friend = new Friend();
friend.setName(list_data.get(i));
friends.add(friend);
}
strings = new String[friends.size()];
for (int i = 0; i < friends.size(); i++) {
char letter = friends.get(i).getName().substring(0, 1).charAt(0);
String currentLetter = (letter + "").toUpperCase();
// 上一个汉语拼音首字母,如果不存在为""
String lastLetter = (i - 1) > 0 ? (friends.get(i - 1).getName()
.substring(0, 1).charAt(0) + "").toUpperCase() : "";
if (!lastLetter.equals(currentLetter)) {
alphaIndex.put(currentLetter, i);
strings[i]=currentLetter;
} } } private void createAd() {
ad = IFLYFullScreenAd.createFullScreenAd(this,
"FDDE8F2023143C2E8702E80CE88B97CA");
ad.setAdSize(IFLYAdSize.FULLSCREEN);
ad.setParameter(AdKeys.SHOW_TIME_FULLSCREEN, "6000");
ad.setParameter(AdKeys.DOWNLOAD_ALERT, "true");
ad.loadAd(new IFLYAdListener() { @Override
public void onAdReceive() {
ad.showAd();
} @Override
public void onAdFailed(AdError arg0) {
Toast.makeText(
getApplicationContext(),
arg0.getErrorCode() + "****"
+ arg0.getErrorDescription(), 0).show(); } @Override
public void onAdClose() { } @Override
public void onAdClick() { } @Override
public void onAdExposure() {
// TODO Auto-generated method stub }
});
} @Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
WindowManager windowManager = (WindowManager) this
.getSystemService(Context.WINDOW_SERVICE);
windowManager.removeView(overlay);
} }

2.Adapter

 package com.anna.contact.adapter;

 import java.util.List;

 import com.anna.contact.R;
import com.anna.contact.entity.Friend; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView; public class MyFriendsAdapter extends BaseAdapter {
LayoutInflater inflater;
List<Friend> list; public MyFriendsAdapter(Context context, List<Friend> list) {
inflater = LayoutInflater.from(context);
this.list = list;
} @Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} @SuppressWarnings("null")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
viewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(
R.layout.vip_myactivity_myfriend_item, null);
holder = new viewHolder();
holder.vip_friend_letter = (TextView) convertView
.findViewById(R.id.vip_friend_letter);
holder.vip_friend_name = (TextView) convertView
.findViewById(R.id.vip_friend_item_name);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
} holder.vip_friend_name.setText(list.get(position).getName());
char letter = list.get(position).getName().substring(0, 1).charAt(0);
String currentLetter = (letter + "").toUpperCase(); String lastLetter = (position - 1) > 0 ? (list.get(position - 1)
.getName().substring(0, 1).charAt(0) + "") : "";
if (!lastLetter.equals(currentLetter)) {
holder.vip_friend_letter.setText(currentLetter);
holder.vip_friend_letter.setVisibility(View.VISIBLE);
} else {
holder.vip_friend_letter.setVisibility(View.GONE); } return convertView;
} public class viewHolder {
TextView vip_friend_letter;
ImageView vip_friend_logo;
TextView vip_friend_name;
} }

3.entity

 package com.anna.contact.entity;

 import android.widget.ImageView;

 public class Friend {
private String name;
private String letter;
private ImageView logo; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getLetter() {
return letter;
} public void setLetter(String letter) {
this.letter = letter;
} public ImageView getLogo() {
return logo;
} public void setLogo(ImageView logo) {
this.logo = logo;
} }

4.view

 package com.anna.contact.view;

 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; public class LetterlistViewForFriend extends View { public LetterlistViewForFriend(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
}
public LetterlistViewForFriend(Context context)
{
super(context);
} public LetterlistViewForFriend(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
} 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; @Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas); if (showBkg)
{
canvas.drawColor(Color.parseColor("#40000000"));
} int height = getHeight();
int width = getWidth();
int singleHeight = height / b.length;
for (int i = 0; i < b.length; i++)
{
//paint.setTextSize(19) ;
paint.setColor(Color.BLACK);
paint.setTypeface(Typeface.DEFAULT_BOLD); paint.setAntiAlias(true);
if (i == choose)
{
paint.setColor(Color.parseColor("#3399ff"));
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 = true;
if (oldChoose != c && listener != null)
{
if (c > 0 && c < b.length)
{
listener.onTouchingLetterChanged(b[c]);
choose = c;
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);
}
}

源码下载:https://yunpan.cn/cr9yS4D6RWwid访问密码 09fa