Android工具集高级UI

滚动时间控件,只选年月

2019-06-20  本文已影响2人  Small_Cake

原文链接:https://www.jianshu.com/p/4a2c853d9276

年月滚动时间选择器
0. 创建BottomDatePickerPop来从底部弹出此时间选择器
/**
 * 底部年月时间选择弹出窗
 */
public class BottomDatePickerPop extends BottomPopupView {

    @BindView(R.id.wheel_year)
    WheelPicker wheelYear;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.wheel_month)
    WheelPicker wheelMonth;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;

    int selectYear;
    int selectMonth;

    public BottomDatePickerPop(@NonNull Context context) {
        super(context);
    }
    private onSelectYearMonth listener;

    public void setListener(onSelectYearMonth listener) {
        this.listener = listener;
    }

    public interface  onSelectYearMonth{
        void selectYearMonth(int year,int month);
    }
    @Override
    protected int getImplLayoutId() {
        return R.layout.pop_bottom_date_picker;
    }

    @Override
    protected void onCreate() {
        ButterKnife.bind(this);
        super.onCreate();
        initYearMonth();
    }

    private void initYearMonth() {
        Calendar now = Calendar.getInstance();
        int yearNum = now.get(Calendar.YEAR);
        int monthNum = now.get(Calendar.MONTH);//默认月份会少1
        selectYear = yearNum;
        selectMonth = monthNum+1;

        List<Integer> years = getYearNum(yearNum);
        List<Integer> months = getMonthNum(monthNum);
        wheelYear.setData(years);
        wheelMonth.setData(months);
        wheelYear.setSelectedItemPosition(years.size()-1);
        wheelMonth.setSelectedItemPosition(months.size()-1);

        initWheelSet(wheelYear);
        initWheelSet(wheelMonth);
        wheelYear.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                int year = (int) data;
                L.e(year+"年");
                selectYear  =year;
            }
        });
        wheelMonth.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                int month = (int) data;
                L.e(month+"月");
                selectMonth = month;
            }
        });
    }
    private List<Integer> getYearNum( int yearNum){
        List<Integer> data = new ArrayList<>();
        for (int i = yearNum-3; i <= yearNum; i++) {
            data.add(i);
        }
        return data;
    }
    private List<Integer> getMonthNum(int monthNum){
        List<Integer> data = new ArrayList<>();
        for (int i = 1; i <= monthNum+1; i++) {
            data.add(i);
        }
        return data;
    }
    private void initWheelSet(WheelPicker wheel){
//        wheel.setCurtain(true);//是否加入蒙版层,颜色深度降低
        wheel.setCurved(true);//圆弧滚轮效果
        wheel.setItemTextColor(ContextCompat.getColor(this.getContext(),R.color.text_light_gray));
        wheel.setSelectedItemTextColor(ContextCompat.getColor(this.getContext(),R.color.text_black));

    }
    @OnClick(R.id.btn_confirm)
    public void doClick(){
        if (listener!=null)listener.selectYearMonth(selectYear,selectMonth);
        this.dismiss();
    }
}

1. 布局中使用WheelPicker创建两个并排的时间滚动器

布局文件pop_bottom_date_picker为:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="选择时间"
        android:textColor="@color/text_black"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.aigestudio.wheelpicker.WheelPicker
        android:id="@+id/wheel_year"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:minHeight="32dp"
        app:layout_constraintEnd_toStartOf="@+id/wheel_month"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_title" />

    <View
        android:id="@+id/divider6"
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_marginTop="48dp"
        android:background="?android:attr/listDivider"
        app:layout_constraintBottom_toTopOf="@+id/wheel_year"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/wheel_year" />

    <com.aigestudio.wheelpicker.WheelPicker
        android:id="@+id/wheel_month"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:minHeight="32dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/wheel_year"
        app:layout_constraintTop_toBottomOf="@+id/tv_title" />

    <Button
        android:id="@+id/btn_confirm"
        style="@style/DefaultOrangeButton"
        android:layout_width="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="16dp"
        android:paddingLeft="80dp"
        android:paddingRight="80dp"
        android:text="确认"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/wheel_year" />

    <View
        android:id="@+id/divider1"
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_marginBottom="48dp"
        android:background="?android:attr/listDivider"
        app:layout_constraintBottom_toTopOf="@+id/wheel_year"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/wheel_year" />

</android.support.constraint.ConstraintLayout>
2.使用了 XPopup来从底部弹出这个时间选择器
    BasePopupView basePopupView;
    private void initDatePicker() {
        BottomDatePickerPop bottomDatePickerPop = new BottomDatePickerPop(_mActivity);
        bottomDatePickerPop.setListener(new BottomDatePickerPop.onSelectYearMonth() {
            @Override
            public void selectYearMonth(int year, int month) {
                tvTitle.setText("订单明细("+year+"年"+month+"月)");
            }
        });
        basePopupView = new XPopup.Builder(_mActivity)
                .asCustom(bottomDatePickerPop);

    }
   @OnClick(R.id.iv_right)
    public void doClicks(View view){
        basePopupView.show();
    }
上一篇下一篇

猜你喜欢

热点阅读