rey5137 / material

A library to bring fully animated Material Design components to pre-Lolipop Android.
http://rey5137.com/material/
Apache License 2.0
6k stars 1.32k forks source link

Setting adapter again adds new views in Spinners #378

Closed DearDhruv closed 1 year ago

DearDhruv commented 8 years ago

Hello @rey5137 I've been using this lib and it is very nice with many features.

Just recently I found out a bug in Spinner. If I set adapter more than once. It adds more views as child. I was adjusting the size of the child and found out the same.

For now I'm removing view -> spinner.removeAllViews(); and then I set adapter If I wanted to change the values or setting new adapter.

Please have look or let me know If I'm missing something. Thanks

rey5137 commented 8 years ago

Can you please provide your code?

DearDhruv commented 8 years ago

Hello @rey5137 Here I'm setting and updating design of spinner

    private void initCitySpinner() {

        // Spinner Drop down City elements
        List<String> cities = new ArrayList<>();
        cities.add(getString(R.string.str_hint_city));

        Cities citiesObject = getApp().getSettings().getCities();

        for (City city : citiesObject.getCities()) {
            cities.add(city.getName());
        }

        // Creating adapter for City spinner
        ArrayAdapter<String> citiesAdapter = new ArrayAdapter<>(this,
                                                                android.R.layout
                                                                        .simple_spinner_item,
                                                                cities);
        citiesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spnCity.setAdapter(citiesAdapter);
        spnCity.setOnItemSelectedListener(this);
        spnCity.setSelection(0);
        adjustSpinnerView(spnCity);
    }

    private void adjustSpinnerView(Spinner spinner) {

        TextView textView = (TextView) spinner.getSelectedView();// ((TextView) spinner
        // .getChildAt(0));
        if (spinner.getSelectedItemPosition() > 0) {
            textView.setTextColor(Color.BLACK);
            ((TextView) spinner.getChildAt(0)).setTextColor(Color.TRANSPARENT);
        } else {
            textView.setTextColor(getResources().getColor(R.color.hint_grey));
        }

        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                             getResources().getDimension(R.dimen.sp_18));
        textView.setPadding(0, 0, 0, 0);

        spinner.setEnabled(true);
    }

Above code gives this output: spinner

Region data depends on Country selection. And City data depends on Region selection.

onItemSelected for Spinner I call above method adjustSpinnerView(parent);

Now If I do not call adjustSpinnerView(parent); on onItemSelected it gives below output. If I do not adjust text on selection then it would go to default size.

bad_spinner

I've updated code after opening issue, So for now I'm not removing the views. But every time I have to call adjustSpinnerView(parent);.

Let me know any update. Thanks. @DearDhruv

rey5137 commented 8 years ago

I don't see where you set adapter more than once in your code. Can you make an example project so I can test it?

DearDhruv commented 8 years ago

Region data depends on Country selection. And City data depends on Region selection. onItemSelected for Spinner I call above method adjustSpinnerView(parent);

So on selection of Country I set adapter again with data updated to Region.

Anyway I'm out of time. I've added some tweak. well it is boiler plate and executes more code. but for now I'll be ok with it. Thanks.

Just updated gradle and now getting Error:Could not find com.github.rey5137:material:1.2.3

I still have many bugs to report; that I'll do in free time. Thanks again. @DearDhruv