Git Product home page Git Product logo

pickerview's Introduction

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.

pickerview's People

Contributors

duanhong169 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.