編輯:關於 Android 程式設計
類似美團首頁的下拉彈出菜單,是工程中經常遇到的控件。不同專案的菜單條目類型與數量各異,因此需要依實際情況填充不同內容。先寫個 demo 以備不時之需。既然每個專案的菜單樣式不同,就必須把容器和內容分離開來。

容器畫面用 PopupWindow 實現,在點擊指定控件後彈出,需要:1. 指定當前 window 的位置與大小;2. 指定 window 的出現方式;3. 若要求其他部分變暗,需指定變暗部分的高度。
內容需要填充到容器中,依不同資料類型與需求設定不同頁面填充,可定義為組合控件或一個 Holder。它需要提供控件填充方法與資料刷新兩個基本方法,同時暴露一個 View 的引用,這樣就能把這個 View 填充到任何想添加的位置。
BaseHolder:
public abstract class BaseHolder<T> {
private View mView;
public abstract View initView();
public abstract void refreshView(T info);
public BaseHolder() {
mView = initView();
mView.setTag(this);
}
public View getView() { return mView; }
}
假設按下按鍵後彈出 PopupWindow,我們可以繼承 Button、複寫其點擊邏輯,實現點擊按鍵在按鍵正下方彈出 PopupWindow 的效果。
public class PopMenuButton extends Button implements View.OnClickListener {
private Context mCtx;
private PopupWindow mPopupWindow;
private LinearLayout mPopupWindowView;
private View mRootView;
private View mShodowView;
private LinearLayout mContentView;
private int[] mLocation = new int[2];
public PopMenuButton(Context context, AttributeSet attrs) { super(context, attrs); mCtx = context; mRootView = this; }
public PopMenuButton(Context context) { super(context); mCtx = context; mRootView = this; }
@Override protected void onFinishInflate() { super.onFinishInflate(); initPopWindow(); }
public void setContentView(View view) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mContentView.addView(view, params);
}
private void initView() {
mPopupWindowView = (LinearLayout) View.inflate(mCtx, R.layout.popmenu_layout, null);
mContentView = (LinearLayout) mPopupWindowView.findViewById(R.id.rl_content);
mShodowView = mPopupWindowView.findViewById(R.id.rl_shodow);
mShodowView.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) { mPopupWindow.dismiss(); }
});
setOnClickListener(this);
}
private void initPopWindow() {
initView();
mRootView.getLocationOnScreen(mLocation);
}
@Override public void onClick(View v) {
if (mPopupWindow == null) {
int[] location = new int[2];
this.getLocationOnScreen(location);
int start = location[1] + this.getHeight() + 1;
int screenHeight = ((Activity) mCtx).getWindowManager().getDefaultDisplay().getHeight();
mPopupWindow = new PopupWindow(mPopupWindowView, ViewGroup.LayoutParams.MATCH_PARENT, screenHeight - start, false);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(0xb0000000));
mPopupWindow.setAnimationStyle(R.style.popupAnimation);
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);
}
if (mPopupWindow.isShowing()) {
mPopupWindow.dismiss();
} else {
int[] location1 = new int[2];
this.getLocationOnScreen(location1);
mPopupWindow.showAtLocation(mRootView, Gravity.NO_GRAVITY, 0, location1[1] + this.getHeight() + 1);
}
}
}
假設要彈出一個二級菜單,可以把邏輯封裝到一個 Holder 中,最終讓 Holder 提供頁面,直接貼到容器裡。
public class DoubleListViewHolder extends BaseHolder<List<List<String>>> {
private List<List<String>> mData;
private ListView mLeftListView;
private ListView mRightListView;
private List<String> mLeftList;
private List<String> mRightList;
private TextListAdapter mLeftAdapter;
private TextRightListAdapter mRightAdapter;
private View mViewClickRecorder = null;
private boolean mFirstMesure = true;
@Override public View initView() {
View view = View.inflate(MyApplication.getContext(), R.layout.doublelistview_layout, null);
mLeftListView = (ListView) view.findViewById(R.id.ll_left);
mRightListView = (ListView) view.findViewById(R.id.ll_right);
mLeftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mViewClickRecorder != view) {
view.setBackgroundColor(MyApplication.getContext().getResources().getColor(R.color.normal_selected_color));
if (mViewClickRecorder != null) {
mViewClickRecorder.setBackgroundColor(MyApplication.getContext().getResources().getColor(R.color.normal_unselected_color));
}
mViewClickRecorder = view;
}
mRightList = mData.get(position + 1);
mRightAdapter = new TextRightListAdapter(mRightList);
mRightListView.setAdapter(mRightAdapter);
}
});
return view;
}
@Override public void refreshView(List<List<String>> info) {
this.mData = info;
mLeftList = info.get(0);
mLeftAdapter = new TextListAdapter(mLeftList);
mRightList = info.get(1);
mRightAdapter = new TextRightListAdapter(mRightList);
mLeftListView.setAdapter(mLeftAdapter);
mRightListView.setAdapter(mRightAdapter);
}
class TextListAdapter extends MyAdapter {
public TextListAdapter(List list) { super(list); }
@Override public View getView(final int position, View convertView, ViewGroup parent) {
TextViewHolder holder = null;
if (convertView == null) { holder = new TextViewHolder(); }
else { holder = (TextViewHolder) convertView.getTag(); }
convertView = holder.getView();
holder.refreshView((String) getItem(position));
if (position == 0 && mFirstMesure) {
mFirstMesure = false;
convertView.setBackgroundColor(MyApplication.getContext().getResources().getColor(R.color.normal_selected_color));
mViewClickRecorder = convertView;
}
return convertView;
}
}
class TextRightListAdapter extends MyAdapter {
public TextRightListAdapter(List list) { super(list); }
@Override public View getView(final int position, View convertView, ViewGroup parent) {
TextViewHolder holder = null;
if (convertView == null) { holder = new TextViewHolder(); }
else { holder = (TextViewHolder) convertView.getTag(); }
convertView = holder.getView();
holder.refreshView((String) getItem(position));
return convertView;
}
}
private class TextViewHolder extends BaseHolder<String> {
private TextView mTextView;
@Override public View initView() {
View view = View.inflate(MyApplication.getContext(), R.layout.simpletext_item, null);
mTextView = (TextView) view.findViewById(R.id.tv_text);
return view;
}
@Override public void refreshView(String info) { mTextView.setText(info); }
}
}
呼叫時分為四步,這樣顯示、邏輯與資料就自然分離開來了:
// 建立容器
PopMenuButton mPopMenuButton = (PopMenuButton) findViewById(R.id.btn_pop);
// 建立 Holder,提供內容
DoubleListViewHolder holder = new DoubleListViewHolder();
// 將內容貼到容器中
mPopMenuButton.setContentView(holder.getView());
// 用資料刷新容器的顯示內容
holder.refreshView(mLists);
Android 字母索引(側邊欄)的完整實現
剛學 Android 時就想自己手寫一個索引,但當時還是菜鳥,一直沒動手。昨天終於寫了一個,今天貼出來備用。先看效果圖: 由於列表是我自己封裝的,看到展示頁面時可能會
自定義 View + 屬性動畫:打造會「充電」的電池進度條
近期專案需要一種類似手機電池充電的動畫效果。以前還沒學屬性動畫的時候,這種東西只能靠「圖片序列 + 定時器」硬做——換一張圖、睡 200 毫秒、再換下一張,程式碼醜而且幀
Android 網絡開發實戰:WebView、HttpURLConnection 與 HttpClient
在 Android 應用中,網絡通信幾乎是必不可少的能力。無論是加載網頁、請求接口,還是上傳與下載數據,都需要合適的網絡組件來完成。本文以實用為導向,梳理 Android
Android 專案重構之路:實現篇解析
專案分層 重構的核心是讓責任分離。本文將專案分為模型層、接口層、核心層與界面層。模型層負責資料結構,接口層負責網路請求,核心層負責業務邏輯,界面層負責使用者交互。 模組