Android滚动菜单实现

本文介绍如何在Android项目中使用WheelView组件创建一个滚动菜单。通过自定义Dialog和布局文件,详细解析代码实现过程,展示了一个请假类型选择的滚动菜单效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上效果图:

这是项目中一个qing请假的选择请假类型的滚动菜单,用了WheelView,继承了WhellViewAdapter并zuo'做了些许修改。

下面开始分析代码:

首先自定义一个Dialog,然后布局文件:



    <yilanTech.EduYunClient.view.wheel.WheelView
        android:id="@+id/dialog_wheelview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_line"
        android:layout_margin="@dimen/common_dp_10" />

然后我们在代码中打造一个通用的滑动菜单弹窗:



public class WheelBottomDiaLog extends Dialog implements View.OnClickListener {
    private TextView cancle, yes;//确定取消按钮
    private WheelView wheelView;
    private onClickYesButtonListener onClickYesButtonListener;//点击确定回调
    private List<String> list;//数据源
    private WheelBottomAdapter wheelBottomAdapter;//自定义Adapter

    public WheelBottomDiaLog(@NonNull Context context, List<String> list) {
        super(context, R.style.dialog_parent);
        this.list = list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rapor_dialog);
     ...//初始化控件,此处省略,下方为重要代码
        wheelView.setShowShadow(false);//去掉上下边框中的阴影
        wheelBottomAdapter = new WheelBottomAdapter(getContext());//初始化adapter
        wheelBottomAdapter.setTextColorCenter(getContext().getResources().getColor(R.color.blacktext));//选中内容字体颜色
        wheelBottomAdapter.setTextColor(getContext().getResources().getColor(R.color.gray7));//未选中字体颜色
        wheelBottomAdapter.setGravity(Gravity.CENTER);//设置item的分布
        wheelBottomAdapter.setIfBold(false);//取消字体加粗
        wheelBottomAdapter.setTextSize(50);//设置item内容字体大小
    ...//设置监听
        wheelView.setViewAdapter(wheelBottomAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bottom_dialog_cancle:
                dismiss();
                break;
            case R.id.dialog_yes:
                onClickYesButtonListener.onResult(list.get(wheelView.getCurrentItem()));//回调选择的数据,getCurrenItem()返回当前index
                dismiss();
                break;
            default:
                break;
        }
    }


    public interface onClickYesButtonListener {
        void onResult(String result);
    }

    public void setOnClickYesButtonListener(onClickYesButtonListener onClickYesButtonListener) {
        this.onClickYesButtonListener = onClickYesButtonListener;
    }

    @Override
    public void show() {
        super.show();
        DisplayMetrics d = new DisplayMetrics();  //为获取屏幕宽、高
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(d);
        android.view.WindowManager.LayoutParams p = this.getWindow().getAttributes();  //获取对话框当前的参数值
        p.gravity = Gravity.BOTTOM;
        p.height = WindowManager.LayoutParams.WRAP_CONTENT;
        p.width = (int) (d.widthPixels * 0.95);
        this.getWindow().setAttributes(p);
    }


自定义Adapter

    class WheelBottomAdapter extends AbstractWheelTextAdapter {

        /**
         * Constructor
         *
         * @param context the current context
         */
        protected WheelBottomAdapter(Context context) {
            super(context);
        }

        @Override
        protected CharSequence getItemText(int index) {
            return list.get(index);
        }

        @Override
        public int getItemsCount() {
            return list.size();
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值