Weird Spinner behaviour

☆樱花仙子☆ 提交于 2019-12-11 18:21:39

问题


I have used a custom adapter for populating my Spinner. I have overriden getDropDownView from which I return the view of each row of the dropdown list. Everything works fine except the dropdown list rendered is not getting the width of the Spinner widget. Rather it gets Like this:

So the dropdown list is missing the highlighted width. I dont know why this is happening. I want it to get the full width of the spinner.

My custom adapter:

class CategorySpinnerAdapter extends ArrayAdapter{

        private Activity context;
        ArrayList<Category> categoryList;
        public CategorySpinnerAdapter(Activity context,int resourceID,ArrayList<Category> categoryList)
        {
            super(context,resourceID,categoryList);

            this.context=context;
            this.categoryList=categoryList;
        }

        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub

            if(convertView==null)
            {
                LayoutInflater inflater=context.getLayoutInflater();
                convertView=inflater.inflate(R.layout.category_spinner_row, parent,false);
            }

            Category currentCategory=categoryList.get(position);

            TextView categoryText=(TextView) convertView.findViewById(R.id.spinnerText);
            categoryText.setText(currentCategory.getCategoryName());

            return convertView;
        }
    }

Code, where I am setting this adapter:

Spinner categorySpinner=(Spinner) getActivity().findViewById(R.id.categorySpinner);
        ArrayList<Category> categoryList=populateCategoryList();

        CategorySpinnerAdapter categorySpinnerAdapter=new CategorySpinnerAdapter(getActivity(), android.R.layout.simple_spinner_item,categoryList);

        categorySpinner.setAdapter(categorySpinnerAdapter);

        categorySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
                    int position, long id) {
                // TODO Auto-generated method stub

                    ArrayList<Reward> modifiedList=new ArrayList<Reward>();
                    //test case: category OK
                        int categoryID=position+1;
                        for(int i=0;i<rewardList.size();i++)
                        {
                            if(rewardList.get(i).getCategoryID()==categoryID)
                            {
                                modifiedList.add(rewardList.get(i));
                            }
                        }

                        adapter.changeDataSet(modifiedList);

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
                //get default ELECTRONICS category 1 data to populate the list
                ArrayList<Reward> defaultCategorizedList=new ArrayList<Reward>();
                //test case: category OK

                    for(int i=0;i<rewardList.size();i++)
                    {
                        if(rewardList.get(i).getCategoryID()==1)
                        {
                            defaultCategorizedList.add(rewardList.get(i));
                        }
                    }

            }
        });

Declaration of the Spinner Item inside the main xml:

 <Spinner
        android:id="@+id/categorySpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_below="@+id/customerRewardPointsTextView"
        android:background="@drawable/btn_dropdown"
        android:spinnerMode="dropdown" />

layout for the dropdown items, category_spinner_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:background="@drawable/category_spinner_background" >

    <TextView
        android:id="@+id/spinnerText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ellipsize="marquee"
        android:gravity="center"
        android:singleLine="true" />

</RelativeLayout>

How can I fix this issue ?


回答1:


try this one:

convertView=inflater.inflate(android.R.layout.simple_list_item_1, parent,false);



回答2:


The problem starts because of "android:background="@drawable/btn_dropdown"" this portion of code. When you remove this you will see the spinner is working as it is supposed to, that means btn_dropdown drawable is conflicting with your spinner's width and eventually it's popup window that's why you are seeing this weird behaviour. I would suggest you to adjust your btn_dropdown drawable so that it does not conflict with spinner's default behaviour. Take a look at these post:

  • how-to-design-spinner-in-android
  • change-spinner-background

I haven't tried those but i think you will find what you need.



来源:https://stackoverflow.com/questions/20220758/weird-spinner-behaviour

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