常用底部滚动选择框

效果:

常用底部滚动选择框_第1张图片

代码:

public class SelectDialog extends BaseDialog {

    private OnSelectListener selectListener;
    private List dataList;
    private WheelVerticalView wheel_select;
    private TextView tv_cancel;
    private RelativeLayout rl_select_layout;

    public SelectDialog(Context context) {
        super(context);
    }

    public SelectDialog(Context context, List dataList, boolean canceledOnTouchOutside, OnSelectListener selectListener) {
        super(context, canceledOnTouchOutside);
        this.dataList = dataList;
        this.selectListener = selectListener;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dlg_select;
    }

    @Override
    protected void initView(View rootView) {
        wheel_select = (WheelVerticalView) rootView.findViewById(R.id.wheel);
        tv_cancel = (TextView) rootView.findViewById(R.id.tv_cancel);
        rl_select_layout = (RelativeLayout) rootView.findViewById(R.id.rl_select_bankcard_layout);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SelectDialog.this.dismiss();
            }
        });
        rl_select_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SelectDialog.this.dismiss();
            }
        });
        rootView.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectListener.onSelect(wheel_select.getCurrentItem());
                SelectDialog.this.dismiss();
            }
        });
        getWindow().setWindowAnimations(R.style.anim_bottom_alpha);
    }

    @Override
    protected void initData() {
        if (dataList == null || dataList.size() == 0)
            return;
        wheel_select.setViewAdapter(new ArrayWheelAdapter<>(mContext, R.layout.item_select_dialog, R.id.tv_content, dataList));
    }

    public void setSelection(int position) {
        wheel_select.setCurrentItem(position);
    }

    public interface OnSelectListener {
        void onSelect(int position);
    }

    private static class ArrayWheelAdapter extends AbstractWheelTextAdapter {
        private List items;

        public ArrayWheelAdapter(Context context, int itemResource, int itemTextResource, List items) {
            super(context, itemResource, itemTextResource);
            this.items = items;
        }

        @Override
        public CharSequence getItemText(int index) {
            if (index >= 0 && index < items.size()) {
                T item = items.get(index);
                if (item instanceof SelectItemBean) {
                    return ((SelectItemBean) item).content;
                }
                return item.toString();
            }
            return null;
        }

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

布局:dlg_select.xml



    
    

        

            

            
        
 
        
        
    
    

布局:item_select_dialog.xml



    

    
    

使用:

SelectDialog dialog = new SelectDialog(context, relationList, true, new SelectDialog.OnSelectListener() {
                    @Override
                    public void onSelect(int position) {
                        mFrtRelationWithProposer.setText(relationList.get(position));
                    }
                });
dialog.show();

你可能感兴趣的:(常用底部滚动选择框)