android实现多列显示的下拉列表框-Spinner

来源:互联网 发布:淘宝卖家插件 编辑:程序博客网 时间:2024/04/25 09:33

严格来说,这并不是一个下拉列表,只不过实现的效果很像是下拉列表

实现原理:

该下拉列表其实是一个AlertDialog.Builder,给它设置了一个以LinearLayout布局的View。该View内使用了一个GridView组件,

代码:

[java] view plaincopyprint?
  1. package yyy.testandroid9; 
  2.     private Intent intent; 
  3.     private int index =0
  4.     private SharedPreferences preferences; 
  5.     private File file; 
  6.     private Spinner spinner; 
  7.     private GridView gridView; 
  8.     private LinearLayout layout; 
  9.     private Builder builder; 
  10.     private AlertDialog dialog; 
  11.     private MyAdapter adapter; 
  12.  
  13.     /** Called when the activity is first created. */ 
  14.     @Override 
  15.     public void onCreate(Bundle savedInstanceState) { 
  16.         super.onCreate(savedInstanceState); 
  17.         setContentView(R.layout.main); 
  18.  
  19.         LayoutInflater inflater = LayoutInflater.from(this); 
  20.         layout = (LinearLayout) inflater.inflate(R.layout.bank_grid, null); 
  21.         gridView = (GridView) layout.findViewById(R.id.grid); 
  22.         adapter = new MyAdapter(this); 
  23.         gridView.setAdapter(adapter); 
  24.         button1 = (Button) findViewById(R.id.button1); 
  25.         button2 = (Button) findViewById(R.id.button2); 
  26.         textView = (TextView) findViewById(R.id.textview); 
  27.         builder = new Builder(TestAndroid9Activity.this); 
  28.         builder.setView(layout); 
  29.          
  30.          
  31.         button1.setOnClickListener(new OnClickListener() { 
  32.             public void onClick(View arg0) { 
  33.                 // TODO Auto-generated method stub 
  34.                  if(dialog == null){ 
  35.                      dialog = builder.show(); 
  36.                  } 
  37.                  dialog.show(); 
  38.                   
  39.             } 
  40.         }); 
  41.          
  42.          
  43.         gridView.setOnItemClickListener(new OnItemClickListener() { 
  44.             public void onItemClick(AdapterView<?> arg0, View arg1,int position, 
  45.                     long arg3) { 
  46.                 // TODO Auto-generated method stub 
  47.                 button1.setText(getResources().getStringArray(R.array.city)[position]); 
  48.                 dialog.dismiss(); 
  49.                 adapter.map.put(index, false); 
  50.                 adapter.map.put(position, true); 
  51.                 index = position; 
  52.                 adapter.notifyDataSetChanged(); 
  53.             } 
  54.         }); 
  55.          
  56.     } 
  57.  
  58.     private class MyAdapterextends BaseAdapter{ 
  59.         private Context context; 
  60.         private String[] citys; 
  61.         private LayoutInflater inflater; 
  62.         public HashMap<Integer, Boolean> map; 
  63.          
  64.         public MyAdapter(Context context) { 
  65.             super(); 
  66.             this.context = context; 
  67.             citys = context.getResources().getStringArray(R.array.city); 
  68.             inflater = LayoutInflater.from(context); 
  69.             map = new HashMap<Integer, Boolean>(); 
  70.             for(int i=0;i<citys.length;i++){ 
  71.                 map.put(i, false); 
  72.             } 
  73.         } 
  74.  
  75.         public int getCount() { 
  76.             // TODO Auto-generated method stub 
  77.             return citys.length; 
  78.         } 
  79.  
  80.         public Object getItem(int position) { 
  81.             // TODO Auto-generated method stub 
  82.             return null
  83.         } 
  84.  
  85.         public long getItemId(int position) { 
  86.             // TODO Auto-generated method stub 
  87.             return position; 
  88.         } 
  89.  
  90.         public View getView(int position, View view, ViewGroup parent) { 
  91.             // TODO Auto-generated method stub 
  92.             if(view == null){ 
  93.                 view = inflater.inflate(R.layout.bank_item,null); 
  94.             } 
  95.             TextView textView = (TextView) view.findViewById(R.id.list_text); 
  96. RadioButton radioButton = (RadioButton) view.findViewById(R.id.radiobutton);
  97.             radioButton.setChecked(map.get(position)); 
  98.             textView.setText(citys[position]); 
  99.             return view; 
  100.         } 
  101.     } 

bank_grid.xml

[java] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:orientation="vertical" 
  6.     android:padding="20dp"  
  7.     android:background="#ffffff" 
  8.     android:id="@+id/grid_layout"
  9.  
  10.      
  11.       
  12.     <GridView     
  13.         android:id="@+id/grid" 
  14.             android:layout_width="fill_parent" 
  15.             android:layout_height="fill_parent" 
  16.             android:verticalSpacing="20px" 
  17.             android:horizontalSpacing="10px" 
  18.             android:numColumns="4" 
  19.             android:scrollbars="vertical" 
  20.             android:layout_margin="10dp"/> 
  21.      
  22. </LinearLayout> 

bank_item.xml

[java] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:background="#ffffff" 
  6.      > 
  7.  
  8.      <RadioButton 
  9.         android:id="@+id/radiobutton" 
  10.         android:layout_width="wrap_content" 
  11.         android:layout_height="wrap_content" 
  12.         android:layout_marginRight="15dp" 
  13.         android:focusable="false" 
  14.         android:clickable="false" 
  15.         android:focusableInTouchMode="false" 
  16.         /> 
  17.     <TextView 
  18.         android:id="@+id/list_text" 
  19.         android:layout_width="wrap_content" 
  20.         android:layout_height="wrap_content" 
  21.         android:textColor="#000000"  
  22.         android:layout_gravity="center_vertical"/> 
  23.  
  24. </LinearLayout> 
原创粉丝点击