我同时使用spinner和listview,我有一些逻辑,当我在列表上滚动时,我有一个数据指示,告诉我在spinner上设置哪些值,并且我使用
listView.setSelection(somePosition);
当我点击Spinner时,它还有一些数据表明要设置
的位置spinnerSurah.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int clickPosition, long l) {
int skipTotal = 0;
for(int i = 0 ; i < clickPosition ; i++)
{
SafeJSONObject surahObject = jsonArraySurahList.getJSONObject(i);
skipTotal+= surahObject.getInt("ayas");
}
SafeJSONObject surahObject = jsonArraySurahList.getJSONObject(clickPosition);
Log.e("spinnerSurah","spinnerSurah surahObject "+surahObject.toString());
positionSelection = skipTotal;
listView.setSelection(positionSelection);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
问题是,当我在listView和我的适配器上滚动时,我需要更改微调器所选项的位置,它调用方法
{{1}}
帮助我,我需要在不调用其监听器的情况下更改微调器的位置。
答案 0 :(得分:0)
如果从侦听器调用ListView选项,您可以在侦听器中设置绕过选择逻辑的条件。
在你的类中声明一个布尔值:
boolean skip_listener = false;
并将您的听众更改为
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int clickPosition, long l) {
if(skip_listener){skip_listener = !skip_listener;return;}
int skipTotal = 0;
for(int i = 0 ; i < clickPosition ; i++)
{
SafeJSONObject surahObject = jsonArraySurahList.getJSONObject(i);
skipTotal+= surahObject.getInt("ayas");
}
SafeJSONObject surahObject = jsonArraySurahList.getJSONObject(clickPosition);
Log.e("spinnerSurah","spinnerSurah surahObject "+surahObject.toString());
positionSelection = skipTotal;
skip_listener = true;
listView.setSelection(positionSelection);
}
}