我想在spinner
的每个项目中显示不同的图片视图,并指明其位置。我的问题是,spinner
只能显示一个ImageView
。
states = getResources().getStringArray(R.array.Facilities);
imgs = getResources().obtainTypedArray(R.array.popup);
image = (ImageView) findViewById(R.id.country_image);
spinner = (Spinner) findViewById(R.id.country_spinner);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, states);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
image.setImageResource(imgs.getResourceId(
spinner.getSelectedItemPosition(), -1));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
答案 0 :(得分:0)
我建议您使用自定义适配器并使用它来扩充自定义布局
row.xml
<?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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
以下是CustomAdaoter,它在微调器
中膨胀上面的布局public class CustomAdapter extends ArrayAdapter {
Context context;
int images[];
public CustomAdapter(Context ctx, int txtViewResourceId, String[] objects, int[] images)
{
super(ctx, txtViewResourceId, objects);
this.context=ctx;
this.images = images;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return customView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return customView(position, convertView, parent);
}
public View customView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, parent, false);
TextView imgPosition = (TextView) convertView.findViewById(R.id.position);
imgPosition.setText(position);
ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
imageView.setImageResource(images[position]);
return convertView;
}
//Override the getCount() because we are passing object as null in constructor
//getCount() determines how many times the getView should b called
@Override
public int getCount() {
return images.length;
}
}
在要显示微调器的活动中调用它
public class MainActivity extends ActionBarActivity {
Spinner spinner;
int imgData[] = {R.drawable.ic_action_alarms,R.drawable.ic_action_discard,R.drawable.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
CustomAdapter adapter = new CustomAdapter(getBaseContext(),R.layout.row,null,imgData);
spinner.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
修改强> 按如下方式实现CustomView方法:
public View customView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, parent, false);
ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
imageView.setImageResource(images[position]);
return convertView;
}