为了账号安全,请及时绑定邮箱和手机立即绑定

title下拉框的一种实现

标签:
Android

效果图:

https://img1.sycdn.imooc.com//5c205a630001ed7c07761128.jpg

基类是继承Dialog:

public abstract class DropDownFilterDialog extends Dialog {

    protected ArrayList<DropDownItem> mTopMenuList;
    OnDropdownClickListener mDropdownClickListener;
    protected LinearLayout mDropDownMenuLayout;
    public Context mContext;
    private int mCurrentIndex = 0;

    public void setCurrentIndex(int mCurrentIndex) {
        this.mCurrentIndex = mCurrentIndex;
    }

    public void setTopMenuItemList(ArrayList<DropDownItem> mTopMenuList) {
        this.mTopMenuList = mTopMenuList;
    }

    public DropDownFilterDialog(Context context) {
        super(context, R.style.DropdownFilterDialogStyle);
        mContext = context;
    }

    public interface OnDropdownClickListener {
        void onDropdownHide();
        void onClickItem(int index);
    }

    public void setDropDownClickListener(OnDropdownClickListener dropdownClickListener) {
        mDropdownClickListener = dropdownClickListener;
    }

    public abstract void hideDropDownFilter(boolean showAnimation);

    public abstract void showDialog(boolean showAnimation, View banner);

    public void init() {
        mDropDownMenuLayout = (LinearLayout) findViewById(R.id.text_chat_menu_linear);
        constructButton(mTopMenuList);
    }

    @Override
    protected void onStart() {
        super.onStart();
        init();
    }

    public void constructButton(ArrayList<DropDownItem> list) {
        mDropDownMenuLayout.removeAllViews();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        mDropDownMenuLayout.addView(View.inflate(mContext,
                R.layout.view_top_menu_separator, null), params);
        final int size = list.size();
        final int lastPosition = size - 1;
        for (int i = 0; i < size; i++) {
            DropDownItem item = list.get(i);
            View view = View.inflate(mContext, R.layout.dropdown_menu_item_layout,
                    null);
            view.setTag(i);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    hideDropDownFilter(true);
                    mDropdownClickListener.onClickItem((Integer) view.getTag());
                }
            });

            TextView btn = (TextView) view
                    .findViewById(R.id.top_menu_button);
            btn.setText(item.getName());
            btn.setTag(item.getIndex());
            if (mCurrentIndex == i) {
                btn.setTextColor(mContext.getResources().getColor(R.color.contact_detail_phone_related_color_pressed));
            }
            TextView tab_indicator_counter = (TextView) view.findViewById(R.id.indicator_counter);
            if (item.getCouter() == 0) {
                tab_indicator_counter.setVisibility(View.GONE);
            } else {
                tab_indicator_counter.setText(String.valueOf(item.getCouter()));
            }

            boolean showSeparator = true;
            if (size == 1) {
                showSeparator = false;
            } else if (i == lastPosition) {
                showSeparator = false;
            }
            view.setBackgroundResource(R.drawable.drop_down_filter_item_background_selector);
            mDropDownMenuLayout.addView(view, params);
            if (showSeparator) {
                LinearLayout.LayoutParams paramsForLine = new LinearLayout.LayoutParams(
                        WindowManager.LayoutParams.MATCH_PARENT, mContext.getResources().getDimensionPixelSize(R.dimen.top_menu_vertical_separator));
                paramsForLine.leftMargin = mContext.getResources().getDimensionPixelSize(R.dimen.drop_down_menu_item_margin_left_right);
                paramsForLine.rightMargin = paramsForLine.leftMargin;
                mDropDownMenuLayout.addView(View.inflate(mContext,
                        R.layout.drop_down_menu_divider_layout, null), paramsForLine);
            }
        }

        mDropDownMenuLayout.invalidate();
    }

}

写一个对于phone的实现类:

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

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text_chat_menu_linear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/top_menu_button_bg"
    android:orientation="vertical" >

</LinearLayout>

使用方法:

private void showDropDownFilter(boolean showAnimation) {
    if (mDropDownFilterDialog == null) {
        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);
        }
    });
}

代码在:https://github.com/nickgao1986/StepSport

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消