|
1 |
| -Welcome! |
| 1 | +# ValuePicker |
| 2 | +An Android library that provides a simple and customizable ValuePicker. |
| 3 | + |
| 4 | +[  ](https://bintray.com/mars885/maven/valuepicker/_latestVersion) |
| 5 | + |
| 6 | + |
| 7 | +[](https://opensource.org/licenses/Apache-2.0) |
| 8 | +[](http://developer.android.com/index.html) |
| 9 | + |
| 10 | +## Contents |
| 11 | + |
| 12 | +* [Demo](#demo-youtube) |
| 13 | +* [Installation](#installation) |
| 14 | +* [Usage](#usage) |
| 15 | +* [Advanced Usage](#advanced-usage) |
| 16 | +* [License](#license) |
| 17 | + |
| 18 | +## Demo (YouTube) |
| 19 | + |
| 20 | +<a href="https://www.youtube.com/watch?v=qzoZh3aYlcY"> |
| 21 | +<img src="/media/demo_thumbnail.png" width="200" height="422"/> |
| 22 | +</a> |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +1. Make sure that you've added the `jcenter()` repository to your top-level `build.gradle` file. |
| 27 | + |
| 28 | +````groovy |
| 29 | +buildscript { |
| 30 | + //... |
| 31 | + repositories { |
| 32 | + //... |
| 33 | + jcenter() |
| 34 | + } |
| 35 | + //... |
| 36 | +} |
| 37 | +```` |
| 38 | + |
| 39 | +2. Add the library dependency to your module-level `build.gradle` file. |
| 40 | + |
| 41 | +````groovy |
| 42 | +dependencies { |
| 43 | + //... |
| 44 | + implementation "com.paulrybitskyi.valuepicker:valuepicker:1.0.0" |
| 45 | + //... |
| 46 | +} |
| 47 | +```` |
| 48 | + |
| 49 | +## Usage |
| 50 | +Basic usage of the ValuePickerView involves two steps - declaring a widget inside the XML file of your choice and configuring it in one of the Kotlin/Java classes. |
| 51 | + |
| 52 | +Let's see how we can do that by following the steps listed above: |
| 53 | + |
| 54 | +1. Declaring a widget inside the XML file. |
| 55 | + |
| 56 | + <details><summary><b>XML (click to expand)</b></summary> |
| 57 | + <p> |
| 58 | + |
| 59 | + ````xml |
| 60 | + <?xml version="1.0" encoding="utf-8"?> |
| 61 | + <androidx.constraintlayout.widget.ConstraintLayout |
| 62 | + xmlns:android="http://schemas.android.com/apk/res/android" |
| 63 | + xmlns:app="http://schemas.android.com/apk/res-auto" |
| 64 | + android:layout_width="match_parent" |
| 65 | + android:layout_height="match_parent" |
| 66 | + android:background="@color/colorPrimary"> |
| 67 | + |
| 68 | + <!-- Other widgets here --> |
| 69 | + |
| 70 | + <com.paulrybitskyi.valuepicker.ValuePickerView |
| 71 | + android:id="@+id/valuePickerView" |
| 72 | + android:layout_width="wrap_content" |
| 73 | + android:layout_height="wrap_content" |
| 74 | + app:vpv_areDividersEnabled="true" |
| 75 | + app:vpv_isInfiniteScrollEnabled="true" |
| 76 | + app:vpv_maxVisibleItems="5" |
| 77 | + app:vpv_textColor="@color/colorAccent" |
| 78 | + app:vpv_dividerColor="@color/colorAccent" |
| 79 | + app:vpv_flingSpeedFactor="0.3" |
| 80 | + app:vpv_textSize="@dimen/date_picker_text_size" |
| 81 | + app:vpv_textTypeface="@font/ubuntu_mono_bold" |
| 82 | + app:vpv_divider="@drawable/custom_divider" |
| 83 | + app:vpv_orientation="vertical"/> |
| 84 | + |
| 85 | + </androidx.constraintlayout.widget.ConstraintLayout> |
| 86 | + ```` |
| 87 | + </p></details> |
| 88 | + |
| 89 | +2. Configuring the widget in one of the Kotlin/Java classes. |
| 90 | + |
| 91 | + <details><summary><b>Kotlin (click to expand)</b></summary> |
| 92 | + <p> |
| 93 | + |
| 94 | + ````kotlin |
| 95 | + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| 96 | + super.onViewCreated(view, savedInstanceState) |
| 97 | + |
| 98 | + //... |
| 99 | + |
| 100 | + with(valuePickerView) { |
| 101 | + onItemSelectedListener = OnItemSelectedListener { item -> |
| 102 | + // Do something with item |
| 103 | + } |
| 104 | + |
| 105 | + val pickerItems = getPickerItems() |
| 106 | + |
| 107 | + items = pickerItems |
| 108 | + setSelectedItem(pickerItems[2]) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + private fun getPickerItems(): List<Item> { |
| 114 | + return mutableListOf<Item>().apply { |
| 115 | + for(number in 1..100) { |
| 116 | + add( |
| 117 | + PickerItem( |
| 118 | + id = number, |
| 119 | + title = number.toString() |
| 120 | + ) |
| 121 | + ) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + ```` |
| 126 | + |
| 127 | + </p></details> |
| 128 | + |
| 129 | + <details><summary><b>Java (click to expand)</b></summary> |
| 130 | + <p> |
| 131 | + |
| 132 | + ````java |
| 133 | + @Override |
| 134 | + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
| 135 | + super.onViewCreated(view, savedInstanceState); |
| 136 | + |
| 137 | + ValuePickerView valuePickerView = view.findViewById(R.id.valuePickerView); |
| 138 | + valuePickerView.setOnItemSelectedListener((item) -> { |
| 139 | + // Do something with item |
| 140 | + }); |
| 141 | + |
| 142 | + final ArrayList<Item> pickerItems = getPickerItems(); |
| 143 | + |
| 144 | + valuePickerView.setItems(getPickerItems()); |
| 145 | + valuePickerView.setSelectedItem(pickerItems.get(2)); |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + private ArrayList<Item> getPickerItems() { |
| 150 | + final ArrayList<Item> pickerItems = new ArrayList<>(100); |
| 151 | + |
| 152 | + for(int i = 1; i <= 100; i++) { |
| 153 | + pickerItems.add( |
| 154 | + new PickerItem( |
| 155 | + i, |
| 156 | + String.valueOf(i) |
| 157 | + ) |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + return pickerItems; |
| 162 | + } |
| 163 | + ```` |
| 164 | + |
| 165 | + </p></details> |
| 166 | + |
| 167 | +## Advanced Usage |
| 168 | + |
| 169 | +See the [Sample app](https://github.com/mars885/value-picker/tree/master/sample/src/main/java/com/paulrybitskyi/sample/valuepicker). |
| 170 | + |
| 171 | +## License |
| 172 | + |
| 173 | +PersistentSearchView is licensed under the [Apache 2.0 License](LICENSE). |
0 commit comments