我已經有了基本的android微調控制器,我想在點擊它後顯示帶有其中一個突出顯示的項目的列表,即最初選定的項目。Android微調器突出顯示所選項目
就像是在這裏完成: http://www.warriorpoint.com/blog/wp-content/uploads/2009/05/05spinner-thumb.png
但是我自己的項目佈局,而不是用單選框,但我自己的背景來代替。
我該如何做到這一點?在選擇器中是否有任何用處,或者是否必須以編程方式執行?
任何幫助表示讚賞。
我已經有了基本的android微調控制器,我想在點擊它後顯示帶有其中一個突出顯示的項目的列表,即最初選定的項目。Android微調器突出顯示所選項目
就像是在這裏完成: http://www.warriorpoint.com/blog/wp-content/uploads/2009/05/05spinner-thumb.png
但是我自己的項目佈局,而不是用單選框,但我自己的背景來代替。
我該如何做到這一點?在選擇器中是否有任何用處,或者是否必須以編程方式執行?
任何幫助表示讚賞。
回答我的問題,你需要有這樣的事情:
public class mySpinnerAdapter extends SimpleCursorAdapter implements SpinnerAdapter {
public OrderSpinnerAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
// TODO whatever you need
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false);
}
// TODO set the background color of the convertView
// depending on your wishes
return convertView;
}
}
這樣我們就可以控制這樣的下拉列表的創建。如果你需要不同的選擇器,你可以在XML文件中輕鬆完成。
然後,只需簡單地創建適配器並使用setAdapter方法將它們綁定到微調器。
public class MainActivity extends Activity {
Spinner mySpinner;
String[] myArray={"1","2","3","4","5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySpinner = (Spinner)findViewById(R.id.spinner_test);
mySpinner.setAdapter(new MyAdapter(this,android.R.layout.simple_spinner_item, myArray));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt){
LayoutInflater inflater = getLayoutInflater();
View spinnerItem = inflater.inflate(android.R.layout.simple_spinner_item, null);
TextView mytext= (TextView)spinnerItem.findViewById(android.R.id.text1);
mytext.setText(myArray[position]);
//int selected = Spinner.
int selected = mySpinner.getSelectedItemPosition();
if(position == selected){
spinnerItem.setBackgroundColor(Color.BLUE);
}
return spinnerItem;
}
}
}
這應該有所幫助。
下面是我測試和驗證的解決方案。您可以使用setSelection(N)突出顯示您想要的項目。
class HighLightArrayAdapter extends ArrayAdapter<CharSequence> {
private int mSelectedIndex = -1;
public void setSelection(int position) {
mSelectedIndex = position;
notifyDataSetChanged();
}
public HighLightArrayAdapter(Context context, int resource, CharSequence[] objects) {
super(context, resource, objects);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View itemView = super.getDropDownView(position, convertView, parent);
if (position == mSelectedIndex) {
itemView.setBackgroundColor(Color.rgb(56,184,226));
} else {
itemView.setBackgroundColor(Color.TRANSPARENT);
}
return itemView;
}
}
很好的回答!爲了清楚起見,'setSelection(pos)'需要被添加到微調器的onItemSelected()監聽器中。 – daisura99