Custom Spinner Does Not Hide Dropdown Menu After Selecting

(Android API version 9) I created a counter with a custom adapter and redefined getView () to inflate it with my xml file, which has a textual representation. But now my counter does not close the drop-down list after the user selects an item. In any case, to close the drop-down list of the counter when selecting an item?

code

//Code in onCreate function
    Spinner list = (Spinner) findViewById(R.id.spn_purchaseList);
    listAdapter = new ItemListAdapter(this, new MyItemList());
    list.setAdapter(listAdapter);
    listAdapter.item_list.addItem(new MyItem("Test", "Test Item"));
    listAdapter.notifyDataSetChanged();
//onCreate end
//the class below is inside "MainActivity extends Activity"
class ItemListAdapter extends BaseAdapter
{
    Context context;
    MyItemList item_list;
    MyItem selectedItem;


    ItemListAdapter(Context con,MyItemList k)
    {
        super();
        this.context=con;
        this.item_list=k;
        selectedItem=null;
    }

    @Override
    public int getCount() {

        return item_list.getCount();
    }

    @Override
    public MyItem getItem(int arg0) {

        return this.item_list.getList().get(arg0);
    }

    @Override
    public long getItemId(int arg0) {

        return  this.item_list.getPosition(this.item_list.getList().get(arg0));
    }

    @Override
    public View getView(int position, View arg1, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        View spinner_item = inflater.inflate(R.layout.spinner_layout, parent, false);

        TextView tx = (TextView)spinner_item.findViewById(R.id.txt_spinner);
        tx.setId((int) item_list.getPosition(item_list.getList().get(position)));


        tx.setText(this.item_list.getList().get(position).name.toString());
        tx.setBackgroundResource(R.drawable.spinner_item);

        tx.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                selectedItem = item_list.getItem(v.getId());
                list.setSelection(v.getId());



            }
        });

        return spinner_item;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent)
    {


        return getView(position,convertView,parent);

    }

}
+4
source share
3 answers

Calling setVisibility (View.GONE) works to hide the drop-down menu, but it seems to cause problems with the Spinner state, i.e. You will not be able to re-open the drop-down list after closing it.

Spinner onDetachedFromWindow() onClick().

@Override
public void onClick(View v) {
    // code here to get selected item and do something with it

    // hide the spinner dropdown
    Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
    if (spinner != null) {
        try {
            Method method = Spinner.class.getDeclaredMethod("onDetachedFromWindow");
            method.setAccessible(true);
            method.invoke(spinner);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
+1

, . clickable="true" onClickListeners, onItemSelectedListeners , .

, android:background="?attr/selectableItemBackground" OnItemSelectedListener() Spinner, . Spinner .

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="?attr/selectableItemBackground">

         <!-- your custom spinner item view -->

    </LinearLayout>

</LinearLayout>

, , .

0

just call the spinner.dismissDropDown () method for the spinner, inside on click on the element. Your problem will be resolved.

0
source

All Articles