Application of Dialog in android (2)

Source: Internet
Author: User

Application of Dialog in android (2)

If you implement a Dialog with an animation drop-down

 

Follow the base class of the previous article to implement the actual subclass:

 

public  class DropDownFilterDialogForPhone extends DropDownFilterDialog {    private boolean mShowAnimation = true;    private boolean mIsInAnimation;    private boolean mFinishShowDialog = false;    public DropDownFilterDialogForPhone(Context context) {        super(context);        Window window = getWindow();        WindowManager.LayoutParams lp = window.getAttributes();        lp.width = WindowManager.LayoutParams.MATCH_PARENT;        lp.gravity = Gravity.TOP | Gravity.FILL_HORIZONTAL;        lp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.drop_down_filter_dialog);    }    @Override    protected void onStart() {        super.onStart();        if (mShowAnimation) {            Animation downAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.drop_down_menu_down_anim);            downAnimation.setAnimationListener(new Animation.AnimationListener() {                @Override                public void onAnimationEnd(Animation animation) {                    mFinishShowDialog = true;                    mIsInAnimation = false;                }                @Override                public void onAnimationRepeat(Animation animation) {                }                @Override                public void onAnimationStart(Animation animation) {                }            });            mDropDownMenuLayout.startAnimation(downAnimation);            mIsInAnimation = true;        } else {            mFinishShowDialog = true;        }    }    public void hideDropDownFilter(boolean showAnimation) {        Animation upAnimation = AnimationUtils.loadAnimation(mContext, R.anim.drop_down_menu_up_anim);        upAnimation.setAnimationListener(new Animation.AnimationListener() {            @Override            public void onAnimationStart(Animation animation) {            }            @Override            public void onAnimationRepeat(Animation animation) {            }            @Override            public void onAnimationEnd(Animation animation) {                dismiss();                mIsInAnimation = false;            }        });        startGridViewUpAnimation(upAnimation);        mDropdownClickListener.onDropdownHide();        mIsInAnimation = true;    }    @Override    public boolean onTouchEvent(MotionEvent event) {        if (event.getAction() == MotionEvent.ACTION_DOWN) {            if (mFinishShowDialog) {                if (!mIsInAnimation) {                    hideDropDownFilter(true);                }                return true;            }        }        return false;    }    @Override    public boolean dispatchKeyEvent(KeyEvent event) {        if (event.getAction() == KeyEvent.ACTION_DOWN                && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {            if (mFinishShowDialog) {                if (!mIsInAnimation) {                    hideDropDownFilter(true);                }                return true;            }        }        return super.dispatchKeyEvent(event);    }    @Override    protected void onStop() {        super.onStop();    }    public void showDialog(boolean showAnimation, View banner) {        Window window = getWindow();        WindowManager.LayoutParams lp = window.getAttributes();        int positionY = mContext.getResources().getDimensionPixelSize(R.dimen.header_hight);        if (banner == null) {            lp.y = positionY;        } else {            lp.y = positionY + banner.getMeasuredHeight();        }        mShowAnimation = showAnimation;        show();    }    public void startGridViewUpAnimation(Animation animation) {        mDropDownMenuLayout.startAnimation(animation);    }}
   @Override    public void onClick(View v) {        if (null == mHeaderClickListener) {            return;        }        super.onClick(v);        switch (v.getId()) {            case R.id.title_layout:                if(mBtnDropDownImageview.getVisibility() == View.GONE) {                    return;                }                if (!mIsShowDialog) {                    // will show dialog                    showDropDownFilter(true);                    mIsShowDialog = true;                } else {                    mDropDownFilterDialog.hideDropDownFilter(true);                    mIsShowDialog = false;                }                break;        }    }    public void setOnDropDownMenuClick(DropDownMenuClicked mDropDownMenuClicked) {        this.mDropDownMenuClicked = mDropDownMenuClicked;    }    public void setDropDownMenuVisible(int visible) {        if(visible == View.VISIBLE) {            mBtnDropDownImageview.setVisibility(View.VISIBLE);        }else{            mBtnDropDownImageview.setVisibility(View.GONE);        }    }    public void rotatePlusButton(Context context, boolean isUp) {        AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(                context, isUp ? R.anim.flip_up : R.anim.flip_down);        animatorSet.setTarget(mBtnDropDownImageview);        animatorSet.start();    }    private void showDropDownFilter(boolean showAnimation) {        if (mDropDownFilterDialog == null) {            if(false) {                mDropDownFilterDialog = new DropDownFilterDialogForTablet(getContext());            }else{                mDropDownFilterDialog = new DropDownFilterDialogForPhone(getContext());            }        }        rotatePlusButton(mContext, false);        mDropDownFilterDialog.setCurrentIndex(mCurrentIndex);        mDropDownFilterDialog.setTopMenuItemList(mDropDownItemList);        mDropDownFilterDialog.showDialog(showAnimation, mBanner);        mDropDownFilterDialog.setDropDownClickListener(new DropDownFilterDialog.OnDropdownClickListener() {            @Override            public void onDropdownHide() {                rotatePlusButton(mContext, true);                mIsShowDialog = false;            }            @Override            public void onClickItem(int index) {                if (index == mCurrentIndex) {                    return;                }                mCurrentIndex = index;                mDropDownMenuClicked.onDropDownMenuClicked(index);            }        });    }    /*****************************************init filter*************************************************/    public int getCurrentIndex() {        return mCurrentIndex;    }    public void initMessageFilterWithState(int state) {        mCurrentIndex = state;        switch (state) {            case STATE_ALL:                setText(R.string.messages_bar_item_all);                break;            case STATE_VOICE:                setText(R.string.messages_bar_item_voice);                break;            case STATE_FAX:                setText(R.string.messages_bar_item_fax);                break;            case STATE_TEXT:                setText(R.string.messages_bar_item_text);                break;        }    }


Animation definition:

 

 

 
     
  
 

The Code clicked in Titlebar can be written as follows:

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.