Comments (3)
I was having the exact same problem with a very erratic behavior. Found out that the component view is not reusable. Let me explain.
First, you need to have the exact order of:
numberPicker.setMinValue
numberPicker.setMaxValue
numberPicker.setDisplayedValues
If you invoke the "numberPicker.setDisplayedValues" first, which is a logic thing to do, you start getting a ArrayIndexOutOfBoundsException with a fixed index of 99. So, you do things in that order.
But the problem is when you try to reuse that same NumberPicker with different values. If the min or max value index is bigger than the displayed values list already there, you get again an ArrayIndexOutOfBoundsException.
But if the indexes are ok, the list is still all mixed up, with the selected value overlapping the other values, and sometimes even some missing values that only appear when we try to scroll through them.
The solution that I found was to set in the XML layout only a container for the NumberPicker (like a FrameLayout or something). And then, programmatically, create the NumberPicker and add it to the container. E.g.:
private void setNumberPicker(ViewGroup numberPickerContainer, String[] displayValues, String valueToSet) {
NumberPicker numberPicker = new NumberPicker(getContext());
numberPicker.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
numberPicker.setSelectedTextColorResource(R.color.picker_selected_text_color);
numberPicker.setSelectedTextSize(R.dimen.picker_selected_text_size);
numberPicker.setTextColorResource(R.color.picker_text_color);
numberPicker.setTextSize(R.dimen.picker_text_size);
numberPicker.setDividerColorResource(android.R.color.transparent);
final int DEFAULT_VISIBLE_ITEMS = 9;
int itemsLength = displayValues.length;
int numberOfVisibleItems = itemsLength < DEFAULT_VISIBLE_ITEMS ? (itemsLength + 1) : DEFAULT_VISIBLE_ITEMS;
numberPicker.setWheelItemCount(numberOfVisibleItems);
numberPicker.setMinValue(1);
numberPicker.setMaxValue(displayValues.length);
numberPicker.setDisplayedValues(displayValues);
for (int i = 0; i < displayValues.length; i++) {
if (displayValues[i].equals(valueToSet)) {
numberPicker.setValue(i + 1);
break;
}
}
numberPickerContainer.removeAllViews();
numberPickerContainer.addView(numberPicker);
}
Hope this helps others with the same problem.
from numberpicker.
+1'd the component being not reusable. I can't get it to work using any of workarounds here without at least graphical glitch (last item being on top before scrolling despite wrap around = false).
@ShawnLin013 this issue definitely should not be closed, it leads devs to believing that they're doing something wrong, so they reorder the min/max/val calls and do some weird workarounds, meanwhile it's the API here being fundamentally broken.
Don't get my wrong, I'm glad you reworked horrible original NumberPicker to point of being somewhat usable, but closing the issue just leads to people wasting time.
from numberpicker.
Hi @oenukr,
Thank you for your patch, but unfortunately there is an unacceptable side effect.
You can try the following code when received data from remote service.
I had tested it after the view is initialized 10 seconds later, and it's working correctly.
new CountDownTimer(10000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// do nothing
}
@Override
public void onFinish() {
int length = 10;
String[] numbers = new String[length];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = String.valueOf(i);
}
numberPicker.setMinValue(1);
numberPicker.setMaxValue(length);
numberPicker.setDisplayedValues(numbers);
numberPicker.setValue(0);
}
}.start();
from numberpicker.
Related Issues (20)
- divider not in center HOT 1
- The library is failing to build the app saying manifest merger failed HOT 2
- The value of the right side appears too late
- Lags when dragging slowly on first item
- OnClick called as soon as the user touches
- Theres has an anr problem when scroll numberpicker
- AAPT: error: attribute np_itemSpacing (aka com.sample.myapp:np_itemSpacing) not found. HOT 1
- Issue scroll inside scrollview
- Posibility to change value with animation HOT 2
- Migrate Artifacts to Another Repository HOT 1
- The wheel keeps on scrolling HOT 2
- Feature Request HOT 2
- Migrate to Maven from Jcenter HOT 3
- Detect and stop in valid scroller positions
- Can I change only the background color of selected lines?
- Problem with release v2.4.13 on Jitpack.io HOT 1
- Custom item view is possible somehow ?
- onValueChanged called several times, with alternating values HOT 1
- [BUG] fontFamily or includeFontPadding HOT 2
- java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from numberpicker.