I can't use API reflection on androidx.appcompat:appcompat:1.1.0

安稳与你 提交于 2021-02-05 12:04:00

问题


I have a problem with androidx.appcompat:appcompat:1.1.0. It's a new problem because on androidx.appcompat:appcompat:1.0.2 it does not exist.

I have a code that uses reflection to get mPopup Field from the spinner and set its height. It works very well on appcompat:1.0.2 but not on androidx.appcomppat:appcompat:1.1.0.

The code is

 private void setPopUp() {

        try {
            Field popup = getPopupField();

            // Get private mPopup member variable and try cast to ListPopupWindow
            final android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner);

            // Set popupWindow height to max - 40dp
            spinner.post(new Runnable() {
                @Override
                public void run() {
                    Rect r = new Rect();
                    spinner.getGlobalVisibleRect(r);
                    int height = 200;
                    popupWindow.setHeight(height);
                }
            });
        } catch (NoClassDefFoundError | ClassCastException | IllegalAccessException e) {
            // silently fail...
        }


    }

    private static Field getPopupField () {
        if (sPopupField == null) {
            try {
                Field popup = Spinner.class.getDeclaredField("mPopup");
                popup.setAccessible(true);

                sPopupField = popup;
            } catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException e) {
                // silently fail...
            }
        }
        return sPopupField;
    }

I read about bugs on a new appcompatActivity from appcompat:1.1.0. However, I can't find a solution to my problem.


回答1:


Everybody.

I was able to do it.

First, i put the spinner on the activity with java code. "programatically".

     public void initTest(){
    spinner2 = new Spinner(this, Spinner.MODE_DROPDOWN);
    spinner2.setAdapter(new ArrayAdapter(this, R.layout.spinner_item, datos));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        spinner2.setId(spinner2.generateViewId());
    }

    ConstraintLayout parentLayout = (ConstraintLayout)findViewById(R.id.main_activity);
    parentLayout.addView(spinner2, 0);

    ConstraintSet cs = new ConstraintSet();
    cs.clone(parentLayout);
    cs.setHorizontalBias(spinner2.getId(), 0.473F);
    cs.setVerticalBias(spinner2.getId(), 0.484F);
    cs.connect(spinner2.getId(), ConstraintSet.BOTTOM, parentLayout.getId(),ConstraintSet.BOTTOM);
    cs.connect(spinner2.getId(), ConstraintSet.START, parentLayout.getId(),ConstraintSet.START);
    cs.connect(spinner2.getId(), ConstraintSet.TOP, parentLayout.getId(),ConstraintSet.TOP);
    cs.connect(spinner2.getId(), ConstraintSet.END, parentLayout.getId(),ConstraintSet.END);
    // cs view id, else getId() returns -1

    // connect start and end point of views, in this case top of child to top of parent.
    // ... similarly add other constraints
    cs.applyTo(parentLayout);

}  

Then i call the code put at the beginning in my question.

I hope it works for many people, and to skip this error of the new version.

Regards.



来源:https://stackoverflow.com/questions/61704428/i-cant-use-api-reflection-on-androidx-appcompatappcompat1-1-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!