当你实现SpinnerAdapter时,你会得到getDropDownView,它与需要扩展BaseAdapter时拥有的getView有什么不同。
private void initView() {
SpinnerDropDownAdapter sddadapter = new SpinnerDropDownAdapter(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sddadapter.name);
Spinner getViewSP = (Spinner) findViewById(R.id.getview_sp);
getViewSP.setAdapter(adapter);
Spinner getViewWDropDownSP = (Spinner) findViewById(R.id.getview_w_drop_down_sp);
getViewWDropDownSP.setAdapter(sddadapter);
}
static class SpinnerDropDownAdapter extends BaseAdapter implements
SpinnerAdapter {
Context context;
SpinnerDropDownAdapter(Context ctx) {
context = ctx;
}
String[] name = { " One", " Two", " Three", " Four", " Five", " Six",
" Seven", " Eight" };
String[] value = { " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8" };
@Override
public int getCount() {
return name.length;
}
@Override
public String getItem(int pos) {
// TODO Auto-generated method stub
return name[pos];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text = new TextView(context);
text.setTextColor(Color.BLACK);
text.setText(name[position]);
return text;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
TextView text = new TextView(context);
text.setTextColor(Color.BLACK);
text.setText(value[position]);
return text;
}
}
接受的答案似乎有点复杂,所以为了简单起见
getView
- 用户按下Spinner之前显示的值,在convertView参数的帮助下可以调整每个值的视图
getDropDownView
- 用户按下Spinner后可以选择的值列表,在convertView参数的帮助下可以适应列表中的每个值
View
,而不是值。getView()
是用于在Spinner
被点击之前看到的单个View
。getDropDownView()
用于在Spinner
被点击时看到的垂直堆栈View
。您可以使用这两种方法根据需要创建/填充两种类型的View
。 - ban-geoengineeringgetView
和getDropDownView
可以显示不同的布局。
我创建了一个Spinner并使用class ImageTextAdapter extends ArrayAdapter<String>
作为适配器。我重写getView
,以便它可以同时显示 ImageView
和 TextView
。但是,我没有重写getDropDownView
。
这是下拉列表之前的屏幕截图:
这是下拉列表之后的屏幕截图:
我使用以下代码初始化了Spinner。
String[] spinnerTexts = {"Tanzania", "Thailand"};
int[] spinnerImages = {R.drawable.tanzania, R.drawable.thailand};
ArrayAdapter<String> adapter = new ImageTextAdapter(this, spinnerTexts, spinnerImages);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
public class ImageTextAdapter extends ArrayAdapter<String> {
int[] images;
public ImageTextAdapter(Context ctx, String[] texts, int[] images) {
super(ctx, android.R.layout.simple_spinner_item, texts);
this.images = images;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = getLayoutInflater().inflate(R.layout.row, parent, false);
TextView textView = (TextView) row.findViewById(R.id.textView);
textView.setText(getItem(position));
ImageView imageView = (ImageView) row.findViewById(R.id.imageView);
imageView.setImageResource(images[position]);
return row;
}
}
public class FolderSpinnerAdpater extends BaseAdapter implements SpinnerAdapter {
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return null;
}
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getView(position, convertView, parent);
}
重写 getView()
和 getDropDownView()
方法,返回不同的视图,你会发现它们之间的差异。