美好的一天。
我尝试从Spinner下拉列表中选择项目。但是,当我点击下拉列表项时,下拉列表des不会关闭。
我开始调试并意识到当我点击下拉列表项时,不会触发onItemSelected方法。但是,首次初始化下拉列表时,会触发该方法。
单击Spinner下拉列表项时是否有任何方法可以触发onItemSelected?
下面是我的Spinner Activity和我的Cursor Adapter类的代码。
public class ActivitySearch extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>, AdapterView.OnItemSelectedListener {
private EditText mSearchTextInput;
private TextInputLayout mSearchTextTextInputLayout;
private AppCompatSpinner mMySpinner;
private AppCompatButton mSearchButton;
private String mModName;
private SearchTextWithSpinnerAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_text_with);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
mSearchTextInput = (EditText) findViewById(R.id.searchTextInput);
mSearchTextTextInputLayout = (TextInputLayout) findViewById(R.id.searchTextTextInputLayout);
mMySpinner= (AppCompatSpinner) findViewById(R.id.mMySpinner);
mSearchButton = (AppCompatButton) findViewById(R.id.searchButton);
setSupportActionBar(toolbar);
if(getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
Bundle extras = getIntent().getExtras();
int defaultModName;
if(extras != null) {
mSearchTextInput.setText(extras.getString(ARG_SEARCH_KEYWORD, ""));
mModName = extras.getString(ARG_MOD_NAME, "");
} else {
Toast.makeText(this, getString(R.string.toast_invalid_data), Toast.LENGTH_LONG).show();
}
//Set adapter for spinner list
mAdapter = new SearchTextWithSpinnerAdapter(this, null, -1, new SearchTextWithSpinnerAdapter.ListViewListener() {
@Override
public void onSpinnerItemClicked(String count) {
mMySpinner.performClick();
int defaultModName;
.........................
mMySpinner.setSelection(defaultModName);
if (mMySpinner!= null) {
try {
Method method = Spinner.class.getDeclaredMethod("onDetachedFromWindow");
method.setAccessible(true);
method.invoke(mMySpinner);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
mMySpinner.setAdapter(mAdapter);
getSupportLoaderManager().initLoader(0, null, this);
mMySpinner.setOnItemSelectedListener(this);
mMySpinner.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(mMySpinner.getWindowToken(), 0);
return false;
}
});
mMySpinner.setSelection(defaultModName);
mMySpinner.setSelected(true);
}
@Override
protected void onResume() {
super.onResume();
EventBus.getDefault().register(this);
}
@Override
protected void onPause() {
super.onPause();
EventBus.getDefault().unregister(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
mMySpinner.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = { }...
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.changeCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// data is not available anymore, delete reference
if(mAdapter != null) {
mAdapter.swapCursor(null);
}
}
这是我的CursorAdapter:
public class SpinnerAdapter extends CursorAdapter {
private LayoutInflater mInflater;
private ListViewListener mCallback;
public interface ListViewListener {
void onSpinnerItemClicked(String count);
}
private static class ViewHolder {
LinearLayout myRow;
ImageView profileImage;
TextView NameText;
TextView DescText;
}
public SpinnerAdapter (Activity activity, Cursor data, int flags, ListViewListener listener) {
super(activity, data, flags);
mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mCallback = listener;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mInflater.inflate(R.layout.row_view_activity_search_text_with, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.Row = (LinearLayout) view.findViewById(R.id.myRow);
viewHolder.profileImage = (ImageView) view.findViewById(R.id.profileImage);
viewHolder.NameText = (TextView) view.findViewById(R.id.NameText);
viewHolder.DescText = (TextView) view.findViewById(R.id.DescText);
view.setTag(viewHolder);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
viewHolder.Row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCallback != null) {
mCallback.onSpinnerItemClicked(xxx);
}
}
});
}
}
非常感谢任何帮助。
谢谢。