Android双层Adapter的使用(在ListView中添加Spinner)
最近做了一个Android的地图软件,用到了Adapter的嵌套,小结如下
需要实现的效果如下:
为了下拉列表Spinner中添加ImageView,本人使用了自定义Adapter子类的方法,为下拉列表添加自定义的View,类IconsAdapter的声明如下
1 package com.Android.Baidu.Layers; 2 3 import com.Android.Baidu.navisurveytest.MainActivity; 4 import com.Android.Baidu.navisurveytest.R; 5 6 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.view.ViewGroup.LayoutParams; 10 import android.widget.BaseAdapter; 11 import android.widget.ImageView; 12 import android.widget.LinearLayout; 13 14 15 public class IconsAdapter extends BaseAdapter { 16 17 @Override 18 public int getCount() { 19 // TODO Auto-generated method stub 20 return 6;/////////////////////////////////////////////////////////////////// 21 } 22 23 @Override 24 public Object getItem(int position) { 25 // TODO Auto-generated method stub 26 return null; 27 } 28 29 @Override 30 public long getItemId(int position) { 31 // TODO Auto-generated method stub 32 return 0; 33 } 34 35 @Override 36 public View getView(int position, View convertView, ViewGroup parent) { 37 // TODO Auto-generated method stub 38 LinearLayout linear = new LinearLayout(MainActivity.mContext); 39 linear.setOrientation(LinearLayout.HORIZONTAL); 40 ImageView imageView = new ImageView(MainActivity.mContext); 41 imageView.setLayoutParams(new LayoutParams(40, 40)); 42 43 // int[] markers = {R.drawable.marker0,R.drawable.marker1, 44 // R.drawable.marker2,R.drawable.marker3, 45 // R.drawable.marker4,R.drawable.marker5, 46 // R.drawable.marker6,R.drawable.marker7, 47 // R.drawable.marker8,R.drawable.marker9}; 48 49 int imgID = -1; 50 51 switch (position) { 52 case 0: 53 { 54 imgID = R.drawable.mark1; 55 break; 56 } 57 case 1: 58 { 59 imgID = R.drawable.mark2; 60 break; 61 } 62 case 2: 63 { 64 imgID = R.drawable.mark3; 65 66 break; 67 } 68 case 3: 69 { 70 imgID = R.drawable.mark4; 71 72 break; 73 } 74 case 4: 75 { 76 imgID = R.drawable.mark5; 77 78 break; 79 } 80 case 5: 81 { 82 imgID = R.drawable.mark6; 83 break; 84 } 85 default: 86 break; 87 } 88 89 //int t = markers[position]; 90 //Drawable drawable =MainActivity.instance.getResources().getDrawable(t); 91 //imageView.setImageDrawable(MainActivity.instance.getResources().getDrawable(MainActivity.instance.markers[position])); 92 //imageView.setImageDrawable(drawable); 93 imageView.setImageDrawable(MainActivity.mContext.getResources().getDrawable(imgID)); 94 linear.addView(imageView); 95 // 96 // TextView textView = new TextView(MainActivity.mContext); 97 // textView.setText(seqMarkerStrings[position]); 98 // textView.setTextColor(Color.BLUE); 99 // textView.setTextSize(30); 100 // linear.addView(textView); 101 102 return linear; 103 104 105 } 106 107 }
假如是添加String,就不需要那么麻烦了,网上关于String的Adapter一大堆。
接着,外层的Adapter——LayerAdapter,声明如下:
1 package com.Android.Baidu.Layers; 2 3 import com.Android.Baidu.navisurveytest.MainActivity; 4 import com.Android.Baidu.navisurveytest.R; 5 import com.baidu.platform.comapi.basestruct.GeoPoint; 6 7 import android.R.integer; 8 import android.app.Activity; 9 import android.app.ActivityManager; 10 import android.content.Context; 11 import android.content.SharedPreferences; 12 import android.graphics.Color; 13 import android.view.LayoutInflater; 14 import android.view.MotionEvent; 15 import android.view.View; 16 import android.view.ViewGroup; 17 import android.view.View.OnTouchListener; 18 import android.widget.AdapterView; 19 import android.widget.BaseAdapter; 20 import android.widget.Button; 21 import android.widget.ImageView; 22 import android.widget.LinearLayout; 23 import android.widget.Spinner; 24 import android.widget.TextView; 25 import android.widget.AdapterView.OnItemSelectedListener; 26 27 public class LayerAdapter extends BaseAdapter { 28 private int[] selectedIDs; 29 30 public LayerAdapter() { 31 // TODO Auto-generated constructor stub 32 selectedIDs = new int[getCount()]; 33 } 34 35 @Override 36 public int getCount() { 37 // TODO Auto-generated method stub 38 return MainActivity.fileNames.size(); 39 } 40 41 @Override 42 public Object getItem(int position) { 43 // TODO Auto-generated method stub 44 return null; 45 } 46 47 @Override 48 public long getItemId(int position) { 49 // TODO Auto-generated method stub 50 return 0; 51 } 52 53 public int getSelectedID(int position){ 54 return selectedIDs[position]; 55 } 56 57 58 @Override 59 public View getView(int position, View convertView, ViewGroup parent) { 60 // TODO Auto-generated method stub 61 View viewcache = ((LayoutInflater) MainActivity.mContext 62 .getSystemService(MainActivity.mContext.LAYOUT_INFLATER_SERVICE)) 63 .inflate(R.layout.layer, null); 64 65 66 TextView tv = (TextView)viewcache.findViewById(R.id.layerName); 67 68 final int selectID = position; 69 70 tv.setText(MainActivity.fileNames.get(position)); 71 72 Spinner sp = (Spinner)viewcache.findViewById(R.id.spinner); 73 sp.setAdapter(new IconsAdapter()); 74 sp.setSelection(MainActivity.instance.readIndex(selectID)); 75 sp.setOnItemSelectedListener(new OnItemSelectedListener() { 76 public void onItemSelected(AdapterView<?> adapterView, View v, 77 int position, long arg3) { 78 selectedIDs[selectID] = position; 79 } 80 81 public void onNothingSelected(AdapterView<?> arg0) { 82 83 } 84 }); 85 sp.setOnTouchListener(new OnTouchListener() { 86 87 @Override 88 public boolean onTouch(View v, MotionEvent event) { 89 // TODO Auto-generated method stub 90 return false; 91 } 92 }); 93 94 return viewcache; 95 96 97 } 98 99 }
同时,为了控制每一层中,层名和下拉列表sp的显示参数,使用xml文件定义输出layout,如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" 3 4 Android:layout_width="match_parent" 5 Android:layout_height="match_parent" 6 Android:gravity="center_horizontal" 7 Android:orientation="vertical" > 8 9 <RelativeLayout 10 Android:id = "@+id/layerRelative" 11 Android:layout_width="wrap_content" 12 Android:layout_height="wrap_content" 13 Android:layout_gravity="center_vertical|center_horizontal" 14 Android:orientation="horizontal" > 15 16 <TextView 17 Android:id="@+id/layerName" 18 Android:layout_width="wrap_content" 19 Android:layout_height="wrap_content" 20 Android:layout_alignParentLeft="true" 21 22 Android:layout_marginBottom="5dp" 23 Android:layout_marginLeft="100dp" 24 Android:layout_marginRight="10dp" 25 Android:layout_marginTop="5dp" 26 27 Android:textColor="#000000" 28 Android:textSize="20sp" /> 29 30 <Spinner 31 Android:id="@+id/spinner" 32 Android:layout_width="wrap_content" 33 Android:layout_height="wrap_content" 34 Android:layout_alignParentRight="true" 35 Android:layout_marginRight="100dp" 36 Android:drawSelectorOnTop="true" 37 Android:prompt="@string/sp1Prompt" /> 38 </RelativeLayout> 39 40 </LinearLayout>
在这里,之所以要是用final int 变量,是为每一个sp关联一个索引。因为,在MainActivity中,给ListView设置setOnItemSelectedListener事件,并不会触发,所以采用了这种方法。
接着,便是在MainActivity里面声明一个Dialog和ListView实例layerListView,并将ListView的实例layerListView作为Dialog的内容视图,进行显示,同时可以设置Dialog的大小。
1 layerAdapter = new LayerAdapter(); 2 3 layerListView = (ListView) convertView 4 .findViewById(R.id.layerListView); 5 6 layerListView.setAdapter(layerAdapter); 7 8 Dialog layersSettingDialog = new AlertDialog.Builder( 9 MainActivity.this) 10 .setTitle("图层符号样式选择") 11 .setPositiveButton("确定", 12 new DialogInterface.OnClickListener() { 13 public void onClick(DialogInterface dialog, 14 int which) { 15 SharedPreferences sharedPreferences = MainActivity.instance.getSharedPreferences( 16 configFileName, Activity.MODE_PRIVATE); 17 SharedPreferences.Editor edit = sharedPreferences.edit(); 18 edit.clear(); 19 edit.commit(); 20 for(int i = 0;i<MainActivity.fileNames.size();i++){ 21 writeIndex(MainActivity.fileNames.get(i), layerAdapter.getSelectedID(i)); 22 } 23 } 24 }) 25 .setNegativeButton("取消", 26 new DialogInterface.OnClickListener() { 27 public void onClick(DialogInterface dialog, 28 int which) { 29 // TODO Auto-generated method stub 30 } 31 }).setView(convertView).create(); 32 33 WindowManager m = getWindowManager(); 34 Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用 35 36 layersSettingDialog.show(); 37 //layersSettingDialog.getWindow().setLayout(300, 200);//This line must be put behind "show()" 38 int w = (int) (d.getHeight() * 0.6); 39 int h = (int) (d.getWidth() * 0.6); 40 41 42 // android.view.WindowManager.LayoutParams lParams = new android.view.WindowManager.LayoutParams(w, h); 43 // lParams.height = h; 44 // lParams.width = w; 45 // lParams.alpha = (float) 0.7; 46 //// lParams.x = (int) (d.getWidth() * 0.4); 47 //// lParams.y = (int) (d.getHeight() * 0.2); 48 // lParams.horizontalMargin = (float) ((float)w/3.0); 49 // lParams.verticalMargin = (float) ((float)h/3.0); 50 layersSettingDialog.getWindow().setLayout(500, 650);