package com.mark;
import java.lang.reflect.Field;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
/**
* @author Sodino E-mail:sodinoopen@hotmail.com
* @version Time:2011-5-1 下午03:22:00
*/
public class MainActivity extends Activity implements ListView.OnScrollListener, OnItemClickListener,
android.view.View.OnClickListener {
private Handler handler;
private DisapearThread disapearThread;
/** 标识List的滚动状态。 */
private int scrollState;
private ListAdapter listAdapter;
private ListView listMain;
private TextView txtOverlay;
private WindowManager windowManager;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
handler = new Handler();
// 初始化首字母悬浮提示框
txtOverlay = (TextView) LayoutInflater.from(this).inflate(R.layout.popup_char_hint, null);
txtOverlay.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) getSystemService(Context.WINDOW_SERVICE);
windowManager.addView(txtOverlay, lp);
// 初始化ListAdapter
listAdapter = new ListAdapter(this, stringArr, this);
listMain = (ListView) findViewById(R.id.listInfo);
listMain.setOnItemClickListener(this);
listMain.setOnScrollListener(this);
listMain.setAdapter(listAdapter);
changeFastScrollerDrawable(listMain);
disapearThread = new DisapearThread();
}
/** 更改指定ListView的快速滚动条图标。 */
private void changeFastScrollerDrawable(ListView list) {
try {
Field f = AbsListView.class.getDeclaredField("mFastScroller");
f.setAccessible(true);
Object obj = f.get(list);
f = f.getType().getDeclaredField("mThumbDrawable");
f.setAccessible(true);
Drawable drawable = (Drawable) f.get(obj);
drawable = getResources().getDrawable(R.drawable.fast_scroller_img);
f.set(obj, drawable);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/** ListView.OnScrollListener */
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
// 以中间的ListItem为标准项。
txtOverlay.setText(String.valueOf(stringArr[firstVisibleItem + (visibleItemCount >> 1)]
.charAt(0)));
}
/** ListView.OnScrollListener */
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.scrollState = scrollState;
if (scrollState == ListView.OnScrollListener.SCROLL_STATE_IDLE) {
handler.removeCallbacks(disapearThread);
// 提示延迟1.5s再消失
boolean bool = handler.postDelayed(disapearThread, 1500);
Log.d("ANDROID_INFO", "postDelayed=" + bool);
} else {
txtOverlay.setVisibility(View.VISIBLE);
}
}
/** OnItemClickListener */
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String personalName = stringArr[position];
String url = ListAdapter.URL_PREFIX + personalName.replace(" ", "%20");
showInfo(personalName, url);
}
/**
* View.OnClickListener <br/>
* 点击“咧牙”图片。<br/>
*/
public void onClick(View view) {
if (view instanceof ImageView) {
// "咧牙"图标
int position = ((Integer) view.getTag()).intValue();
ActionItem actionAdd = new ActionItem(getResources().getDrawable(R.drawable.icon_info),
"Info", this);
ActionItem actionWeb = new ActionItem(getResources().getDrawable(R.drawable.icon_web),
"Web", this);
ActionItem actionEMail = new ActionItem(getResources().getDrawable(
R.drawable.icon_email), "Email", this);
QuickActionBar qaBar = new QuickActionBar(view, position);
qaBar.setEnableActionsLayoutAnim(true);
qaBar.addActionItem(actionAdd);
qaBar.addActionItem(actionWeb);
qaBar.addActionItem(actionEMail);
qaBar.show();
} else if (view instanceof LinearLayout) {
// ActionItem组件
LinearLayout actionsLayout = (LinearLayout) view;
QuickActionBar bar = (QuickActionBar) actionsLayout.getTag();
bar.dismissQuickActionBar();
int listItemIdx = bar.getListItemIndex();
TextView txtView = (TextView) actionsLayout.findViewById(R.id.qa_actionItem_name);
String actionName = txtView.getText().toString();
String personalName = stringArr[listItemIdx];
String url = ListAdapter.URL_PREFIX + personalName.replace(" ", "%20");
if (actionName.equals("Info")) {
showInfo(personalName, url);
} else if (actionName.equals("Web")) {
go2Web(url);
} else if (actionName.equals("Email")) {
sendEMail(personalName, url);
}
}
}
public void showInfo(String name, String url) {
String content = "Name:" + name + "\n"//
+ "URL:" + url//
;
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.info_dialog);
dialog.setTitle("Personal Information");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText(content);
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.icon_info);
dialog.show();
}
private void go2Web(String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
private void sendEMail(String name, String url) {
Intent mailIntent = new Intent(Intent.ACTION_SEND);
// 设置邮件类型为纯文本,即为所有可见字符与一些简单的控制符的组合。
mailIntent.setType("plain/text");
// 收件人地址
String[] arrReceiver = { "sodinoopen@hotmail.com" };
// 抄送地址
String[] arrCc = { "sodinoopen@hotmail.com" };
// 密送地址
String[] arrBcc = { "sodinoopen@hotmail.com", "sodinoopen@hotmail.com" };
String mailSubject = "This is a mail send by list demo";
String mailBody = "Hello everyone, I think the my demo is very good, haha. thanks.";
// 指定要添加的附件的完整路径
String attachPath = "file:///sdcard/Test.apk";
mailIntent.putExtra(Intent.EXTRA_EMAIL, arrReceiver);
mailIntent.putExtra(Intent.EXTRA_CC, arrCc);
mailIntent.putExtra(Intent.EXTRA_BCC, arrBcc);
mailIntent.putExtra(Intent.EXTRA_SUBJECT, mailSubject);
mailIntent.putExtra(Intent.EXTRA_TEXT, mailBody);
mailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(attachPath));
// 过滤Intent并启动GMail界面,之后点击Send即可将邮件发送出去。
Intent finalIntent = Intent.createChooser(mailIntent, "Mail Sending...");
startActivity(finalIntent);
}
private class DisapearThread implements Runnable {
public void run() {
// 避免在1.5s内,用户再次拖动时提示框又执行隐藏命令。
if (scrollState == ListView.OnScrollListener.SCROLL_STATE_IDLE) {
txtOverlay.setVisibility(View.INVISIBLE);
}
}
}
public void onDestroy() {
super.onDestroy();
// 将txtOverlay删除。
txtOverlay.setVisibility(View.INVISIBLE);
windowManager.removeView(txtOverlay);
}
private String[] stringArr = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
"Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu",
"Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler", "Alverca",
- 1
- 2
前往页