在片段中我有片段,我将自定义微调器
public class ModeManual extends Fragment {
TextView textDetail;
private Bundle arguments;
private ArrayList<LvItem> myItems = new ArrayList<LvItem>();
public static final ModeManual newInstance(String myString) {
ModeManual f = new ModeManual();
Bundle arguments = new Bundle();
arguments.putString("myTxt", myString);
f.setArguments(arguments);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
arguments = getArguments();
View v = inflater.inflate(R.layout.fragment_manual_mode, viewGroup, false);
final MySpinner spinner = (MySpinner) v.findViewById(R.id.spinner);
//arrayadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(new ItemAdapter(getActivity().getBaseContext(), R.layout.my_list_item, spinner.getItems()));
return v;
}
} Spinner类是
public class MySpinner extends Spinner implements AdapterView.OnItemSelectedListener{
private String[] itemText = null;//holds the title for each item
private String[] itemEntryVal = null;//holds the value for each item
private String listDesc, selectedVal, crtCtrlKey, defaultVal;
private int[] itemIco;//holds the icon resource id for each item
private int idx;
private List<LvItem> myItems;
private Context context;
private ImageView myIcon;
private SharedPreferences preferences;
private TextView myTitle;
public MySpinner(Context context) {
super(context);
}
public MySpinner(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
Resources resources = context.getResources();
preferences = PreferenceManager.getDefaultSharedPreferences(context);
ItemCollection coll = new ItemCollection(context,
attrs,
R.styleable.MySpinner,
R.styleable.MySpinner_spinIco,
R.styleable.MySpinner_android_entries,
R.styleable.MySpinner_spinDesc,
R.styleable.MySpinner_android_entryValues,
R.styleable.MySpinner_android_key,
R.styleable.MySpinner_android_defaultValue);
listDesc = coll.getStrDesc();
selectedVal = coll.getSelVal();
crtCtrlKey = coll.getCtrlKey();
defaultVal = coll.getDefVal();
itemIco = coll.getIcons();
itemText = coll.getText();
itemEntryVal = coll.getEntries();
idx = Integer.parseInt(coll.getSelVal());
}
public void setItem(int index, AdapterView<?> parentView){
ItemAdapter adapter = (ItemAdapter) parentView.getAdapter();
Log.v("setItem", "setItem " + index + "items ");
for (int i = 0; i < adapter.getCount(); i++) {
LvItem item = adapter.getItem(i);
boolean sel = index == i ? true : false;
item.setItemChecked(sel);
}
SharedPreferences.Editor editor = preferences.edit();
editor.putString(crtCtrlKey, itemEntryVal[index]);
editor.commit();
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//My Code
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
//myCode
}
public ArrayList getItems() {
ArrayList<LvItem> myItems = new ArrayList<LvItem>();
for (int i = 0; i < itemText.length; i++) {
myItems.add(new LvItem(itemIco[i], itemText[i], listDesc +itemText[i], selectedVal.equals(itemEntryVal[i]) ? true : false));
}
return myItems;
}
}
如何从片段中设置onItemSelected?或者应该如何停止?
这通常在片段/活动中实现,但我会在不同的片段中使用类似的控制微调器,所以我希望onItemSelected对所有人都是通用的。
答案 0 :(得分:0)
您不需要在OnItemSelectedListener
中实施MySpinner
,您可以像这样在片段/活动中执行此操作。
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案 1 :(得分:0)
我已经解决了以下问题
片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
arguments = getArguments();
View v = inflater.inflate(R.layout.fragment_manual_mode, viewGroup, false);
final MySpinner spinner = (MySpinner) v.findViewById(R.id.spinner);
return v;
}
和MySpinner
public class MySpinner extends Spinner implements AdapterView.OnItemSelectedListener{
private String[] itemText = null;//holds the title for each item
private String[] itemEntryVal = null;//holds the value for each item
private String listDesc, selectedVal, crtCtrlKey, defaultVal;
private int[] itemIco;//holds the icon resource id for each item
private int idx;
private Context context;
private ImageView myIcon;
private SharedPreferences preferences;
private TextView myTitle;
ItemAdapter myAdapter;
public MySpinner(Context context) {
super(context);
super.setOnItemSelectedListener(this);
}
public MySpinner(Context context, AttributeSet attrs) {
super(context, attrs);
super.setOnItemSelectedListener(this);
this.context = context;
Resources resources = context.getResources();
preferences = PreferenceManager.getDefaultSharedPreferences(context);
ItemCollection coll = new ItemCollection(context,
attrs,
R.styleable.MySpinner,
R.styleable.MySpinner_spinIco,
R.styleable.MySpinner_android_entries,
R.styleable.MySpinner_spinDesc,
R.styleable.MySpinner_android_entryValues,
R.styleable.MySpinner_android_key,
R.styleable.MySpinner_android_defaultValue);
listDesc = coll.getStrDesc();
selectedVal = coll.getSelVal();
crtCtrlKey = coll.getCtrlKey();
defaultVal = coll.getDefVal();
itemIco = coll.getIcons();
itemText = coll.getText();
itemEntryVal = coll.getEntries();
idx = Integer.parseInt(coll.getSelVal());
initialize();
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(i!=idx) {
int cnt = adapterView.getCount();
for (int j = 0; j < cnt; j++) {
LvItem item = (LvItem) adapterView.getItemAtPosition(j);
item.setItemChecked(i == j ? true : false);
}
myAdapter.notifyDataSetChanged();
idx = i;
SharedPreferences.Editor editor = preferences.edit();
editor.putString(crtCtrlKey, itemEntryVal[i]);
editor.commit();
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
private void initialize() {
ArrayList<LvItem> myItems = new ArrayList<LvItem>();
for (int i = 0; i < itemText.length; i++) {
myItems.add(new LvItem(itemIco[i], itemText[i], listDesc +itemText[i], selectedVal.equals(itemEntryVal[i]) ? true : false));
}
myAdapter = new ItemAdapter(super.getContext(), R.layout.my_list_item, myItems);
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
setAdapter(myAdapter);
super.setSelection(idx);
}
}