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

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

实现原理:

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

代码:

  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 MyAdapter extends 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.     } 
package yyy.testandroid9;
	private Intent intent;
	private int index = 0;
	private SharedPreferences preferences;
	private File file;
	private Spinner spinner;
	private GridView gridView;
	private LinearLayout layout;
	private Builder builder;
	private AlertDialog dialog;
	private MyAdapter adapter;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		LayoutInflater inflater = LayoutInflater.from(this);
		layout = (LinearLayout) inflater.inflate(R.layout.bank_grid, null);
		gridView = (GridView) layout.findViewById(R.id.grid);
		adapter = new MyAdapter(this);
		gridView.setAdapter(adapter);
		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		textView = (TextView) findViewById(R.id.textview);
		builder = new Builder(TestAndroid9Activity.this);
		builder.setView(layout);
	    
		
		button1.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				 if(dialog == null){
					 dialog = builder.show();
				 }
				 dialog.show();
				 
			}
		});
		
		
		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				button1.setText(getResources().getStringArray(R.array.city)[position]);
				dialog.dismiss();
				adapter.map.put(index, false);
				adapter.map.put(position, true);
				index = position;
				adapter.notifyDataSetChanged();
			}
		});
		
	}

	private class MyAdapter extends BaseAdapter{
		private Context context;
		private String[] citys;
		private LayoutInflater inflater;
        public HashMap<Integer, Boolean> map;
		
		public MyAdapter(Context context) {
			super();
			this.context = context;
			citys = context.getResources().getStringArray(R.array.city);
			inflater = LayoutInflater.from(context);
			map = new HashMap<Integer, Boolean>();
			for(int i=0;i<citys.length;i++){
				map.put(i, false);
			}
		}

		public int getCount() {
			// TODO Auto-generated method stub
			return citys.length;
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View view, ViewGroup parent) {
			// TODO Auto-generated method stub
			if(view == null){
				view = inflater.inflate(R.layout.bank_item, null);
			}
			TextView textView = (TextView) view.findViewById(R.id.list_text);
			RadioButton radioButton = (RadioButton) view.findViewById(R.id.radiobutton);
			radioButton.setChecked(map.get(position));
			textView.setText(citys[position]);
			return view;
		}
	}

bank_grid.xml

  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> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="20dp" 
    android:background="#ffffff"
    android:id="@+id/grid_layout">

	
	 
	<GridView	
	    android:id="@+id/grid"
  			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:verticalSpacing="20px"
			android:horizontalSpacing="10px"
			android:numColumns="4"
			android:scrollbars="vertical"
			android:layout_margin="10dp"/>
	
</LinearLayout>

bank_item.xml

  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> 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值