我的OnItemSelectedListener
有一个Spinner
,但是当所选项目与前一项目相同时,不会调用它。显然OnClickListener
不是Spinner
的选项。
每次用户点击某个项目时我都需要抓住。有什么想法吗?
也许这个Spinner
位于ActionBar
内部会扰乱正常行为?
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.tracklist_menu, menu);
Spinner spinner = (Spinner) menu.findItem(R.id.option_ordering_spinner)
.getActionView();
spinner.setAdapter(mSpinnerAdapter);
spinner.setSelection(PrefsHelper.getOrderingSpinnerPos(prefs));
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String str = "selected";
System.out.println(str);
if (optionMenuInitialized) {
switch (position) {
case 0:
// rdm
getActivity()
.sendBroadcast(
new Intent(
MyIntentAction.DO_RESHUFFLE_PLAYLIST));
smp.setCurrentTracklistCursorPos(-1);
trackAdapter.notifyDataSetChanged();
break;
case 1:
// artist
getActivity()
.sendBroadcast(
new Intent(
MyIntentAction.DO_ORDER_PLAYLIST_BY_ARTIST));
smp.setCurrentTracklistCursorPos(-1);
trackAdapter.notifyDataSetChanged();
break;
case 2:
// folder
getActivity()
.sendBroadcast(
new Intent(
MyIntentAction.DO_ORDER_PLAYLIST_BY_FOLDER));
smp.setCurrentTracklistCursorPos(-1);
trackAdapter.notifyDataSetChanged();
break;
}
PrefsHelper.setOrderingSpinnerPos(prefEditor, position);
prefEditor.commit();
}
optionMenuInitialized = true;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
答案 0 :(得分:84)
好的,我终于找到了一个解决方案,通过创建我自己的扩展Spinner的类:
public class MySpinner extends Spinner {
OnItemSelectedListener listener;
public MySpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setSelection(int position) {
super.setSelection(position);
if (listener != null)
listener.onItemSelected(null, null, position, 0);
}
public void setOnItemSelectedEvenIfUnchangedListener(
OnItemSelectedListener listener) {
this.listener = listener;
}
}
答案 1 :(得分:18)
我发现了这项工作,而不是提供的工作
/** Spinner extension that calls onItemSelected even when the selection is the same as its previous value */
public class NDSpinner extends Spinner {
public NDSpinner(Context context)
{ super(context); }
public NDSpinner(Context context, AttributeSet attrs)
{ super(context, attrs); }
public NDSpinner(Context context, AttributeSet attrs, int defStyle)
{ super(context, attrs, defStyle); }
@Override public void
setSelection(int position, boolean animate)
{
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position, animate);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
@Override public void
setSelection(int position)
{
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
}
答案 2 :(得分:5)
这里有一个更好的实施:
public class SpinnerPlus extends Spinner {
AdapterView.OnItemSelectedListener listener;
public SpinnerPlus(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setSelection(int position) {
super.setSelection(position);
if (listener != null)
listener.onItemSelected(this, getSelectedView(), position, 0);
}
public void setOnItemSelectedEvenIfUnchangedListener(
AdapterView.OnItemSelectedListener listener) {
this.listener = listener;
}
}
答案 3 :(得分:4)
尽管选择了最后一个索引的值,但要使微调器更改,只需使用:
spinner.setSelection(0);
在调用其他选择之前
spinner.setSelection(number);
这样,微调器将触发两次 OnItemSelected 事件。只要确保它第二次做你需要的任何事情。
答案 4 :(得分:2)
如果它仍然是实际的,则应该正确调用回调
@Override
public void setSelection(int position) {
super.setSelection(position);
if(listener != null)
listener.onItemSelected(this, getChildAt(position), position, 0);
}
马丁
答案 5 :(得分:2)
重写常见的解决方案,但具有:
AppCompatSpinner
扩展OnItemSelectedListener
侦听器,而不是创建自己的侦听器这里:
import android.content.Context;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatSpinner;
public class FixedSpinner extends AppCompatSpinner {
// add other constructors that you need
public FixedSpinner(Context context, int mode) {
super(context, mode);
}
private void processSelection(int position) {
boolean sameSelected = position == getSelectedItemPosition();
final OnItemSelectedListener listener = getOnItemSelectedListener();
if (sameSelected && listener != null) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
listener.onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
@Override
public void setSelection(int position) {
processSelection(position);
super.setSelection(position);
}
@Override
public void
setSelection(int position, boolean animate) {
processSelection(position);
super.setSelection(position, animate);
}
@Override
public void setOnItemSelectedListener(@Nullable OnItemSelectedListener listener) {
// hack for initial listener call
setSelection(0, false);
super.setOnItemSelectedListener(listener);
}
}
答案 6 :(得分:1)
最简单的解决方案:
spinner.performItemClick(视图,位置,ID)
答案 7 :(得分:0)
我遇到了同样的问题,每次适配器更改项目时都通过设置onItemSelectedListener来解决。
答案 8 :(得分:0)
这是一个更好的实现-
自定义微调器类-
import android.content.Context;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatSpinner;
public class CSpinner extends AppCompatSpinner {
private int lastPosition = 0;
public CSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setSelection(int position) {
super.setSelection(position);
boolean sameSelected = lastPosition == getSelectedItemPosition();
OnItemSelectedListener onItemSelectedListener = getOnItemSelectedListener();
if (sameSelected && onItemSelectedListener != null) {
onItemSelectedListener.onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
lastPosition = position;
}
}
设置侦听器-
spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("onItemSelected", String.valueOf(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案 9 :(得分:-1)
我找到了一个简单的解决方案
再次调用setAdapter代替第二个微调器的notifyDataSetChanged