当前位置: 代码迷 >> Android >> Dialog在android中的应用(2)
  详细解决方案

Dialog在android中的应用(2)

热度:21   发布时间:2016-04-28 01:03:00.0
Dialog在android中的应用(二)

如果实现一个带动画的下拉的Dialog


沿用上篇的基类,实现实际上的子类:

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;        }    }


动画的定义:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate         android:fromXDelta="0"        android:toXDelta="0"        android:fromYDelta="-100%"        android:toYDelta="0%"        android:duration="@integer/gridview_duration"    /></set>

在Titlebar里面点击的代码可以这样写:


  相关解决方案