撸一个最简单的条件选择器picker(不联动)

来源:互联网 发布:中超数据服务商 编辑:程序博客网 时间:2024/06/01 21:30

用过各种时间选择器,地址选择器等等,但是有的时候需要一个类似身高体重,性别年龄的选择器,这种就是我下面所要说的不联动选择器,网上很多demo也有这种效果但是大多是封装完善的万能选择器,你如果抽取出来的话代码量就太多了,对于项目来说得不偿失,于是我写一个最简单的条件选择器。
思路很简单,底部我们使用一个dialog上面加上两个WheelView就可以了,看代码:

public class BottomDialog extends DialogFragment implements View.OnClickListener{    WheelView npHeight;    WheelView npWeight;    TextView mCancel;    TextView mSure;    private static final List<String> HEIGHTS = new ArrayList<String>();    private static final List<String> WEIGHTS = new ArrayList<String>();    static {        for (int i=150;i<=200;i++){            HEIGHTS.add(i+"cm");        }        for (int i=40;i<=150;i++){            WEIGHTS.add(i+"kg");        }    }    public static BottomDialog newInstance() {        Bundle args = new Bundle();        BottomDialog fragment = new BottomDialog();        fragment.setArguments(args);        return fragment;    }    @Override    public void onStart() {        super.onStart();        Window window = getDialog().getWindow();        WindowManager.LayoutParams params = window.getAttributes();        params.gravity = Gravity.BOTTOM;        params.width = WindowManager.LayoutParams.MATCH_PARENT;        window.setAttributes(params);        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));    }    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);        View view = inflater.inflate(R.layout.fragment_bottom_sheet,container,false);        mCancel=(TextView) view.findViewById(R.id.tv_cancel);        mSure=(TextView) view.findViewById(R.id.tv_sure);        mCancel.setOnClickListener(this);        mSure.setOnClickListener(this);        npHeight=(WheelView)view.findViewById(R.id.wl_cm);        npWeight=(WheelView)view.findViewById(R.id.wl_kg);        initValue();        slideToUp(view);        return view;    }    private String mHight=150+"cm";    private String mWight=40+"kg";    private void initValue() {            npHeight.setOffset(1);            npHeight.setItems(HEIGHTS);            npHeight.setOnWheelViewListener(new WheelView.OnWheelViewListener() {                @Override                public void onSelected(int selectedIndex, String item) {                    mHight=item;                }            });            npWeight.setOffset(1);            npWeight.setItems(WEIGHTS);            npWeight.setOnWheelViewListener(new WheelView.OnWheelViewListener() {                @Override                public void onSelected(int selectedIndex, String item) {                    mWight=item;                }            });    }    public  void slideToUp(View view){        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,                1.0f, Animation.RELATIVE_TO_SELF, 0.0f);        slide.setDuration(400);        slide.setFillAfter(true);        slide.setFillEnabled(true);        view.startAnimation(slide);        slide.setAnimationListener(new Animation.AnimationListener() {            @Override            public void onAnimationStart(Animation animation) {            }            @Override            public void onAnimationEnd(Animation animation) {            }            @Override            public void onAnimationRepeat(Animation animation) {            }        });    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.tv_cancel:                this.dismiss();                break;            case R.id.tv_sure:                EventBus.getDefault().post(new TestBean(mHight,mWight));                this.dismiss();                break;        }    }}

布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/white"    android:orientation="vertical"    >    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="15dp">        <TextView            android:id="@+id/tv_cancel"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentLeft="true"            android:layout_marginLeft="14dp"            android:textSize="15sp"            android:textColor="@color/blue"            android:text="取消"            />        <TextView            android:id="@+id/tv_sure"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:layout_marginRight="14dp"            android:textSize="15sp"            android:textColor="@color/blue"            android:text="确定"            />    </RelativeLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:gravity="center">        <WheelView            android:id="@+id/wl_cm"            android:layout_width="wrap_content"            android:layout_height="wrap_content"/>        <WheelView            android:id="@+id/wl_kg"            android:layout_width="wrap_content"            android:layout_height="wrap_content"/>    </LinearLayout></LinearLayout>

实现就是这么简单O(∩_∩)O哈哈~

0 0
原创粉丝点击