码
//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); }}解决方法 调用setVisibility(VIEw.GONE)可以隐藏下拉列表,但它似乎会导致Spinner状态出现问题,即在关闭后您将无法重新打开下拉列表.
首选方法是获取Spinner的句柄并从onClick()侦听器调用它的onDetachedFromWindow().
@OverrIDepublic 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(); } }}总结
以上是内存溢出为你收集整理的android – 自定义Spinner在选择后不隐藏下拉菜单全部内容,希望文章能够帮你解决android – 自定义Spinner在选择后不隐藏下拉菜单所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)