Unable to make direct scrollbar always visible (Android)

I have such a problem - I want the scroll bar to always be visible. But for spinner function

setScrollbarFadingEnabled(false); 

causes failures using NullExceptionPointer during GUI drawing.

XML tags also cannot solve this problem - it seems that the counter simply ignores them.

Perhaps there is another way to move? For example, using a custom scrollbar? If so, how can I do this?

Thank you for your answers, Dmitry.

As I already asked, here is the LogCat error message for a simple project with just a counter:

 AndroidRuntime(2252): FATAL EXCEPTION: main AndroidRuntime(2252): java.lang.NullPointerException AndroidRuntime(2252): at android.view.View.onDrawScrollBars(View.java:5836) AndroidRuntime(2252): at android.view.View.draw(View.java:6799) AndroidRuntime(2252): at android.view.ViewGroup.drawChild(ViewGroup.java:1640) AndroidRuntime(2252): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) AndroidRuntime(2252): at android.view.ViewGroup.drawChild(ViewGroup.java:1638) AndroidRuntime(2252): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) AndroidRuntime(2252): at android.view.View.draw(View.java:6796) AndroidRuntime(2252): at android.widget.FrameLayout.draw(FrameLayout.java:352) AndroidRuntime(2252): at android.view.ViewGroup.drawChild(ViewGroup.java:1640) AndroidRuntime(2252): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) AndroidRuntime(2252): at android.view.ViewGroup.drawChild(ViewGroup.java:1638) AndroidRuntime(2252): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) AndroidRuntime(2252): at android.view.View.draw(View.java:6796) AndroidRuntime(2252): at android.widget.FrameLayout.draw(FrameLayout.java:352) AndroidRuntime(2252): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2078) AndroidRuntime(2252): at android.view.ViewRoot.draw(ViewRoot.java:1433) AndroidRuntime(2252): at android.view.ViewRoot.performTraversals(ViewRoot.java:1175) AndroidRuntime(2252): at android.view.ViewRoot.handleMessage(ViewRoot.java:1753) AndroidRuntime(2252): at android.os.Handler.dispatchMessage(Handler.java:99) AndroidRuntime(2252): at android.os.Looper.loop(Looper.java:123) AndroidRuntime(2252): at android.app.ActivityThread.main(ActivityThread.java:4632) AndroidRuntime(2252): at java.lang.reflect.Method.invokeNative(Native Method) AndroidRuntime(2252): at java.lang.reflect.Method.invoke(Method.java:521) AndroidRuntime(2252): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871) AndroidRuntime(2252): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) AndroidRuntime(2252): at dalvik.system.NativeStart.main(Native Method) 
+4
source share
3 answers

Spinner has no scrollbar, so you get a NullPointerException.

The popup shown by spinner has a scroll bar. Therefore, you need to change the ListView properties shown by spinner. But ListView itself is not disclosed by Spinner by any public methods.

Even if you get ListPopupWindow by reflection, another problem arises: ListPopupWindow ListView is created only after clicking the Spinner button.

But OnClickListener Spinner cannot be registered to set ListView properties after the show.

You can create a Custom Spinner with an override performClick parameter, and then get mPopup by reflection. and use mPopup.getListView().setScrollbarFadingEnabled(false)

But if you are planning to create your own Spinner, I find it easier to implement a pop-up window according to your needs than to use reflection.

+7
source

After the initial research, I think it is not possible to call setScrollbarFadingEnabled(false); at Spinner.

I read the implementation of the Spinner.java class and View.java (this last one implements the setScrollbarFadingEnabled(boolean) API16, and the problem is the line:

 cache.scrollBar.setAlpha(255); 

Probably cache.scrollBar is null at this point, and I have not found a way to force this attribute to be initialized.

Most of the methods that manage this attribute are protected or private, so we cannot just name them.

I will continue to research this problem in order to find a possible solution, but for now, and given that no one has answered this question yet, I think this is impossible.

0
source

Thanks to @nandeesh for his answer, and also @ Eng.Fouad for his / her answer on another page .

 public class VisibleScrollbarSpinner extends Spinner { @Override public boolean performClick() { final boolean superResult = super.performClick(); try { final Field mPopupField = Spinner.class.getDeclaredField("mPopup"); mPopupField.setAccessible(true); //noinspection ConstantConditions ((ListPopupWindow) mPopupField.get(this)).getListView().setScrollbarFadingEnabled(false); mPopupField.setAccessible(false); } catch (Exception e) { Log.d(TAG, e.toString(), e); } return superResult; } public VisibleScrollbarSpinner(Context context) { super(context); } public VisibleScrollbarSpinner(Context context, int mode) { super(context, mode); } public VisibleScrollbarSpinner(Context context, AttributeSet attrs) { super(context, attrs); } public VisibleScrollbarSpinner(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public VisibleScrollbarSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode) { super(context, attrs, defStyleAttr, mode); } } 
0
source

Source: https://habr.com/ru/post/957503/


All Articles