🕘 Android滚动选择器(省市区联动选择、日期选择、时间选择)

Overview

PickerView gitHub release platform Android Arsenal license Build status

Android滚动选择器

使用方法

1. 添加依赖

注:${latestVersion}请替换为当前最新版本号,见releases

gradle:

implementation 'com.github.duanhong169:picker-view:${latestVersion}'

maven:

<dependency>
	<groupId>com.github.duanhong169</groupId>
	<artifactId>picker-view</artifactId>
	<version>${latestVersion}</version>
	<type>pom</type>
</dependency>

2. 集成到项目中

2.1 集成PickerView

添加到layout文件中:

<top.defaults.view.PickerView
	android:id="@+id/pickerView"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"/>
使用列表作为数据源
List<Item> items = new ArrayList<>();
for (int i = 0; i < 42; i++) {
    items.add(new Item("Item " + i));
}

pickerView.setItems(Item.sampleItems(), item -> textView.setText(item.getText()));
实现Adapter作为数据源

配置数据源:

PickerView.Adapter adapter = new PickerView.Adapter() {

    @Override
    public int getItemCount() {
        return 42;
    }

    @Override
    public String getText(int index) {
        return "Item " + index;
    }
};
pickerView.setAdapter(adapter);

监听选择事件:

pickerView.setOnSelectedItemChangedListener((pickerView, previousPosition, selectedItemPosition) -> 
        textView.setText(pickerView.getAdapter().getText(selectedItemPosition)));

2.2 集成DivisionPickerView

添加到layout文件中:

<top.defaults.view.DivisionPickerView
    android:id="@+id/divisionPicker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:preferredMaxOffsetItemCount="4"
    android:background="#e7e7e7"/>

构建省市区数据源:

参考Divisions.java

设置数据源并监听选择事件

final List<DivisionModel> divisions = Divisions.get(this);
divisionPicker.setDivisions(divisions);
divisionPicker.setOnSelectedDateChangedListener(division -> textView.setText(Division.Helper.getCanonicalName(division)));

2.3 集成DateTimePickerView

添加到layout文件中:

<top.defaults.view.DateTimePickerView
    android:id="@+id/datePickerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:preferredMaxOffsetItemCount="3"
    app:textSize="18sp"
    app:type="dateTime"
    app:minutesInterval="fifteen"
    app:curved="true"
    android:background="#e7e7e7"/>

设置初始日期:

dateTimePickerView.setStartDate(Calendar.getInstance());
// 注意:月份是从0开始计数的
dateTimePickerView.setSelectedDate(new GregorianCalendar(2017, 6, 27, 21, 30));

监听选择事件:

dateTimePickerView.setOnSelectedDateChangedListener(new DateTimePickerView.OnSelectedDateChangedListener() {
    @Override
    public void onSelectedDateChanged(Calendar date) {
        int year = date.get(Calendar.YEAR);
        int month = date.get(Calendar.MONTH);
        int dayOfMonth = date.get(Calendar.DAY_OF_MONTH);
        int hour = date.get(Calendar.HOUR_OF_DAY);
        int minute = date.get(Calendar.MINUTE);
        String dateString = String.format(Locale.getDefault(), "%d年%02d月%02d日%02d时%02d分", year, month + 1, dayOfMonth, hour, minute);
        textView.setText(dateString);
        Log.d(TAG, "new date: " + dateString);
    }
});

更详细的使用方法请参见示例。

License

Copyright 2018 Hong Duan

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

You might also like...
Comments
  • Added customization features.

    Added customization features.

    Added set selectedItemsPosition method in DivisionPickerView. Added set custom typeface and item height for DivisionPicker. Added set custom mask gradient, AbiliAdded set custom mask gradient, Ability to show or hide selected item drawable in PickerView. to show or hide selected item drawable in DivisionPicker.

    opened by ali-star 0
  • Colors do not correspond with application theme

    Colors do not correspond with application theme

    First of all, I would like to thank you for this amazing library. It is the best PickerView I found so far. There is only one problem.

    When I use PickerView with dark theme, topMask and bottomMask are not set correctly. Please set those fields to value corresponding with actual theme. You will need to add something like this:

    TypedValue typedValue = new TypedValue();
    if (resolveAttribute(android.R.attr.windowBackground, typedValue, true)) {
        int[] gradientColors = {typedValue.data, Color.TRANSPARENT};
        topMask = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, gradientColors);
        bottomMask = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, gradientColors);
    }
    

    Or make those fields public. In the meantime, I am forced to use this ugly workaround (reflection):

    fun Resources.Theme.fixPickerViewsMask(vararg pickerViews: PickerView) {
        val typedValue = TypedValue()
        if (resolveAttribute(android.R.attr.windowBackground, typedValue, true)) {
            val gradientColors = intArrayOf(typedValue.data, Color.TRANSPARENT)
    
            val topMask = PickerView::class.java.getDeclaredField("topMask")
            topMask.isAccessible = true
            val bottomMask = PickerView::class.java.getDeclaredField("bottomMask")
            bottomMask.isAccessible = true
    
            for (picker in pickerViews) {
                topMask.set(picker, GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, gradientColors))
                bottomMask.set(picker, GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, gradientColors))
            }
        }
    }
    
    opened by kuhy 0
  • 日期显示有问腿

    日期显示有问腿

                @Override
                public PickerView.PickerItem getItem(int index) {
                    final Calendar tempCalendar = (Calendar) startDate.clone();
                    tempCalendar.add(Calendar.DAY_OF_YEAR, index);
                    return new PickerView.PickerItem() {
                        @Override
                        public String getText() {
                            if (TimeUtils.isToday(tempCalendar)) {
                                return "今天";
                            }
                            return TimeUtils.date(tempCalendar);
                        }
                    };
                }
    

    这个地方需要修改,否则日期显示不准确

    opened by zuixianjian 0
Releases(1.0.0)
Owner
Hong Duan
To be a better man. Focus on Android development.
Hong Duan