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
android通讯录导航栏源码(一)的更多相关文章
- Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程
转载请注明出处 http://www.cnblogs.com/crashmaker/p/3549365.html From crash_coder linguowu linguowu0622@gami ...
- Android音乐播放器源码(歌词.均衡器.收藏.qq5.0菜单.通知)
一款Android音乐播放器源码,基本功能都实现了 qq5.0菜单(歌词.均衡器.收藏.qq5.0菜单.通知) 只有向右滑动出现,菜单键和指定按钮都还没有添加. 源码下载:http://code.66 ...
- Ubuntu 14.04 LTS 下 android 2.3.5 源码编译过程
Ubuntu 14.04 LTS 下 android 2.3.5 源码编译过程 在新的Ubuntu 64位系统下去编译早期的安卓源码是会出现很多问题的,因为64位系统在安装完成后,很多32位的兼容 ...
- android版猜拳游戏源码分享
android版猜拳游戏源码分享安卓版猜拳游戏源码,该文件中带有安装测试包的,这个游戏源码比较简单的,现在有两个代码,一个自定义VIEW的,一个就是普通的imageView图片,游戏非常适合一些新手的 ...
- 一款非常简单的android音乐播放器源码分享给大家
一款非常简单的android音乐播放器源码分享给大家,该应用虽然很小,大家常用的播放器功能基本实现了,可能有点还不够完善,大家也可以自己完善一下,源码在源码天堂那里已经有了,大家可以到那里下载学习吧. ...
- Android底部导航栏——FrameLayout + RadioGroup
原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6285881.html Android底部导航栏有多种实现方式,本文详细介绍FrameLayout ...
- Android底部导航栏创建——ViewPager + RadioGroup
原创文章,引用请注明出处:http://www.cnblogs.com/baipengzhan/p/6270201.html Android底部导航栏有多种实现方式,本文详解其中的ViewPager ...
- Android -- 带你从源码角度领悟Dagger2入门到放弃
1,以前的博客也写了两篇关于Dagger2,但是感觉自己使用的时候还是云里雾里的,更不谈各位来看博客的同学了,所以今天打算和大家再一次的入坑试试,最后一次了,保证最后一次了. 2,接入项目 在项目的G ...
- Android -- 带你从源码角度领悟Dagger2入门到放弃(二)
1,接着我们上一篇继续介绍,在上一篇我们介绍了简单的@Inject和@Component的结合使用,现在我们继续以老师和学生的例子,我们知道学生上课的时候都会有书籍来辅助听课,先来看看我们之前的Stu ...
随机推荐
- 【极客学院出品】Cocos2d-X系列课程之九-BOX2D物理引擎
Cocos2d-x 是时下最热门的手游引擎,在国内和国外手机游戏开发使用的份额各自是70%和25%,在App Store的top10中,有7个是用它开发的. 本节课程为Cocos2d-x系列课程之九, ...
- apache 三种工作模式的讲解
Apache 2.X 支持插入式并行处理模块,称为多路处理模块(MPM).在编译apache时必须选择也只能选择一个MPM,对类UNIX系统,有几个不同的MPM可供选择,它们会影响到apache的速 ...
- null值的判断
select * from Students where Address IS null --判断address是nulselect * from Students where Address is ...
- 创建mvc
有几个界面就建几个文件夹 每个文件夹中都有三个文件夹,(models,Controllers,views) 创建一个common 和一个Base文件夹(先建文件夹,可以直接拉进去) common的目的 ...
- U - 神、上帝以及老天爷(第二季水)
Description HDU 2006'10 ACM contest的颁奖晚会隆重开始了! 为了活跃气氛,组织者举行了一个别开生面.奖品丰厚的抽奖活动,这个活动的具体要求是这样的: ...
- (原)STL中vector的疑问
以前基本上没有用过STL,当然包括里面的vector.今天试验了一下. 主要看了这个网址: http://blog.csdn.net/phoebin/article/details/3864590 代 ...
- 2015.4.16-C#中ref和out的区别
如图: 输出结果是: 上面显示的是 ref 只是地址传递,所以最初改变的也只是地址,但是如果 在给其赋值,值会随之改变;如果 在方法内直接赋值,那么输出的结果 就是现在的值,之后 ...
- spring下配置dbcp,c3p0,proxool[转]
不管通过何种持久化技术,都必须通过数据连接访问数据库,在Spring中,数据连接是通过数据源获得的.在以往的应用中,数据源一般是Web应用服务器提供的.在Spring中,你不但可以通过JNDI获取应用 ...
- ButterKnife你需要知道的点
ButterKnife,这个方便findViewById而使用的库,无数的应用在使用它,以注解来实现的他,对应用的性能影响几乎可以忽略,可以放心使用. 使用: compile 'com.jakewha ...
- php 数组 array_intersect_key() array_unique()移除重复
<?php // array_unique($array) 去除重复 // array_unshif()向数组的顶部追加函数 // array_shif($a,"ss")向数 ...