Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自定義控件:下拉菜單的實現與優化

編輯:關於 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 中,最終讓 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);
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved